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!
)
)
Related
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!
)
Inside the for loop I'm trying to access the element at index count in CLs (this line of code: echo !!CLs[!count!]!!) , but I'm not sure how to do this. I don't really understand how expansion works in this case, so what you see below it me trying something out of no where.
#ECHO off
setlocal enableextensions enabledelayedexpansion
SET CLs[0]=#
SET /A count = 0
FOR /F "tokens=5" %%I IN ('some command') DO (
echo !!CLs[!count!]!! :: THIS LINE
IF NOT %%I == CLs[!count!] (
SET /A count += 1
SET CLs[!count!]=%%I
)
)
echo The item is %CLs[10]%
endlocal
Thanks
According to the post How does the Windows Command Interpreter (CMD.EXE) parse scripts? (see phase 5), the line echo !!CLs[!count!]!! cannot work, because the opening !! are collapsed to a single !, then !CLs[! is expanded to an empty string (assuming such variable is not defined), then count is returned literally, then !]! is expanded to an empty string and the final ! is dismissed. Or in other words, delayed expansion cannot be nested.
You can use call though to introduce another parsing phase, like this:
call echo %%CLs[!count!]%%
The line IF NOT %%I == CLs[!count!] ( ... ) is wrong, you must expand the right value too. However, call if will not help unfortunately, because if (like for and rem) is a special command that is recognised by the parser earlier than others, like call.
To work around that you can store the value of !count! in a for meta-variable, like %%J, for instance, to introduce another parsing phase, and use !CLs[%%J]! then, like this:
set /A "count=0"
for /F "tokens=5" %%I in ('some command') do (
for %%J in (!count!) do (
echo !CLs[%%J]!
if not "%%I" == "!CLs[%%J]!" (
set /A "count+=1"
set "CLs[!count!]=%%I"
)
)
)
Another yet slower possibility is to put the relevant code into a sub-routine:
set /A "count=0"
for /F "tokens=5" %%I in ('some command') do (
call :SUB !count!
)
goto :EOF
:SUB
echo !CLs[%~1]!
if not "%%I" == "!CLs[%~1]!" (
set /A "count+=1"
set "CLs[%~1]=%%I"
)
goto :EOF
You may also take a look at the post Arrays, linked lists and other data structures in cmd.exe (batch) script about how to deal with such pseudo-arrays.
ECHO ------------- START AT %time%
REM <!-- language: lang-dos -->
#ECHO Off
setlocal enableextensions ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q58209698.txt"
SET CLs[0]=#
SET /a clscnt[0]=0
SET /A count = 0
FOR /F "tokens=*" %%I IN ('type %filename1%') DO (
SET "processed="
FOR /f "tokens=1,2,3delims=[]=" %%a IN ('set cls[') DO IF /i "%%a"=="cls" (
IF "%%I"=="%%c" (SET /a clscnt[%%b]+=1&SET "processed=y")
)
IF not DEFINED processed SET /a count+=1&SET "cls[!count!]=%%I"&SET /a clscnt[!count!]=1
)
FOR /L %%a IN (0,1,%count%) DO ECHO !clscnt[%%a]! times !cls[%%a]!
ENDLOCAL
ECHO -------------------------Second way -----------------
#ECHO Off
setlocal enableextensions ENABLEDELAYEDEXPANSION
SET "sourcedir=U:\sourcedir"
SET "filename1=%sourcedir%\q58209698.txt"
SET CLs[0]=#
SET /a clscnt[0]=0
SET /A count = 0
FOR /F "tokens=*" %%I IN ('type %filename1%') DO (
SET "processed="
FOR /L %%a IN (0,1,!count!) DO (
IF "%%I"=="!cls[%%a]!" (SET /a clscnt[%%a]+=1&SET "processed=y")
)
IF not DEFINED processed SET /a count+=1&SET "cls[!count!]=%%I"&SET /a clscnt[!count!]=1
)
FOR /L %%a IN (0,1,%count%) DO ECHO !clscnt[%%a]! times !cls[%%a]!
ENDLOCAL
GOTO :EOF
I used a file named q58209698.txt containing some dummy data for my testing and chose to use the entire data line, having no suitable files where token 5 existed.
Note that as a bonus, I've added clscnt - an array of occurence-counts.
Shown: two separate ways of achieving the aim of finding/counting the unique tokens. Naturally, if the cls array is pre-loaded with the required tokens, then it's basic-programmer's-play to adjust the code to detect/report occurrences of those tokens.
The two methods are similar. In the first, set is used to list the established variables starting cls[. The first if ensures processing only the array-name cls, then either it's a repeat (set prcoessed to a value and increment the occurrences-counter) or it's a new value (when the for...%%a loop ends, processed is still undefined) so record it.
The second way is more direct, using the value of count to specifically interrogate the values in the cls array.
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
Am very new to batch scritping. I just tried to execute the below code for which am totally confused.
#echo off
set m=100
set i=0
FOR /L %%N IN (0,1,%m%)do (
set /a i=1+%i%
)
echo %i%
pause
Here, am incrementing i value until N reaches 100. So, am expecting the value of i at the end of loop to be 101 but it shows 1. Can anyone explain what's the reason behind this.
thanks
You need delayed expansion : http://www.robvanderwoude.com/variableexpansion.php
#echo off
setlocal enableDelayedExpansion
set m=100
set i=0
FOR /L %%N IN (;;0,1,%m%;;) do (
set /a i=1+!i!
)
endlocal & set /a i=%i%
echo %i%
In batch files, variable reads are replaced with their values at parse time, before executing the line or the block (code enclosed in parenthesis).
So, the %i% reference inside the for loop is replaced with the value of the variable before the for loop, that is, 0 , and %m% is replaced with 100, and what gets executed is
for /l %%n in (0 1 100) do ( set /a i=1+0 )
You can enable delayed expansion (setlocal enabledelayedexpansion command) and change the sintax from %i% to !i! to indicate the parser that reads to the variable should be delayed until the moment of executing the line. The code should be
#echo off
setlocal enabledelayedexpansion
set m=100
set i=0
FOR /L %%N IN (0,1,%m%)do (
set /a i=1+!i!
)
echo %i%
endlocal
The read of the value of i inside the for loop is delayed. This way, the real value is readed just before each execution of the line. Reference to i out of the loop does not need it. When the line is reached and parsed, the correct value is retrieved and echoed.
Anyway, while this is the general rule, set /a has a different behaviour. If you change your code to
set /a i+=1
or
set /a i=i+1
the parser will correctly identify the operation and execute the expected code with or without delayed expansion enabled.
The following code takes in all command line parameters of a batch file. In my case I have about 30 command line parameters and they are all numbers of 1, 2, or 3. I take them in then want to reassign them to other characters. I want each var, if it is a 1, change it to /*, if it's a 2, change it to */, if it's a 3, change it to #.
The first part works great, it's the second part of reassigning that I can't get the syntax for.
SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
set var!count!=%%i
set var!count!=!var!count!:1=/*! <--don't work
set var!count!=!var!count!:2=*/! <--don't work
set var!count!=!var!count!:3=#! <--don't work
set /a count=!count!+1
)
The problem is the way of accessing the array members.
In your case, the best way seems to use a temp variable.
SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
set "temp=%%i"
set "temp=!temp:1=/*!"
set "temp=!temp:2=*/!"
set "temp=!temp:3=#!"
set "var!count!=!temp!"
set /a count+=1
)
I think jeb has the simplest solution, but there are other options.
1) You can transfer the current value of count into a FOR variable. This is how I tend to do it.
SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
for %%N in (!count!) do (
set "var%%N=%%i"
set "var%%N=!var%%N:1=/*!"
set "var%%N=!var%%N:2=*/!"
set "var%%N=!var%%N:3=#!"
)
set /a count+=1
)
2) You can use CALL to delay expansion of the outer variable, but I don't like this option because the normal expansion is not as safe as delayed expansion.
SETLOCAL ENABLEDELAYEDEXPANSION
set count=1
FOR %%i IN (%*) DO (
set "var!count!=%%i"
call set "var!count!=%%var!count!:1=/*%%"
call set "var!count!=%%var!count!:2=*/%%"
call set "var!count!=%%var!count!:3=#%%"
set /a count+=1
)