Batch File Variables Not Working how they should - file

I am running this code:
#echo off
set /a var=0001
:start
if %var% == 10 set %var5%=00
if %var% == 100 set %var5%=0
if %var% == 1000 set %var5%=
if %var% == 9999 pause
set /a var=%var5%%var%+1
set var2=STRING %VAR%
set var3=ENTER
set var4=DELAY 1000
echo %var2%
echo %var3%
echo %var4%
#echo %var2%>> inject.txt
#echo %var3%>> inject.txt
#echo %var4%>> inject.txt
goto start
Which I made to generate a text file for a brute forcer that runs on a Usb rubber ducky. But, the if %var% == 10 set %var5%=00 Does not work. When it reaches 10, 100, or 1000 it does not add the 0000 to the beginning of the number. Please help, I'm using this for a friend that had his steam account hacked, and he needs to crack the 4 digit pin for the "parental controls"

Get rid of the %%around var5 in the set statement. You use the %% syntax only to read the value of an env var, not to assign.

The value assigned by set /a will suppresss leading zeroes; the hard way here is to use strings.
The easy way is this:
set /a var=10000
:loop
set "anothervar=%var:~-4%"
echo %anothervar%
set /a var+=1
if %var% lss 20000 goto loop

Related

An If statement in my .bat script isn't working as expected

What is supposed to happen is that you input a number between 1 and 1,048,567. The program checks if your input is actually a number between 1 and 1,048,567. If your input is a valid number then it will continue onto the next bit of code. If the input is invalid then it will display a message saying it is invalid then loop back and ask you for input again.
When I run this however, I input anything and it says invalid input even if I did input a number between 1 and 1,048,567.
Code:
:setup
#echo off
title Pokemon Shiny Sim
set delay = nul
set count = 0
set chance = 4096
:settings
:setChance
cls
echo Set shiny chance (1 in x). Range: (1-1,048,567)
echo Leave blank for 1 in 4096.
set /p chance = Input:
set /a chance = %chance%+0
if %chance% GEQ 1 (
if %chance% LEQ 1048567 (
goto setDelay
)
)
echo Invalid Input.
pause
goto setChance
:setDelay
cls
echo Set delay between attempts in seconds. Range: (1-60).
echo Leave blank for no delay.
set /p delay = Input:
set /a delay = %delay%+0
if %delay% == nul (
goto loopStart
)
if %delay% GEQ 1 (
if %delay% LEQ 60 (
cls
goto loopStart
)
)
echo Invalid Input.
pause
goto settings
:loopStart
set /a count = %count%+1
set /a rand = %random% %% %chance%+1
if %rand% == 1 (
echo Attempt: %count% | Shiny: Yes!
pause
)
else (
echo Attempt: %count% | Shiny: No
)
goto loopStart
I suggest to read first debugging a batch file and second the answer onWhy is no string output with 'echo %var%' after using 'set var = text' on command line?
Next look on your rewritten code below:
:setup
#echo off
title Pokemon Shiny Sim
set "delay=0"
set "count=0"
set "chance=4096"
:settings
:setChance
cls
echo Set shiny chance (1 in x). Range: (1-1,048,567)
echo Leave blank for 1 in 4096.
set /P "chance=Input: "
set /A chance+=0
if %chance% GEQ 1 if %chance% LEQ 1048567 goto setDelay
echo Invalid input.
pause
goto setChance
:setDelay
cls
echo Set delay between attempts in seconds. Range: (1-60).
echo Leave blank for no delay.
set /P "delay=Input: "
set /A delay+=0
if %delay% == 0 goto loopStart
if %delay% GEQ 1 if %delay% LEQ 60 cls & goto loopStart
echo Invalid input.
pause
goto settings
:loopStart
set /A count+=1
set /A rand=%random% %% chance + 1
if %rand% == 1 (
echo Attempt: %count% ^| Shiny: Yes!
pause
) else (
echo Attempt: %count% ^| Shiny: No
)
goto loopStart
All spaces around the equal signs are removed in this batch code.
The command line set "delay = nul" is modified to set "delay=0" because the condition if %delay% == nul is never true after execution of set /a delay = %delay%+0 resulting in execution of set /a delay = nul + 0 which results in assigning value 0 to environment variable delay on nul not existing as environment variable with that name having an integer value. The result of a valid arithmetic expression is always a number assigned as string to the environment variable and never a string like nul.
set /a chance = %chance%+0 is modified to set /A chance+=0 and set /a delay = %delay%+0 is modified to set /A delay+=0 because otherwise the input check is insecure as the user has for example the freedom to enter | for variable chance resulting in execution of command line set /a chance = |+0 which cause an unexpected exit of batch file execution.
Never use %variable% or !variable! in an arithmetic expression as not needed in general.
The help output on several pages on running set /? in a command prompt window explains in chapter about usage of set /A that each string which can't be interpreted as number or operator isĀ interpreted automatically as name of an environment variable whose current value should be converted to an integer on evaluation of the expression. If the environment variable is not defined at all or its value can't be successfully converted to a 32-bit signed integer, it is replaced in the expression by integer value 0.
There are exceptions like the usage of a random number in an arithmetic expression which requires %random% or !random! or when a variable name contains a space character or a character which would be interpreted as operator. In such cases it is necessary that the Windows command interpreter replaces the environment variable name already in preprocessing state or immediately before execution of the command set by random value respectively value of the environment variable.
set /a chance = %chance%+0 makes it also possible that the user of this batch file enters for example PROCESSOR_LEVEL or PROCESSOR_REVISION and this input although not being a number at all would be handled as valid because those two strings are the names of environment variables having numbers as values. PROCESSOR_REVISION has by default a hexadecimal number assigned which can be processed nevertheless completely or partly as number by command set.
Another syntax error is in block
if %rand% == 1 (
echo Attempt: %count% | Shiny: Yes!
pause
)
else (
echo Attempt: %count% | Shiny: No
)
The keyword else must be on same line as the closing ) of true branch of the IF condition separated from ) with a space character.
And redirection operator | must be escaped with caret character ^ to be interpreted as literal character to output into console window.
Note: set /A chance+=0 makes it still possible to enter for example 170 percent or 170X which results in chance having value 170 and therefore input is valid although in real the entered string is not a number.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
cls /?
echo /?
goto /?
if /?
pause /?
set /?
title /?
I haven't tested the code yet, but I found some fatal issues in the code.
Mis-setting variable
set /a count = %count% + 1
This sets a variable count (Note the space!). Remove the space! Also, this can be shortened to set /a count+=1.
ECHOing special characters
| is one of the special characters reserved for redirection in batch. To properly echo it, use echo string ^| string instead.
Poor IF statement practice
if %rand% == 1 (
only works when %rand% is alphanumeric. If %rand% is space, the cmd.exe sees:
if == 1 (
which is incorrect.
To correct it, do
if "%rand%"=="1" (
Alternatively, use EQU for numeric comparison, and == for string comparison.

Why are mathematical operations and output of environment variables in an ELSE branch not working as expected?

I am trying to use nested if-else statements in order to implement or operator.
The problem is that the code only works outside the last nested else statement and I can't figure out why.
I added some notes marked with // that are not actually in the script to help you get a clue of what I am trying to do.
Here is my batch script:
:computerMakePick
setLocal
set /a currentNumber= 15
set /a addOne=%currentNumber%+1
set /a addTwo=%currentNumber%+2
//the next segment implements OR operator for two conditions using nested if-else statement
if %addOne% == 7 ( //checking first condition.
echo Computer chose: %addOne%
set /a currentNumber= %addOne%
)else (
if %addTwo% == 8 ( // now checking: OR second condition
echo Computer chose: %addTwo%
set /a currentNumber= %addTwo%
)else ( // if not both of the above then do this. NOW this section below doesn't work
set /a bottomlimit= 1
set /a upperlimit= 2
set /a limit=%upperlimit%-%bottomlimit%+1
set /a randomChoice= %bottomlimit% + %RANDOM% %% %limit%
set /a currentNumber= %currentNumber%+%randomChoice%
echo Computer chose: %currentNumber%
)
)
endLocal & set /a currentNumber= %currentNumber%
goto :eof
If I take the last else section to outside like this below, then it works:
:computerMakePick
setLocal
set /a currentNumber= 15
set /a addOne=%currentNumber%+1
set /a addTwo=%currentNumber%+2
//the next segment implements OR operator for two conditions using nested if-else statement
if %addOne% == 7 ( //checking first condition.
echo Computer chose: %addOne%
set /a currentNumber= %addOne%
)else (
if %addTwo% == 8 ( // now checking: OR second condition
echo Computer chose: %addTwo%
set /a currentNumber= %addTwo%
)else (
echo. // need to put something in here or else it doesn't work.
) // could also delete this last else-statment but it doesn't matter
)
//now this below works fine. and I don't understand why under second-else section it doesn't
set /a bottomlimit= 1
set /a upperlimit= 2
set /a limit=%upperlimit%-%bottomlimit%+1
set /a randomChoice= %bottomlimit% + %RANDOM% %% %limit%
set /a currentNumber= %currentNumber%+%randomChoice%
echo Computer chose: %currentNumber%
endLocal & set /a currentNumber= %currentNumber%
goto :eof
By saying it's not working I mean if I print the values of each variable: bottomlimit, upperlimit, limit, etc. when they are defined inside the second else statement, for example for the command line echo value of limit is = %limit% I get blanks (nothing).
Why is this happening and how can I fix it to work inside the second else statement?
Use the following code:
#echo off
:computerMakePick
setLocal
set "currentNumber=15"
set /a addOne=currentNumber + 1
set /a addTwo=currentNumber + 2
rem // the next segment implements OR operator for two conditions using nested if-else statement
if %addOne% == 7 ( rem // checking first condition.
echo Computer chose: %addOne%
set "currentNumber=%addOne%"
) else if %addTwo% == 8 ( rem // now checking: OR second condition
echo Computer chose: %addTwo%
set "currentNumber=%addTwo%"
) else ( rem // if not both of the above then do this. NOW this section below doesn't work
set "bottomlimit=1"
set "upperlimit=2"
set /a limit=upperlimit - bottomlimit + 1
set /a randomChoice=bottomlimit + %RANDOM% %% limit
set /a currentNumber+=randomChoice
setlocal EnableDelayedExpansion
echo Computer chose: !currentNumber!
endlocal
)
endLocal & set "currentNumber=%currentNumber%"
goto :EOF
Environment variables are always of type string. So even on using integers, the numbers are stored in memory as strings and not as integers. Therefore don't use set /a variable=number if there is no real reason to do so as this results in converting number from string to integer for the arithmetic expression, and converting it back from integer to string for assigning the result of this arithmetic expression to environment variable.
Usage of environment variable expansion within an arithmetic expression which is the string after set /a is usually nonsense as Windows command interpreter automatically interprets each string not being a number or an operator as name of an environment variable whose current value has to be converted to an integer for evaluation of the expression.
Yes, whether immediate nor delayed expansion is needed within an arithmetic expression even when the set /a command line is within a command block.
And in batch files // is not a comment, use command REM and take into account that Windows command interpreter first parses the lines with command REM and then executes the command if there is no syntax error, see %~ in REM statement.
For more details:
Run in a command prompt window set /? and really read carefully everything of output help.
If you use any of the logical or modulus operators, you will need to
enclose the expression string in quotes. Any non-numeric strings in the
expression are treated as environment variable names whose values are
converted to numbers before using them. If an environment variable name
is specified but is not defined in the current environment, then a value
of zero is used. This allows you to do arithmetic with environment
variable values without having to type all those % signs to get their
values.
Read the answer Why is no string output with 'echo %var%' after using 'set var = text' on command line?
Read the answer on IF ELSE syntax error within batch file?
Variables within the else-statement are not expanded. Use setlocal enabledelayedexpansion instead and denote your variables with exclamation marks:
:computerMakePick
setLocal enabledelayedexpansion
set /a currentNumber= 15
set /a addOne=%currentNumber%+1
set /a addTwo=%currentNumber%+2
if %addOne% == 7 (
echo Computer chose: %addOne%
set /a currentNumber= %addOne%
)else (
if %addTwo% == 8 (
echo Computer chose: %addTwo%
set /a currentNumber= %addTwo%
)else (
set /a bottomlimit= 1
set /a upperlimit= 2
set /a limit=!upperlimit!-!bottomlimit!+1
set /a randomChoice= !bottomlimit! + !RANDOM! %% !limit!
set /a currentNumber= !currentNumber!+!randomChoice!
echo Computer chose: !currentNumber!
)
)
endLocal & set /a currentNumber= %currentNumber%
goto :eof

How to increase alphabetic variable - Batch

I was able to increase the number variable:
SET /a Y=0
SET /a Y+=1
ECHO %Y% = 1
But I want to "increase" the variable with letters.
Ex:
SET Y=A
SET Y+=1
ECHO %Y% = B
Anyway to do something like this in Batch?
Only with a sort of fake pointer.
#Echo off
Setlocal EnableDelayedExpansion
Set "Letters=ABCDEFGHIJKLMNOPQRSTUVWXYZ"
Set I=25
SET Y=!Letters:~%I%,1!
Echo Y=%Y%
SET /A I+=1,I=I %% 26
SET Y=!Letters:~%I%,1!
Echo Y=%Y%
To have the algorythm continue with A again after reaching Z, you've to calculate the modulus 26
Y=Z
Y=A

What's wrong with this "Guess my number" batch game?

I wrote this game. it simply sets a random number between 1 and 16 and the user must guess it. when the guess was correct, it shows the attempt count to the user.
But when I run it and enter a number, it displays an error and the close immediately. I think the error says missing something.
#echo off
color 0A
set /a key = %random% / 2048
set /a attempts=0
title Guess My Number (0 to 16)
:AGAIN
set /p in = Guess it.
set /a in = %in%
if %key% GTR %in% (
echo My Number is greater.
set /a attempts=%attempts%+1
goto again
)
if %key% LSS %in% (
echo My Number is less.
set /a attempts=%attempts%+1
goto again
)
if %key% == %in% (
echo right!
echo You Guessed it in %attempts% attempts.
goto end
)
:end
pause
As npocmaka wrote, the error is that you have spacec in your SET statements. Removing them results in the following code which works perfectly:
#echo off
color 0A
set /a key =%random% / 2048
set /a attempts=0
title Guess My Number (0 to 16)
:AGAIN
set /p in=Guess it.
set /a in=%in%
if %key% GTR %in% (
echo My Number is greater.
set /a attempts=%attempts%+1
goto again
)
if %key% LSS %in% (
echo My Number is less.
set /a attempts=%attempts%+1
goto again
)
if %key%==%in% (
echo right!
echo You Guessed it in %attempts% attempts.
goto end
)
:end
pause
EDIT: And by the way, your code is nonsense. The answer is always 2. :D I guess you need something like set /a key=%random%%%16
Here's how I'd do your little game
#echo off
color 0A
title Guess My Number (0 to 16)
:Start
set /a key=%random%%%16
set attempts=0
:again
set /p in=What is your guess? (0-16):
set /a attempts+=1
if %key% GTR %in% (
echo My Number is greater.
goto again
)
if %key% LSS %in% (
echo My Number is less.
goto again
)
if %key% == %in% (
echo CORRECT!
echo You Guessed it in %attempts% attempts.
pause
)
Your issue is already solved, with the extra spaces. But I used a different method to generate a random number up to 16, and I moved your attempts counter up to just below the set /p so it'll always be counting up one. In your current version, you have it set to only go up for incorrect guesses. I think it should count the final guess as well. Plus I shortened the attempts counter slightly for you, just a helpful tip for you in the future.
-Edit- I forgot to mention, you don't need this line at all set /a in=%in% as the variable %in% is already set with this line set /p in=Guess it. there's no need to set it twice.

Basic cmd number loop

I have created a simple program to print numbers 1 to 10 and store them in a text file:
#echo off
SET /A X=1
:START
IF %X% LEQ 10 (
ECHO %X%>>C:\TXT.TXT
SET /A X+=1
GOTO START
)
PAUSE
Output that I am getting is:
ECHO OFF
10
Where have I gone wrong?
You could use a for loop for this (not an if statement):
FOR /L %i IN (1,1,10) do echo %i
(this loops from 1 to 10 in command line)
See also: http://ss64.com/nt/for_l.html
EDIT (as I tried to put the code in my comment -> if you change your code to output in the console, you'll see that your code does work, but in your case the txt only has the last time your echo'd):
#echo off
SET /A X=1
:START
IF %X% LEQ 10 (
ECHO %X%
SET /A X+=1
GOTO START
)
pause

Resources