I'm trying to write a program that creates a certain amount of files inside a folder. The number of files to create works perfectly but when I add the option to input the size of the files it completely fails.
This is my actual code:
#echo off
set counter = 1
set loopTime = 0
cls
set /p filesNumber=files:
set /p fileSize=file size:
:loop
set /a counter = counter + 1
type nul > ping%counter%
if "%fileSize%" != "0" goto insertFileSize
:continue
set /a loopTime = loopTime + 1
if "%loopTime%" == "%filesNumber%" goto end
goto loop
:insertFileSize
set insertFileSizeLoopTime = 0
:insertFileSizeTempFunc
echo 0 >> ping%counter%
set /a insertFileSizeLoopTime = insertFileSizeLoopTime + 1
if "%insertFileSizeLoopTime%" == "fileSize" goto continue
goto inserFileSizeTempFunc
:end
cls
echo %filesNumber% files created successfully.
pause
When I run it with filesNumber = 10 and fileSize = 10, it creates a file called ping1, which is right, but only one and with 0 bytes.
I was expecting 10 files [ping1, ping2, ..., ping10] and each one with 10 bytes.
Can someone tell me what am I doing wrong?
Thanks.
Related
There's a file called "Settings.txt", this file contains a lot of lines including the following variable line:
X 100
The number in this line is the variable part, "X" is a fixed text.
What I need is to check if that number is (less than 100) OR (equal or greater than 100) then based on the result > goto A or B.
The script could be something like:
IF >> "X 100" in "D:\Settings.txt" GEQ 100 goto A else goto B
:A
#echo the value is equal or greater than 100
pause & goto continue
:B
#echo the value is less than 100
pause
:continue
#the rest of the script
A mechanism to read from the Settings.txt file is needed to get the number.
#ECHO OFF
FOR /F "tokens=1,2" %%A IN ('findstr.exe /B "X [:digit:]+" "Settings.txt"') DO (SET "NUM=%%~B")
ECHO NUM is set to %NUM%
IF %NUM% GEQ 100 (GOTO ALABEL) ELSE (GOTO BLABEL)
:ALABEL
ECHO Do greater than or equal stuff
GOTO TheEnd
:BLABEL
ECHO Do less than stuff
GOTO TheEnd
:TheEnd
EXIT /B 0
This question already has an answer here:
Variables are not behaving as expected
(1 answer)
Closed 1 year ago.
I wrote the following batch script, but keep getting a missing operator error
set /a control=1
goto :while
:while
if "!control!"=="1" (
set /p FirstNumber=Please enter your first number:
set /p SecondNumber=Please enter your Second number:
set /a result=%FirstNumber%/%SecondNumber%
echo %FirstNumber% divided by %SecondNumber% = %result%
echo %FirstNumber% divided by %SecondNumber% = %result% >> results.txt
goto :while
)
pause
When I run this code, I keep getting a missing operand error.
This is my output:
Please enter your first number 1: 10
Please enter your second number 1: 2
Missing operand.
divided by =
setlocal enabledelayedexpansion when setting and using a variabled inside a code block:
#echo off
setlocal enabledelayedexpansion
set /a control=1
:while
if !control! equ 1 (
set /p FirstNumber=Please enter your first number:
set /p SecondNumber=Please enter your Second number:
set /a result=FirstNumber / SecondNumber
echo !FirstNumber! divided by !SecondNumber! = !result!
echo !FirstNumber! divided by !SecondNumber! = !result!
goto :while
)
and without needing the parenthesized code block and obviously no need for delayedexpansion
#echo off
set /a control=1
:while
if not %control% equ 1 goto :eof
set /p FirstNumber=Please enter your first number:
set /p SecondNumber=Please enter your Second number:
set /a result=FirstNumber / SecondNumber
echo %FirstNumber% divided by %SecondNumber% = %result%
echo %FirstNumber% divided by %SecondNumber% = %result%
goto :while
choice /c 123456789 >NUL
if errorlevel = 9 if %ui9%==9 set "ui9=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 8 if %ui8%==8 set "ui8=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 7 if %ui7%==7 set "ui7=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 6 if %ui6%==6 set "ui6=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 5 if %ui5%==5 set "ui5=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 4 if %ui4%==4 set "ui4=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 3 if %ui3%==3 set "ui3=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 2 if %ui2%==2 set "ui2=X" & set /a moves = %moves% + 1 & goto :medmodulebot
if errorlevel = 1 if %ui1%==1 set "ui1=X" & set /a moves = %moves% + 1 & goto :medmodulebot
cls
echo That spot's already taken
ping localhost -n 3 >NUL
goto medmoduleuser
I'm making a tic-tac-toe bot. When entering a digit such as 9, then entering it again in the next turn, it will trigger the if errorlevel = 8 if %ui8%==8 set "ui8=X" & set /a moves = %moves% + 1 & goto :medmodulebot instead of just going through the code until it hits the bottom... It seems that choice is returning errorlevels oddly? What do I do to prevent this?
In addition to the comments, errorlevel testing could be avoided completely. By converting the choice value into the literal value with a for / F loop metavariable
#echo off & Setlocal ENABLEdelayedexpansion
Rem // script body
For /F "Delims=" %%C in ('Choice /N /C:123456789') do (
if !ui%%C!==%%C (
set "ui%%C=X"
set /a moves+=1
goto :medmodulebot
)
)
cls
echo That spot's already taken
ping localhost -n 3 >NUL
goto medmoduleuser
I have a Batch Script which when I run wont display a string variable.
#ECHO OFF
color 0c
set /a Copyed = 1
set /a File = %random%
set /a Origin = %~z1
set /a End = 0
set /a OSize = %~z1
if %~z1 LEQ 999 (
set /a Size = %~z1
set Z="Bytes"
)
if %OSize% GEQ 1000 (
set /a OSize /= 1000
set /a OEnd += 1
)
if %OSize% GEQ 1000 (
set /a OSize /= 1000
set /a OEnd += 1
)
if %OSize% GEQ 1000 (
set /a OSize /= 1000
set /a OEnd += 1
)
if %OSize% GEQ 1000 (
set /a OSize /= 1000
set /a OEnd += 1
)
if OEnd == 1 (set OZ="KB")
if OEnd == 2 (set OZ="MB")
if OEnd == 3 (set OZ="GB")
if OEnd == 4 (set OZ="TB")
echo ::::::::::::::::::::::: > %File%
echo :::::::::::::::::::::::
echo Original File "%1" Has A Size Of %OSize% %OZ% >> %File%
echo Original File "%1" Has A Size Of %OSize% %OZ%
pause
When I run this script using another file I get :
:::::::::::::::::::::::
Original File ""P:\Virus Testing\Fill.txt"" Has A Size Of 536
Press any key to continue . . .
In the file it creates I get this :
:::::::::::::::::::::::
Original File ""P:\Virus Testing\Fill.txt"" Has A Size Of 536
As you can see neither of them display the varible %OZ%. How do I fix this? Thanks In Advance
Try
if %OEnd% == 1 (set OZ="KB")
if %OEnd% == 2 (set OZ="MB")
if %OEnd% == 3 (set OZ="GB")
if %OEnd% == 4 (set OZ="TB")
A few bugs in your script:
You need to initialize OEnd, not End
set /a OEnd = 0
You need a descriptor for OZ for bytes, something like
if %OEnd% == 0 (set OZ=Bytes)
And, each of the other checks needs to check the value of OEnd, not name itself:
if %OEnd% == 1 (set OZ=KB)
if %OEnd% == 2 (set OZ=MB)
if %OEnd% == 3 (set OZ=GB)
if %OEnd% == 4 (set OZ=TB)
I need a batch file that writes the count number to a txt file.
Next time the batch file is run, it should read the current count number from the txt file and add 1 to count and save this new value in the txt file. (nothing else is in the txt file)
When count is >5 it should start from 1 again
Example:
Count.bat runs 1 time:
count.txt has no count so Count.bat saves the value 1 in count.txt
Count.bat is run 2 time:
Count.bat reads 1 from count.txt and saves the new value 2 to count.txt
When count.bat is run for the 6 time it should start over by saving the value 1 in count.txt
I think this just be easy to do, but I'am not use to batch commands
So hopefully someone here could help me.
I know this isn't exactly what you asked for, but you may want to create separate files:
#echo off
if not exists count.1 goto l1
if not exists count.2 goto l2
if not exists count.3 goto l3
if not exists count.4 goto l4
if not exists count.5 goto l5
del count.*
rem -- fall trhough -- and create first count-file
:l1
echo . > count.1
goto end
:l2
echo . > count.2
goto end
:l3
echo . > count.3
goto end
:l4
echo . > count.4
goto end
:l5
echo . > count.5
rem -- fall through -- goto end
:end
Start with the file temp.txt with one line containing just a one (1) without parenthesis.
for /f "eol=# tokens=* delims=," %%i in (temp.txt) do (
set /A Count = %%i + 1
If '%Count%' == '6' (set /A Count = 1)
)
echo %Count% > temp.txt
easier with modulo:
#echo off & setlocal
(<count.txt set /p count=) 2>nul
set /a count=count %% 5 + 1
>count.txt echo %count%
type count.txt
if not exist count.txt (
echo 1 > count.txt
exit
)
< count.txt set /p count=
if %count% equ 6 (
echo 1 > count.txt
exit
)
set /a count=count+1
echo %count% > count.txt
To be honest; I only wrote this answer because this question is 6 years old.