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!
)
Related
Forgive me if there is a simple answer to this, I'm new to all of this.
The below .bat script generates a list of numbers depending on how many numbers you want.
However what I would like is to format the numbers it generate.
For example, if I input 20, instead of it coming out 1, 2, 3 etc. I would like it to come out as 001, 002... 020.
Is this possible? Am I missing something obvious?
Many Thanks.
#echo off
setlocal EnableExtensions
:start
cls
set /p loopcount= How Many Users?:
set "x="0"
:loop
set /a "x+=1"
echo %x%
set /a loopcount=loopcount-1
if %loopcount%==0 goto exitloop
goto loop
:exitloop
pause
goto start
just implementing SomethingDark's suggestion (and a minor change in the logic of the loop):
set /p "loopcount= How Many Users?: "
set "x=1000"
:loop
set /a x+=1
echo %x:~-3%
set /a loopcount-=1
if %loopcount% gtr 0 goto :loop
echo loop finished.
(btw: your set "x="0" has a quote too much (probably a typo)
Here's a quick example batch file which uses powershell for your leading zeroes.
I have used a for loop as the looping mechanism.
#Echo Off
SetLocal EnableExtensions
:AskNum
Set "num=1"
Set /P "num=how many users?"
Set num | findstr.exe /RX "^num=[123456789][0123456789]*$" 1>NUL || GoTo AskNum
For /F %%G In ('powershell.exe "1..%num% | %% ToString User000"') Do Echo %%G
Pause
This script will not continue unless the end user inputs an integer, without a leading 0, and which is a minimum value of 1. Here you can modify the Echo command in Do to an alternative, for example net.exe User %%G /Add, or if you wish, to a parenthesized sequence of commands. In each case %%G will contain the returned string with the incremented three digits.This version prepends each three digit number sequence with an additional string, (User), but that can simply be deleted or replaced if/as required.
#ECHO OFF
SETLOCAL enabledelayedexpansion
SET /p "lastnum= How many numbers? "
SET /a lastnum+=1000
FOR /L %%e IN (1001,1,%lastnum%) DO SET /a num=%%e&ECHO !num:~-3!
GOTO :EOF
No need for complication
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!
)
)
I have a batch script which loops and I want to count how many cycles it has done.
This is how the script looks:
#echo off
title MyTitle
set cycles=0
:startpoint
echo %cycles%
(my command)
goto startpoint
I would like to be able to see the variable "cycles" increment by 1 each time it goes back to :startpoint, how do i do that?
To perform arithmetic operations in batch you need to use the /a switch with the set command:
#echo off
title MyTitle
set cycles=0
:startpoint
set /a cycles=cycles+1
echo %cycles%
...
(my command)
...
goto startpoint
Type set /? in cmd for more information.
Using this code
setlocal enableextensions enabledelayedexpansion
set /a count = 1
for /f "tokens=*" %%a in (config.properties) do (
set /a count += 1
echo !count!
)
endlocal
works for me, the reason because I was using %count% instead of !count! so I keep getting 1 instead of the expected output. So if using %% doesn't work for you, you can as well use !! to either display your output or do your calculations or comparisons.
well this worked for me.
#echo off
setlocal enabledelayedexpansion
SET /A i = 1
for /f "tokens=*" %%f in (temp.txt) do (
IF !i!==2 echo %%f
SET /a i+=1
)
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
I am new in batch. Trying for some days to make something in batch but have a problem I cannot solve. I read a lot of your comments but did not find answer. Maybe you can help me?
The point is:
I input string from keyboard( e.g. 10 characters ). name of it is"allinputstring"
Calculate of length is ok ( by redirect in txt file and expand its bytes ). name "length"
Parse string in 10 pieces (strings) is ok.
So here is a problem, I want to echo these pieces, so I use next code, I use a counter to find out is the counter give me good count as output variable, and echo it to see on screen if it is good. Counter seems good, end echo of pieces strings is good enough. But I want to put in line 5. Variable count instead of "%%m", and cannot find a syntax way how to do it.
setlocal enabledelayedexpansion
for /l %%m in (1,1,!lenght!) do (
set /a count=0
set /a count=count+%%m
echo !count!!allinputstring:~%%m,1!
)
endlocal
please help me.
Try this:
#echo off &setlocal enabledelayedexpansion
set /a lenght=9
set "allinputstring=ABCDEFGHIJ"
for /l %%m in (0,1,%lenght%) do (
set /a count=0
set /a count+=%%m
echo !count! !allinputstring:~%%m,1!
)
endlocal
Output is:
0 A
1 B
2 C
3 D
4 E
5 F
6 G
7 H
8 I
9 J
#ECHO off
setlocal ENABLEDELAYEDEXPANSION
SET allinputstring=abcdefghijk
SET lenght=10
for /l %%m in (1,1,!lenght!) do (
set /a count=0
set /a count=count+%%m
FOR %%z IN (!count!) DO echo !count! !allinputstring:~%%z,1!
)
GOTO :eof
Does this do what you require?
So... to make COUNT show (I've assigned it to KOWNT, but the syntax endlocal&set count=%count% would assign it to COUNT instead)
I've changed the starting value of the FOR/L because character counting starts from character#0 in the string.
#ECHO off
setlocal ENABLEDELAYEDEXPANSION
SET allinputstring=abcdefghij
SET lenght=9
for /l %%m in (0,1,!lenght!) do (
set /a count=0
set /a count=count+%%m
FOR %%z IN (!count!) DO echo !count! !allinputstring:~%%z,1!
)
endlocal&SET KOWNT=%count%
ECHO Now KOWNT=%KOWNT% but count=%count% because we have exited the SETLOCAL
GOTO :eof
When the ENDLOCAL is encountered, the parser substitutes the CURRENT value of the variables in the line and THEN executes the line.
Hence, the line is executed as
endlocal&set KOWNT=9
since the value of count at the time is 9.
When the SETLOCAL is executed, all changes to the environment since the matching SETLOCAL are thrown away. The environment variables are restored to their state when the SETLOCAL was executed and count becomes empty again (as it was before the routine.) THEN the SET instruction is executed, which sets KOWNT to 9.