I'm a programmer in batch and I've just joined this site to ask 1 question...
I made this program that writes random numbers from 1 to 10 and encodes/writes them into a file... The problem is, ANOTHER Batch file must read the numbers and check if it's below 5.
The batch file that generates random numbers is not inputting a random number into the file;
Instead, the file says
ECHO is off
Which confuses the other batch file, and therefore it crashes.
Here's the code of both batch files;
The "Writer" of the numbers
#echo off >nul
title Batch Arithmetic Communicator
:check
timeout /t 1 >nul /nobreak
echo %t% >>wait.rsm
SET /A t=%RANDOM% * 10 / 32768 + 1
goto cont
:cont
timeout /t 1 >nul
if exist pack.rsm goto cont2
if NOT exist pack.rsm goto cont
:cont2
set /p data=<pack.rsm
del pack.rsm
if %data% LSS 5 goto move
if %data% GTR 5 goto check
The receiver of the numbers
#echo off
:a
timeout /t 1 >nul
if exist wait.rsm goto b
if NOT exist wait.rsm goto a
:b
set /p d=<wait.rsm
if %d% GTR 5 goto a
if %d% LSS 5 goto w
:w
echo 3>>pack.rsm
goto a
Help?
Your writer is echoing %t% before it's setting it:
echo %t% >>wait.rsm
SET /A t=%RANDOM% * 10 / 32768 + 1
At the time of that first statement, %t% is set to nothing so it's as if you said:
echo >>wait.rsm
which is why you're getting ECHO is off, what you see with a "naked" echo command.
Basically, you need to set %t before trying to echo it so swap those two statements around:
SET /A t=%RANDOM% * 10 / 32768 + 1
echo %t% >>wait.rsm
Related
This must be very basic but i can't seem to find a way to get this done
my batch script goes like this:
#echo off
echo Type In Desired Volume And Press Enter
echo.
echo 0 = 0 %%
echo 1 = 100 %%
echo 0.10 = 10 %%
echo 0.65 = 65 %%
echo.
set /p input=
echo %input% > "C:\SetVol\Source\Volume.txt
I want to make it more user friendly by letting the user input a number between 0 and 100 instead of, for example, 0.10 for 10% audio volume. But i still need to output the 0.10 to a textfile if the user enters 10.
Google appears to longer be my best friend and we cant seem to communicate on this.
If anybody could help me get starter that would be great.
A simple way is to convert the input number to the requested output format.
One step is to prefix the input with 0.
set /p input=[Enter volume in %%]:
set "output=0.%input%"
echo %input% > "C:\SetVol\Source\Volume.txt
But this would fails for one digit values like 2%, becomes 0.2 instead of 0.02.
This can be fixed with prefix each number with 00 and take the last three digits and add a dot between.
set /p input=[Enter volume in %%]:
set "temp=00%input%"
set "output=%temp:~-3,1%.%temp:~-2%"
echo %output% > "C:\SetVol\Source\Volume.txt
You can use choice in a loop to make a key based 'slider', and then modifiy the variable value to include a 0. prefix or be 1 using an if condition:
#Echo off
set "volume=50"
:Volume
cls
Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
If not "%%G"=="C" Goto :Volume
)
IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
Set "Volume=0.0%Volume%"
) Else Set "Volume=0.%Volume%"
:#your script here
For NTFS systems, a variant that stores the last set volume in an alternate data stream and reasigns the last value on return:
#Echo off
set "volume=50"
For /f "Usebackq delims=" %%G in ("%~f0:Volume")Do Set "%%G"
:Volume
cls
Echo( Current volume: %Volume%%% [I]ncrease [D]ecrease [C]ontinue
For /f "delims=" %%G in ('Choice /N /C:IDC')Do (
If "%%G"=="I" If not %Volume% GEQ 100 Set /A Volume+=1
If "%%G"=="D" If not %Volume% LEQ 0 Set /A Volume-=1
If not "%%G"=="C" Goto :Volume
)
Set Volume >"%~f0:Volume"
IF %volume% Equ 100 ( Set "Volume=1" )Else If %volume% LSS 10 (
Set "Volume=0.0%Volume%"
) Else Set "Volume=0.%Volume%"
:#your script here
Note: by using this method of input, invalid input cannot be entered.
Figured out a way to use the choice input, if anyone has a more neat or different way to do this please let me know... 291 more lines to edit...
#echo off
echo Choose Audio Volume 0-100 %%
:choice
set /P c=
if /I "%c%" EQU "0" goto :0
if /I "%c%" EQU "1" goto :1
if /I "%c%" EQU "2" goto :2
goto :choice
:0
echo 0 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:1
echo 0.01 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
:2
echo 0.02 > C:\users\%username%\desktop\numbertest.txt
echo You chose %c% %%
pause
exit
Having an issue with the following issue with a batch file...
Problem: I have created a batch script to check if files are present in a folder location and if there are files present it will write the PC name (labeled asset) to a log file. However, after scanning 99 PCs it hangs for a about 20 seconds and then scans a few more and hangs. I need to figure out a way to have this run smoothly.
Performance shows that the processor does spike. I've tried calling it from another batch file with a low priority with the same results.
All PCs are named "ABC" followed by 4 numbers.
Full code is below because I do not know where the focus should be. If there is a more appropriate edit I would be happy to oblige.
cls
#echo off
echo Set range of asset tags to scan:
:min
echo:
set /p min="Enter lowest asset number to scan (number only): "
if not defined min goto :min
if %min% lss 1000 (
msg * Asset must be 4 digits
goto :min
)
if %min% gtr 5000 (
msg * Min asset out of range
goto :min
)
:max
echo:
set /p max="Enter highest asset number to scan (number only): "
if not defined max goto :max
if %max% gtr 5000 (
msg * Max asset out of range
goto :max
)
if %max% lss %min% (
msg * Max cannot be lower than min
goto :max
)
set /a max=%max%+1
#echo off
REM Count Logic
set count=%min%
REM sets date/time log stamp to consistent value. If done per line, the seconds change will create multiple files.
set name=Scan_Results_%DATE:~-4%%DATE:~4,2%%DATE:~7,2%%TIME:~0,2%%TIME:~3,2%%TIME:~6 ,2%%TIME:~10,2%
REM creates a date and time stamped log file from location batch file is run
echo Dictation Scan from %Date% %Time% > %name%.txt
#echo off
:loop
#echo off
set /a count=%count%
echo Asset tag being scanned: %count%
#echo off
REM "ABC" to count as string to inject in filepath
set asset=abc%count%
ping -n 1 %asset%.domain.com > NUL
IF ERRORLEVEL 0 (goto :scan) ELSE goto :No
:scan
REM IF LOGIC (If files are detected DO X or else Y [loop until end])
#echo off
for /F %%i in ('dir /b "\\%asset%.domain.com\C$\Program Files\Speech Machines\SubSpace\temp\*.*"') do (
goto :Yes
)
goto :No
:Yes
echo writing asset to log file
echo %asset% >> %name%.txt
set /a count=%count%+1
goto :iteration
:No
set /a count=%count%+1
goto :iteration
:iteration
REM This is the highest asset tag number that you want to scan
if not #%count%#==#%max%# goto loop
goto final
:final
echo %found% >> %name%.txt
#echo SCANNING COMPLETE.
Pause
exit
The logic in
ping -n 1 %asset%.domain.com > NUL
IF ERRORLEVEL 0 (goto :scan) ELSE goto :No
fails to detect offline machines. The if errorlevel n construct will be evaluated as true for any errorlevel value equal or greater than n, so, if errorlevel 0 will be true for any non negative errorlevel.
You can try with any of
ping -n 1 %asset%.domain.com > NUL
if errorlevel 1 ( goto :no ) else ( goto :scan )
ping -n 1 %asset%.domain.com > NUL
if not errorlevel 1 ( goto :scan ) else ( goto :no )
ping -n 1 %asset%.domain.com > NUL && goto :scan || goto :no
ping -n 1 %asset%.domain.com > NUL
if %errorlevel% equ 0 ( goto :scan ) else ( goto :no )
BUT, depending on the ip version and subnet of source and target machines, testing the ping errorlevel is not a secure way to test if the target machine is online (read here)
I have been having troubles with batch-codes that I would expect to work, but don't...
Below is what I have written...
#echo off
cls
:loop
set /p "input=Input a number: "
set /a "number=%input%" 2>nul
REM check if input valid
if "%input%" NEQ "%number%" (
cls
Echo Please Enter a valid number! &Echo.&Echo.
goto :loop
)
Set /a Even=number%%2
if %Even% EQU 0 (
Echo Substituting Even Number in: x / 2
Echo set /p"=(%number%) / 2 = "
set /a answer=number/2
) Else (
Echo Substituting Odd Number in: 3x - 1
<nul set /p"=3(%number%)-1 = "
set /a answer=number*3
set /a answer=answer-1
)
Echo %answer%
Echo.
Echo.
goto :loop
Echo Unexpected Error . . .
pause
Exit
Whenever I input a number into the console, it does the math, like I want it to, but prints the number -1, and every time i input another number, the number goes to -2, -3, -4, so on.
Put a setlocal enableextensions at the beginning after the #echo off, e.g.
#echo off
setlocal enableextensions
cls
Also, I think you would also need to use delayed variable expansion (usually denoted by !var!), which would change your script above to something like this:
#echo off
setlocal enableextensions enabledelayedexpansion
cls
:loop
set /p "input=Input a number: "
set /a number=!input! 2>nul
REM check if input valid
if "!input!" NEQ "!number!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
REM Make sure that it is an integer put in (just in case)
set /a int=!number! %% 1
if "!input!" NEQ "!int!" (
cls
Echo Please Enter a valid number!
Echo.
Echo.
goto :loop
)
Set /a Even=!number! %% 2
if !Even! EQU 0 (
Echo Substituting Even Number in: x / 2
set /a answer=!number! / 2
) Else (
Echo Substituting Odd Number in: 3x - 1
set /a answer=!number! * 3 - 1
)
Echo !answer!
Echo.
Echo.
goto :loop
I also would like to point out that I also fixed a few other bugs (set /p isn't of any use in this script at all, especially in where it is used, and also you need the modulus to find even/odd).
It's pretty self-explanatory, what I'm trying to do here.
When I do something like this:
set n=0
:choose
set /a n+=1
choice /c yn
if errorlevel 2 ( echo NO! >>log.txt
) else echo YES! >>log.txt
if %n% lss 10 goto choose
The loop executes produces a new line for each time the command is executed (10 times=10 lines)
I want it to write all 10 outputs to one line in the log.
set n=0
:choose
set /a n+=1
choice /c yn
if errorlevel 2 ( echo|set /p=NO! >>log.txt
) else echo|set /p=YES! >>log.txt
if %n% lss 10 goto choose
I would like to ask you a question.
I am creating a simple program that scans numbers (people's grades) and checks if they are at a certain range (say, 75% to 90%) and if they are at a certain range, do the following command; here's the code.
(More text below the code)
#echo off
color a
title Happy Factor Decoder
echo Hello!
set /p eg="Exam Grade (RAW): "
set /p teg="TOTAL RAW Exam Grade (The highest score): "
echo Calculating
set /a m=%teg% - %eg%
echo You had %m% mistakes
echo Breaking down...
timeout /t 1 >nul
set /a bdf1=%eg% / 4
echo %bdf1%
set /a bdf2=%teg% / 4
echo %bdf2%
set /a bdf3=%m% / 4
echo %bdf3%
echo I BROKE IT DOWN YEAH :D
if %eg% == 4 goto happy
if %eg% == 3 goto kindahappy
if %eg% == 2 goto kindasad
if %eg% == 1 goto sad
:happy
echo Your father will be happy about this
pause
:kindahappy
echo Your father will be KINDA happy about this
pause
:kindasad
echo Your father will be KINDA sad about this
pause
:sad
echo Your father will be sad about this
pause
You see, What i want it to do is this (in pseudocode)
IF BDF1 IS AT CERTAIN RANGE (80-90) GOTO HAPPY
Any ideas?
One number between minand max is just number>=max AND number<=min, right???
IF %BDF1% GEQ 80 IF %BFD1% LEQ 90 GOTO :HAPPY
I don't know the units of your calculations, but you can check a range from lowest to high:
#ECHO OFF &SETLOCAL
REM some calculation here
IF %BDF1% leq 25 GOTO :sad
IF %BDF1% leq 50 GOTO :kindasad
IF %BDF1% leq 75 GOTO :kindahappy
IF %BDF1% leq 100 GOTO :happy
ECHO uups!
goto:eof
:sad
ECHO sad
goto:eof
:kindasad
ECHO kindased
goto:eof
:kindahappy
ECHO kindahappy
goto:eof
:happy
REM please enter your code here :)