Here's the little bit of code I have:
if %a%==1 set /a count=%count%+%random% %%100
What it does (at the moment) is simply stay as %count% and not increase by a random number between 1 and 100.
What I want it to do is this:
If a is equal to 1 (which it always is in an earlier line of code) it will increase count by a random number between 0 and 99. If anyone could help, that would be great! (It's my first time on stackoverflow and I'm new at programming so make it simple!)
I'm not sure what your problem is? Have you posted your full script? The following script works for me.
#echo off
setlocal
set a=1
set count=0
echo count: %count%
if %a% == 1 set /a count=%count%+%random% %%100
echo count: %count%
As MC ND points out you may have problems if you're using count inside another block (e.g., inside a for-loop). The following does not work.
#echo off
setlocal
set a=1
set count=0
for /L %%x in (1,1,1) do (
if %a% == 1 set /a count=%count%+%random% %%100
rem This always prints 0!
echo count: %count%
)
To get it working use setlocal enableDelayedExpansion. The following is again working.
#echo off
setlocal enableDelayedExpansion
set a=1
set count=0
for /L %%x in (1,1,1) do (
if !a! == 1 set /a count=!count!+%random% %%100
rem Now this works!
echo count: !count!
)
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!
)
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 would like to print the following:
0
1
2
3
4
I have tried this:
ECHO OFF
FOR /L %%A in (1,1,5) DO (
SET /a "B=%%A-1"
ECHO %B%
)
However, this gives me:
4
4
4
4
4
How can I achieve the desired output while using both A and B in my code?
ECHO OFF
setlocal
FOR /L %%A in (1,1,5) DO (
SET /a "B=%%A-1"
call ECHO %%B%%
)
Since you are not using setlocal, B will be set to the value from the previous run. %B% will be replaced by 4 since B was set to 4 by the previous run. the call echo trick uses a parsing quirk to retrieve the current (run-time) value of the variable.
Here's "the official" way:
ECHO OFF
setlocal enabledelayedexpansion
FOR /L %%A in (1,1,5) DO (
SET /a "B=%%A-1"
ECHO !B!
)
In delayedexpansion mode, !var! retrieves the value of var as it changes at run-time. This is not without its drawbacks, but you'd need to read up on delayedexpansion for a guide on that matter.
I have the code:
#echo off
set /p dec="Path? "
set patha="C:\Users\%username%\%dec%"
set /a i=2
setlocal EnableDelayedExpansion
:import
if "%i%"=="12" goto loopend
if "!patha:~-%i%,1!"=="." set ext="!patha:~-%i%!"
set /a i=%i%+1
goto import
:loopend
echo %ext%
pause
It loops through the code 10 times, but when I have it echo "!patha:~-%i%,1!" and it echoes "." it doesn't set the ext variable. Am I doing comparisons wrong?
Works happily or me in W7, although
FOR /f %%i IN ("%patha%") DO SET ext="%%~xi"
echo %ext%
would seem to do the same thing.
That is, assuming you want to extract the last n characters starting "."... (that's not clear)
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.