Batch Coding For loop to create files with different names - batch-file

I'm new to Batch coding, so please go easy.
Please consider the following code:
FOR /L %%G IN (1,1,20) DO (break>"C:\New folder\%%G+1.txt")
I'm trying to create text files with the above code, but I'm getting 1+1, 2+1, 3+1.. and so on.
Is there a way to not touch the parameters, but to increase the parameter in %%G+1? Instead of outputting as a string, it gives me a number.
Please guide me. thanks
Update: I tried this code below
:MakeTextFiles
setlocal enabledelayedexpansion
set "var=1"
FOR /L %%G IN (1,1,20) DO
(
set /a "var=%var%+1"
break>"C:\New folder\!var!.txt"
)
EXIT /B
But it's not working.

setlocal enabldelayedexpansion
FOR /L %%G IN (1,1,20) DO
(
set /a _new=%%G+1
break>"C:\New folder\!_new!.txt"
)
Please see hundrds of articles on SO about delayedexpansion
Two problems with your latest change:
The ( must be on the same physical line as the do.
set /a var=!var!+1
or
set /a var=var+1
or
set /a var+=1
set /a accesses the run-time value of var, !var! is the run-time value of var, %var% is the parse-time value of var - the value that it has when the for is encountered.

You don't need arithmetic addition here, just change the set you loop over:
FOR /L %%G IN (2,1,21) DO (break>"C:\New folder\%%G.txt")
If you definitely want arithmetic:
setlocal enabledelayedexpansion
FOR /L %%G IN (1,1,20) DO (
set /a var=%%G+1
break>"C:\New folder\!var!.txt")
You need to look here:
calculating the sum of two variables in a batch script
and here
delayed expansion

Related

can I use a variable to extract a substring in batch?

I need to extract the characters of a string one by one in a loop. Ideally, I would've done something like this, but as you might have guessed, it doesn't work.
#echo off
setlocal EnableDelayedExpansion
set /a len=5
set var=abcde
for /l %%n in (1,1,%len%) do (
set /a num=%%n - 1
echo %var:~!num!,1%
)
it works seamlessly if I replace !num! with a plain number, but with the variable, behaves as if the percent signs aren't there and echoes:
var:~0,1
var:~1,1
var:~2,1
var:~3,1
var:~4,1
To directly fix your issue replace:
echo %var:~!num!,1%
with:
call echo %%var:~!num!,1%%`
But you can do it without set /a num=%%n - 1 because you are already counting using for /L but note we are counting from 0.
Also note, we start couting from 0.
#echo off
setlocal EnableDelayedExpansion
set /a len=4
set "var=abcde"
for /l %%n in (0,1,%len%) do (
echo(!var:~%%n,1!
)

Batch - Variable substring in a For loop, with variable range

For the record, seeing that this type of questions is pretty popular on SO, I read around 30 answers in SO and couldn't find a question and answer that fit perfectly to my needs.
I have the following snippet:
#echo off
setlocal EnableDelayedExpansion
set string=test
for /l %%l in (0,1,1) do (
set /A remainder=%%l %%2
if remainder equ 1 (
set /A curr=%%l+1
call set res=!string:~-%curr%,1!
echo !res!
)
)
When executed this way, I get:
string:~-curr
How do I get the line with the variable substring with variable range - call set res=!string:~-%curr%,1!, to execute properly? For instance, for the second index (1), I want it to execute as if set res=!string:~-2,1! is written there.
How do I solve this?
You cannot use normal (immediate) expansion for the variable curr since you write and read it in the same block of code. However, since nested delayed expansion (like !string:~-!curr!,1!) does not work (because !string:~-! and !,1! were seen as variables then, and the former was even invalid syntax), you could use an interim for loop:
#echo off
setlocal EnableDelayedExpansion
set "string=test"
for /L %%l in (0,1,1) do (
set /A "remainder=%%l%%2"
if !remainder! equ 1 (
set /A "curr=%%l+1"
for %%k in (!curr!) do (
set "res=!string:~-%%k,1!"
echo(!res!
)
)
)
endlocal
(I prefer this over the approach call set "res=%%string:~-!curr!,1%%", because call may introduce trouble with the caret symbol ^ and it is slower.)
I have no idea, what your code is supposed to do, but to clear the error you mention:
#echo off
setlocal EnableDelayedExpansion
set string=test
for /l %%l in (0,1,1) do (
set /A remainder=%%l %%2
if !remainder! equ 1 (
set /A curr=%%l+1
call set res=%%string:~-!curr!,1%%
echo !res!
)
)

Syntacs for EnableDelayedExpansion in nested for loops

If I simplified my requirement, I want to get last two digits of these strings using batch file. This is not the exact requirement. but I made it simple for understanding. :)
11-22-33
11-22-44
11-22-55
11-22-66
expected outcome is
33
44
55
66
This is the code I wrote
#echo off
SetLocal EnableDelayedExpansion
REM Fill strings to a array
set DIR_COUNT=0
for %%x in ("11-22-33" "11-22-44" "11-22-55" "11-22-66") do (
set /A DIR_COUNT+=1
set CONTENT_DIR_NAME=%%~x
set "DIR_LIST[!DIR_COUNT!]=!CONTENT_DIR_NAME!"
)
REM This part is working when I hard code the index of array to 1. I placed this two lines for testing purpose of above code.
for %%a in (%DIR_LIST[1]:-= %) do set mfgName=%%a
echo %mfgName%
REM this loop doesn't work because the syntax not correct.
for /L %%i in (1,1,%DIR_COUNT%) do (
for %%a in (%!DIR_LIST[%%i]!:-= %) do (
set mfgName=%%a
echo !mfgName!
)
)
As my understanding syntax of (%!DIR_LIST[%%i]!:-= %) is not correct. Any ideas how to DelayedExpansion inside a DelayedExpansion and correct this syntax
Here is the correct syntax for your batch file.
#echo off
SetLocal EnableDelayedExpansion
REM Fill strings to a array
set DIR_COUNT=0
for %%x in ("11-22-33" "11-22-44" "11-22-55" "11-22-66") do (
set /A DIR_COUNT+=1
set CONTENT_DIR_NAME=%%~x
set "DIR_LIST[!DIR_COUNT!]=!CONTENT_DIR_NAME!"
)
for /L %%i in (1,1,%DIR_COUNT%) do (
for %%a in (!DIR_LIST[%%i]:-^= !) do (
set mfgName=%%a
echo !mfgName!
)
)
pause

Batch-File Variable Part of Another Variable

Okay so let's say I have the following code
set playerlevel=5
set x=player
echo %x%level
I want the output to be "5", is that possible?
dbmitch's answer is good for integers, but if you want to display strings or integers, you can simply use delayed expansion.
#echo off
setlocal enabledelayedexpansion
set playerlevel=5
set x=player
echo !%x%level!
Note that if the code is located inside of a code block (i.e. enclosed in parentheses), the syntax is slightly different.
#echo off
setlocal enabledelayedexpansion
REM This is just an example code block to show off the alternate syntax with the %%s
for /L %%A in (1,1,1) do (
set playerlevel=5
set x=player
call echo %%!x!level%%
)
You can get what you want by using a combination of enabledelayedexpansion and the set /a to emulate an EVAL function
Try this:
#echo off
setlocal enabledelayedexpansion
set playerlevel=5
set x=player
set /a varx = "%x%level"
echo %varx%

Windows Batch: Loop through to echo a set of variables

I have a batch file that contains the code below which assigns a variable for each line in a text file.
So the text file might have:
RELEASE1
RELEASE2
RELEASE3
The batch file sets each line in the text file to var1, var2, var3 with the following code:
#echo off
setlocal ENABLEDELAYEDEXPANSION
set vidx=0
for /F "tokens=*" %%A in (sites.txt) do (
SET /A vidx=!vidx! + 1
set var!vidx!=%%A
)
I need a method to echo all the defined variables. The number of variables will always change. So it might go up to var8 or var10 etc...
I'm pretty certain a for loop would do the trick but not sure what the best approach or how to do it? I was thinking of using vidx as the number of iterations? Thanks for your help!
Very Easy
#echo off
setlocal ENABLEDELAYEDEXPANSION
set count=0
for /F "tokens=*" %%A in (sites.txt) do (
SET /A count+= 1
set var!count!=%%A
)
Rem //:Notice That %count% still stores the last variable, use this in a "for /l" loop:
for /l %%a in (1,1,%count%) do (
Rem Below line is optional and displays variable number
<nul set /p="Variable %%a: "
Echo !var%%a!
)
That should work fine as long as none of the data contains parenthesis, in which case you'll have to escape them.
Type for /? for help on this. Ask If you want an explanation.
set var|findstr /R "var[0-9]*="
May be the shortest way....

Resources