I am having problems with variables transferring throughout my batch file.
This is a rough example of what I have:
#echo off
setlocal enabledelayedexpansion
:one
set variableone=outputone
set variabletwo=outputtwo
set variablethree=outputthree
goto two
:two
set /a variable%variableone%four=numberone
set /a variable%variabletwo%five=numbertwo
set /a variable%variablethree%six=numberthree
goto three
:three
set /a variable%variableone%four+=(2*(!variable%variabletwo%five!-!!variable%variablethree%six!)
echo !variable%variableone%four!
exit
It is a lot longer than that and this is just a simplified version of what it actually is, however, the variables in the label ":three" won't transfer down so the variable ends up blank which leaves the equation blank as well. Is there a way to fix this?
It's tremendously difficult to see what you are doing.
In the below code, I've replaced the variable names NUMBER* with values.
I've also added the missing close-parenthesis and I'm wondering about the two successinve !
#ECHO OFF
setlocal enabledelayedexpansion
:one
set variableone=outputone
set variabletwo=outputtwo
set variablethree=outputthree
goto two
:two
set /a variable%variableone%four=numberone
set /a variable%variabletwo%five=numbertwo
set /a variable%variablethree%six=numberthree
set /a variable%variableone%four=14
set /a variable%variabletwo%five=25
set /a variable%variablethree%six=36
goto three
:three
set /a variable%variableone%four+=(2*(!variable%variabletwo%five!-!!variable%variablethree%six!))
echo !variable%variableone%four!
set var
Now for me, what is being echoed is -8 which is equal to 14+(2*(25-36))
So - works for me!
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
I'm trying to create a variable that will link to others based off of a variable result, but can't seem to get it to work. Here's the code:
#echo off
set rollnumbera=10
set /a num=%random% %%rollnumbera +1
set "message=msg%num%"
set msg1=a
set msg2=b
set msg3=c
set msg4=d
set msg5=e
set msg6=f
set msg7=g
set msg8=h
set msg9=i
set msg10=j
echo %message%
echo %num%
pause
When I run it it displays the msg%num% result, but does not seem to be linking to the set msg#s. I'm wondering if there is a way to do that, or if an "if %num% == message number (echo message)" is required. This is just an ease of access question, but any advice would be appreciated.
The fixed code:
#echo off
setlocal enabledelayedexpansion
set rollnumbera=10
set /a num=(%random% %% rollnumbera)+1
set msg1=a
set msg2=b
set msg3=c
set msg4=d
set msg5=e
set msg6=f
set msg7=g
set msg8=h
set msg9=i
set msg10=j
for %%a in ("!msg%num%!") do set message=%%a
echo %message%
echo %num%
pause
I'm making a card game, so I'm trying to set a specific amount of times a number can be used, so I'm basically looking for either a way to do "double variables" or a way around them:
#echo off
color 0a
set 1=13
set 2=13
set 3=13
set 4=13
:loop
if %counter%==0
goto :skip
set /a card=%random%*4/32767+1
set %card%=%%card%%-1
goto :loop
:skip
....
I think this is the logic you are going for. Your assumption was correct. You do need double variable expansion. This can be achieved two ways. With Delayed Expansion or using the CALL command. I chose the latter. For the example I set the counter to 13 and kept echoing the contents of each of the variables after each loop.
#echo off
set _1=13
set _2=13
set _3=13
set _4=13
set "counter=13"
:loop
IF "%counter%"=="0" goto skip
set /a card=%random% %% 4 +1
call set /a _%card%=%%_%card%%% - 1
echo _1=%_1%
echo _2=%_2%
echo _3=%_3%
echo _4=%_4%
set /a counter-=1
goto :loop
:skip
pause
EDIT: Now that I look at that again, you actually don't need to use the CALL at all or delayed expansion because the SET command will take care of it for you.
#echo off
set _1=13
set _2=13
set _3=13
set _4=13
set "counter=13"
:loop
IF "%counter%"=="0" goto skip
set /a card=%random% %% 4 +1
set /a _%card%-=1
echo _1=%_1%
echo _2=%_2%
echo _3=%_3%
echo _4=%_4%
set /a counter-=1
goto :loop
:skip
I have following code:
set b=Hello
set c=1
set d=5
for /l %%x in (1,1,%d%) do (set /a c=c+1 & set "a=%a%%b%%c%")
echo %a%
And for this, I wanted an output as:
Hello1Hello2Hello3Hello4Hello5
But instead, I get result as:
Hello1Hello1Hello1Hello1Hello1
Can anyone give me any idea on how can I do so?
You need:
setlocal enableextensions enabledelayedexpansion
set b=Hello
set c=0
set d=5
for /l %%x in (1,1,%d%) do (set /a c=c+1 & set "a=!a!!b!!c!")
echo %a%
endlocal
The ! variant of % will expand the variables at the time the code is executed whereas % expands when the code is parsed. And, since parsing happens on the entire statement (from for to the closing parenthesis), you'll get the original value only.
Note that you don't need delayed expansion for the invariant b but, once you've decided you need them, you may as well use them everywhere - they act more as you'd expect in most cases.
You'll notice I've also changed the initial value of c to get the output you stated that you wanted. As it was, you would get 2,3,4,5,6 rather than 1,2,3,4,5.
I think this is what you want:
#Echo Off
Set "a="
Set "b=Hello"
Set "c=1"
Set "d=5"
For /L %%A In (%c%,1,%d%) Do Call Set "a=%%a%%%b%%%A"
If Defined a Echo %a%
Pause
Or alternatively:
#Echo Off
Set "a="
Set "b=Hello"
Set "c=1"
Set "d=5"
For /L %%A In (1,%c%,%d%) Do Call Set "a=%%a%%%b%%%A"
If Defined a Echo %a%
Pause
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)