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

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

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

#1 Post by goodywp » 10 Oct 2024 15:52

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
Last edited by goodywp on 11 Oct 2024 14:05, edited 2 times in total.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: to set variable in a range of values

#2 Post by miskox » 11 Oct 2024 01:40

Maybe you could add another FOR loop to gor thru these values?

Saso

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

Re: to set variable in a range of values

#3 Post by goodywp » 11 Oct 2024 07:03

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.

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

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

#4 Post by miskox » 13 Oct 2024 08:22

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

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

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

#5 Post by goodywp » 15 Oct 2024 09:45

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


miskox
Posts: 630
Joined: 28 Jun 2010 03:46

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

#6 Post by miskox » 15 Oct 2024 11:53

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

goodywp
Posts: 265
Joined: 31 Jul 2017 09:57

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

#7 Post by goodywp » 15 Oct 2024 13:55

Thanks again!

Post Reply