I'm not able to create 10 files with following batch code - batch-file

Following is the code in which I want to create 10 files if the counter reach 10 but the comparison is not working am I missing something or am I doing something wrong? It creates only one file and prints as following in that one file
10 == 0 set
#echo off
set limit=10
set count=0
:start
set count = %count% + 1
echo %limit% == %count% set > YouAreAnIdiot%random%.txt
if %count%==%limit%
exit 0
else
goto start

two errors in one line: set count = %count% + 1:
a) the space between count and = is part of your variable name. (It would be %count %)
b) to calculate with set, you need the /a parameter:
set /a count=%count% + 1
Surprisingly, set /a doesn't care for the additional space, but get used to the syntax without spaces around the = - this keeps life simple.
set /a doesn't need the percent signs with variables, so set /a count=count+1 also works.
There is a short form to do that:
set /a count+=1
Also your if statement will not work. The complete construct has to be on one (logical) line:
if %count%==%limit% (
exit 0
) else (
goto start
)
(note the spaces around the parantheses - they are critical)

Related

How can I make a random number/letter generator?

Hello guys I am very bad at notepad, I want to make a random code generator.
That Generates codes like this 3K2EU-ZGS5L-P3DNL-YM9JC
I want it to work with a notepad .bat file so I can let it run on my PC
This can be done using %random% values to assign numbers and letters as demonstrated
below. If you need letters and numbers in specific places within the 20 digits,
adjust the Condition used to call getLetters or Getnumber to call according to current
%dig%==number
#ECHO OFF
SETLOCAL enableDelayedExpansion
:open
Set dig=0
:main
Set /a dig=%dig% + 1
CALL :digit%dig%
Call LetorNum
IF %dig%==20 goto result
GOTO main
:LetorNum
Set /a pick=%random% * 2 / 32768 + 1
IF %pick%==1 call :getLetter
IF %pick%==2 call :getNumber
GOTO :EOF
:getLetter
Set /a Letter=%random% * 26 / 32768 + 1
IF %letter%==1 Set disp!dig!=a
REM repeat for each letter value between a and z
IF %letter%==26 Set disp!dig!=z
GOTO :EOF
:getNumber
Set /a number=%random% * 10 / 32768 + 1
IF %number%==1 Set disp!dig!=0
REM repeat for every number value between zero and 9
IF %number%==9 Set disp!dig!=9
GOTO :EOF
:result
ECHO %disp1%%disp2%%disp3%%disp4%%disp5%-%disp6%%disp7%%disp8%%disp9%%disp10%-
%disp11%%disp12%%disp13%%disp14%%disp15%-%disp16%%disp17%%disp18%%disp19%%disp20%
pause >nul
GOTO open

String Left & Mid Manipulation in a For Loop Batch File

I am writing a batch file with a for loop and am having trouble getting the left and mid logic working. I have successfully got this logic working outside of the for loop and have attached the code here.
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ZKR
set /a value = 1000
set /a range = "%value%"
set /a secrange = "%value%"
for /l %%x in (1, 1, 10) do (
echo %range%
set range=%range:~0,1%
set secrange=%secrange:~1,3%
echo !range!
echo !secrange!
set "newstr=!range!.!secrange!"
echo !newstr!
PAUSE
)
The output is shown here.
Split 1000
So the above cmd bat is able to split the string and then combine it with a period mark in the middle of the two split strings.
However, in my for loop I don't achieve this result. I know it may be a syntax issue but I am not sure how to fix it (I have tried a lot of different things already).
#echo off
SETLOCAL ENABLEDELAYEDEXPANSION
REM ZKR
set /a range = "X"
set /a secrange = "X"
for /l %%x in (1, 1, 10000) do (
if %%x geq 1000 (
if %%x lss 10000 (
set "range=%%x"
set "secrange=%%x"
echo !range!
echo !secrange!
set "range=%range%:~0,1%"
echo !range!
echo %range%
set secrange=%secrange:~1,3%
echo !secrange!
echo %secrange%
set "newstr=!range!.!secrange!"
echo !newstr!
PAUSE
)
REM End x greater 1000
)
REM End for loop
)
I use secrange the same way as I did in the original code without the for loop. I tried to manipulate the range portion with many different syntax combos but have not been able to output the left character 1. I only am able to output 0 for both characters.
I attached the output here. Bad Output
I understand there is an error (Echo is Off). Please ignore that it is just based off my echo output type for my personal testing.
The important two outputs are the third and fourth line (whichever one is referring to the correct syntax). Does anyone know how to fix the syntax in order to get the left character of 1000 in the for loop and the remaining 3 zeros of the 1000?

Use text file as a counter

I would like to increase the value read from a text file by one, 1. Then when it reaches a specific value, do something and reset the value in the text file to zero, 0.
Edit
Here's my batch file:
set /p count=<c:\count.txt
set /a count+=1
echo.%count%>c:\count.txt
if %count%==5 (
echo.%count%>c:\count.txt
)
exit
some minor points only:
- changed syntax for writing to file, because echo 2>file doesn't write 2 to the file, but writes Erroroutput from echo(which is empty)
- used geq instead of == (optional, just better logic and safer)
- writing 0 instead of %count% if count is equal/greater 5
set /p count=<c:\count.txt
set /a count+=1
>c:\count.txt echo.%count%
if %count% geq 5 (
>c:\count.txt echo.0
echo reached five. Doing something.
)
exit

Calling an IF statement | Batch

Error Code:
If was unexpected at this time
The code sets the stats for each player then it calls the function to determine the amount of damage based on the stats and other variables
What I want to do is call a function with two IF statements within it
Example:
#echo off
set /a level=5
set /a opponentLevel=5
set /a movePower=50
set moveType=physical
goto player
:damageCalculator
if %moveType%==physical (
pause
set /a damage=(((( 2 * %level% / 5 + 2) * %attackStat% * %movePower% / %opponentDefenceStat%) / 50) + 2)
set /a opponentHealth-=%damage%
) else if %moveType%==special (
set /a damage=(((( 2 * %level% / 5 + 2) * %spAttackStat% * %movePower% / %opponentSpDefenceStat%) / 50) + 2)
set /a opponentHealth-=%damage%
)
goto :eof
:player
set type=fire
set/a health=19
set/a attackStat=10
set/a defenceStat=10
set/a spAttackStat=11
set/a spDefenceStat=10
set/a speedStat=12
:opponent
set /a opponentHealth=18
set /a opponentAttackStat=10
set /a opponentDefenceStat=9
set /a opponentSpAttackStat=8
set /a opponentSpDefenceStat=9
set /a opponentSpeedStat=13
:attack
pause
cls
call :damageCalculator
echo It did %damage% Damage!!
pause>nul
Is this just one of those things that batch can't do?
update per edited question
Your script has a few small issues. Firstly, I'll point out that it's often helpful to rem out #echo off when trying to track down the cause of a problem. Doing so in this case shows that the line causing your error is your first set /a line.
The reason your if statements are failing is because the parentheses within your math are being treated as the end of your code block. As soon as the closing parenthesis of ...5 + 2) is encountered, the batch interpreter treats that as the end of your if statement, and therefore gets confused when there's more stuff on the line. You need to quote your set /a statements to prevent this.
set /a "damage=(((( 2 * level / 5 + 2) * attackStat * movePower / opponentDefenceStat) / 50) + 2)"
See how I did set /a "variable=value" there? You could also escape the closing parentheses with a caret -- e.g. ^), but quoting the "var=value" is a little easier to read I think. The quotation marks keep the contents within the context of the command. They're basically saying, "This is a single token. It's a distinct part of the set command, not the code block as a whole." As a bonus, you can also see that the % signs aren't needed to retrieve variable values within a set /a command. Neat, huh?
You've got another problem. Since you're setting the %damage% variable within the same parenthetical code block as you're retrieving it, %damage% is going to be evaluated too early. You could setlocal enabledelayedexpansion and retrieve it as !damage!, and that would certainly work. But there's a simpler fix. Just put it outside the if statements. You're doing set /a opponentHealth-=damage regardless of whether the move type is physical or special, anyway, right?
:damageCalculator
if "%moveType%"=="physical" (
set /a "damage=(((( 2 * level / 5 + 2) * attackStat * movePower / opponentDefenceStat) / 50) + 2)"
) else if "%moveType%"=="special" (
set /a "damage=(((( 2 * level / 5 + 2) * spAttackStat * movePower / opponentSpDefenceStat) / 50) + 2)"
)
set /a opponentHealth-=damage
goto :eof
But still, you should include setlocal just below #echo off to keep from junking up your environment with variables that have no meaning outside the scope of this script.
Here's another tip. You can combine many statements within a single set /a line.
Before:
set /a level=5
set /a opponentLevel=5
set /a movePower=50
set "moveType=physical"
After:
set /a level=5, opponentLevel=5, movePower=50
set "moveType=physical"
original answer
Change your second if to else if or add a carriage return before it (after the preceding parenthesis). Also, move your goto :EOF to the next line after the closing parenthesis.
The explanation for this is that the cmd interpreter treats a parenthetical code block as a single command. So in essense,
if [true condition] (
action
) second command
is being evaluated as
if [true condition] (action) second command
which results in an error because there's no line break or other separator between the first command (the if) and the second. Here's an example of a valid compound command:
#echo off & setlocal
Compound command need:
unconditional AND (& -- right side always executes after the left)
example: endlocal & goto :EOF
logical AND (&& -- right side executes only if the preceding command exited zero)
example: find /i "text" "textfile.txt" >NUL && echo Text was found
pipe (| -- makes the command on the right do something with the output generated by the command on the left)
example: dir /s /b | more
logical OR (|| -- right side executes only if the preceding command exited non-zero)
example: tasklist | find /i "iexplore.exe" >NUL || echo Internet Explorer not running
... or in the case of an if statement, else.
if condition 1 (
action 1
) else if condition 2 (
action 2
) else (
action 3
)
Or if you want to check that two conditions are true:
if %var% leq 10 if %var% geq 5 (
echo Variable is between 5 and 10.
)
Of course, you don't have to use compound statements. There's no reason why a function can't have multiple if statements that aren't compound.
:fn <str>
if "%~1"=="fish" (echo Bloop.)
if "%~1"=="cats" (echo Meow.)
if "%~1"=="dogs" (echo Grrrr.)
goto :EOF
... is perfectly valid.

Rpg batch level system error?

So i seem to find it difficult to put an 'if' statement inside another 'if' statement. I have the labels for gotos set up correctly the error occurs when the code goes to the levelstats
The code is meant, once the player has met the max exp, to go to the next level. The lvlguage checks to see if the exp requirements are met. That works fine. It goes to levelstats and then the prompt closes.
:LvlGuage
If %CharExp% GEQ %CharMaxExp% (
Goto LevelStats
)
Goto MENU
:LevelStats
::the error starts here
If %CharLevel%==1 (
Goto Level2
)
Goto MENU
If %CharLevel%==2 (
Goto Level3
)
Goto MENU
:: This goes on till level 10
:Level2
If %CharExp% GEQ 100 (
Set /a CharExp=%CharExp% -100
Set CharMaxExp=280
Set CharLevel=2
Set /a CharAttack=(%CharAttack% * 1.5) + 1
Goto LEVELUP
)
Goto MENU
:Level3
If %CharExp% GEQ 280 (
Set /a CharExp=%CharExp% -280
Set CharMaxExp=784
Set CharLevel=3
Set /a CharAttack=(%CharAttack% * 1.5) + 1
Goto LEVELUP
)
Goto MENU
:LEVELUP
Cls
Echo You Have Leveled Up.
Timeout 2 >nul
Goto MENU
Your variables are calculated once just when your script starts. Take a look ath this: http://www.ss64.com/nt/delayedexpansion.html
You need to add Setlocal EnableDelayedExpansion at the beginning at your script and adress to your vars with !varname! instead of %varname% when you want to get the changed values. This will always give you the last set value of the variable instead of the first assigned value.

Resources