Page 1 of 1

How to set and make a range of values working in one function

Posted: 10 Oct 2024 15:52
by goodywp
Hi All,

I have a piece of code below to insert string after that line number.

Code: Select all

setlocal DisableDelayedExpansion
set randomline=267
set "lineno=0"
(
    FOR /F "delims=" %%L in ('findstr /n "^" Input.robot') do (
        set /a lineno+=1
        set "line=%%L"
        setlocal EnableDelayedExpansion        
        if "!lineno!"=="%randomline%" call :insertblock
        set "line=!line:*:=!"
        (echo(!line!)
        endlocal
    )
) > Output.robot
exit /b

:insertblock
echo     [Tags]    BVTN
The above code only insert after line 267. How can I make the above code to insert the string in multiple lines via set ramdomline=(multiple values)
How to do it?
Thanks

Re: to set variable in a range of values

Posted: 11 Oct 2024 01:40
by miskox
Maybe you could add another FOR loop to gor thru these values?

Saso

Re: to set variable in a range of values

Posted: 11 Oct 2024 07:03
by goodywp
Thanks!

I tried below and only got the last value

Code: Select all

setlocal DisableDelayedExpansion
FOR %%G IN (152 159 198 205) DO set "randomline=%%G"
set "lineno=0"
(
    FOR /F "delims=" %%L in ('findstr /n "^" Input1.robot') do (
        set /a lineno+=1
        set "line=%%L"
        setlocal EnableDelayedExpansion
        if "!lineno!"=="%randomline%" call :insertblock
        set "line=!line:*:=!"
        (echo(!line!)
        endlocal
    )
) > Output.robot
exit /b

:insertblock
echo     [Tags]    BVTN
But if I use below code nothing happened

Code: Select all

setlocal DisableDelayedExpansion
set "lineno=0"
(
    FOR /F "delims=" %%L in ('findstr /n "^" Input1.robot') do (
        set /a lineno+=1
        set "line=%%L"
        setlocal EnableDelayedExpansion
	FOR %%G IN (152 159 198 205) DO set randomline=%%G        
        if "!lineno!"=="%randomline%" call :insertblock
        set "line=!line:*:=!"
        (echo(!line!)
        endlocal
    )
) > Output.robot
exit /b

:insertblock
echo     [Tags]    BVTN
How I can make that range of values works inside the funtion of insertation.

Re: How to set and make a range of values working in one function

Posted: 13 Oct 2024 08:22
by miskox
Another FOR loop can't be used because you are inserting new string in a new line - and this increases number of lines.

Try this:

Code: Select all

@echo off
REM generate input file as we don't have it
break>input.robot
for /L %%f in (1,1,300) do echo %%f_ABCDEFG>>input.robot

REM remove lines above - they are here just to generate input file

setlocal DisableDelayedExpansion

set randomline=#152#159#198#205#267#

set "lineno=0"
(
    FOR /F "delims=" %%L in ('findstr /n "^" Input.robot') do (
        set /a lineno+=1
        set "line=%%L"
        setlocal EnableDelayedExpansion        
        set "line=!line:*:=!"
        echo/!line!
	call set randomlinetmp=%%randomline:#!lineno!#=%%
	if not "!randomlinetmp!"=="!randomline!" echo     [Tags]    BVTN
        endlocal
    )
) > Output.robot
exit /b
Output:

Code: Select all

151_ABCDEFG
152_ABCDEFG
    [Tags]    BVTN
153_ABCDEFG
154_ABCDEFG
.
.
.
158_ABCDEFG
159_ABCDEFG
    [Tags]    BVTN
160_ABCDEFG
161_ABCDEFG
.
.
.
197_ABCDEFG
198_ABCDEFG
    [Tags]    BVTN
199_ABCDEFG
200_ABCDEFG
.
.
.
204_ABCDEFG
205_ABCDEFG
    [Tags]    BVTN
206_ABCDEFG
207_ABCDEFG
.
.
.
266_ABCDEFG
267_ABCDEFG
    [Tags]    BVTN
268_ABCDEFG
269_ABCDEFG
Saso

Re: How to set and make a range of values working in one function

Posted: 15 Oct 2024 09:45
by goodywp
Thanks Miskox! It works but I removed your code for generating input.txt since I already had input.txt

Code: Select all

@echo off
setlocal DisableDelayedExpansion
For /f %%a In ('WMIC Path Win32_LocalTime Get DayOfWeek^|Findstr [0-6]') Do ( 
    Set DOW=%%a)
IF %DOW%==1 (set randomline=#151#158#204#211#252#260#310#316#)
IF %DOW%==2 (set randomline=#165#169#215#219#264#268#326#)

set "lineno=0"
(
    FOR /F "delims=" %%L in ('findstr /n "^" Input1.robot') do (
        set /a lineno+=1
        set "line=%%L"
        setlocal EnableDelayedExpansion        
        set "line=!line:*:=!"
        echo/!line!
	call set randomlinetmp=%%randomline:#!lineno!#=%%
	if not "!randomlinetmp!"=="!randomline!" echo     [Tags]    BVTN
        endlocal
    )
) > Output.robot
exit /b


Re: How to set and make a range of values working in one function

Posted: 15 Oct 2024 11:53
by miskox
You could use this (see viewtopic.php?p=70603#p70603)

Code: Select all

For /f %%a In ('WMIC Path Win32_LocalTime Get DayOfWeek /value ^|find "="') Do Set "%%a"
instead of

Code: Select all

For /f %%a In ('WMIC Path Win32_LocalTime Get DayOfWeek^|Findstr [0-6]') Do ( 
    Set DOW=%%a)
Of course

Code: Select all

IF %DOW%
should be changed to

Code: Select all

IF %dayofweek%
or even better to

Code: Select all

IF "%dayofweek%"
Saso

Re: How to set and make a range of values working in one function

Posted: 15 Oct 2024 13:55
by goodywp
Thanks again!