Regenerating values %random% after starting new process - batch-file

I am testing the following batch file as seen below. I always echo the value of num and number at the start of the bat just to check if these variable are empty. Which they are. On first run, the batch file gets to echo Random number = %number% and a valid number is shown. If the batch is then properly terminated by giving N as the choice, the batch closes. During the second start of the bat, a valid random number is chosen again as expect.
Unexpectedly, what occurs when a random number is chosen, and the batch file is ended by clicking the X or terminating the program with alt+f4. When starting the bat a second time, the random number chosen Most of the time be the same number that was chosen on the first run of the batch file. Even after set "num=" and set "number=" when echo Random number = %number% is ran the chosen number will be the same as the number chosen in the first ran bat.
I don't imagine this was explained very well, so perhaps the best way to see what is happening is to create and run the bat yourself. Chose a few random numbers during your first run through. Then force close the batch file and do not properly terminate the batch file. When you run the batch file again, the first chosen random number of run 2 will be the same random number last generated on run 1. Clearing the variable at the beginning of the bat file does not seem to resolve this problem. The question then is. How do I ensure that a new random number is always generated regardless of how the batch file is terminated? Is set /a num=%random% * 10 / 32768 + 1 not the best way to set a random number?
#echo off
:start
set "num="
set "number="
echo old num=%num% and old number=%number%
pause
goto RAND
:RAND
cls
set /a num=%random% * 10 / 32768 + 1
set number=%num%
goto :%num%
:%num%
set "num="
echo Random number = %number%
echo.
CHOICE /M "Do you want another random number?"
IF ERRORLEVEL 2 GOTO exit
IF ERRORLEVEL 1 GOTO start
:exit
exit

Related

Batch File - How to copy text from file, and then update it to a new value

Each time I open the batch file, I would like it to read the information currently stored in the text file, and then apply that stored information it pulled to calculating a new integer.
I'm trying to figure out how to get a number copied from a text file, stored as a variable, and then updated to a new integer in that text file, say adding 1 to the value.
I've been sifting through information online, and everything seems to point in a different direction.
Here is a test code I've gotten from digging thus-far:
set file="Test.txt"
set /a _Counter =< %file%
echo:%_Counter%
set /a "_Update=%_Counter%+1"
echo:%_Update% >%file%
timeout /t 10
For some reason when I try to get the information for the counter, it doesn't pull any data from the text file, and I'm left with this line output by the batch file.
F:\Users\Test\Documents\JumbledDirectory> set /a _Counter = Directory\Test.txt 0<F:\Users\Test\Documents\Jumbled
The most common answer I've seen is to use something along the lines of
set /p _Counter=< Test.txt
echo %_Counter%
As seen here: Windows batch command(s) to read first line from text file
But upon doing this I've either ended up with
echo:%_Counter%
being completely blank, or it defaults to 0 each time.
Any help would be appreciated as I've sadly been trying to find how to get this simple function for around 6 hours now.
#ECHO Off
SETLOCAL
set "file=q72474185.txt"
set /p _Counter=< "%file%"
echo:%_Counter%
set /a _Update=_Counter+1
echo:%_Update% >"%file%"
TYPE "%file%"
GOTO :EOF
When you use the point-click-and-giggle method of executing a batch, the batch window will close if a syntax-error is found or the script runs to completion. You can put a pause after statements and home in on the error, but better to open a 'command prompt' and run your batch from there so that the window remains open and any (error) messages will be displayed.
The error message would be about missing operand.
I've changed the filename as I track each question I respond to with its own set of data.
Use set "var=value" for setting string values - this avoids problems caused by trailing spaces. Don't assign a terminal \, Space or " - build pathnames from the elements - counterintuitively, it is likely to make the process easier.
set /a does not expect input from anywhere, it simply performs the calculation and assigns the result. Quotes are not necessary so I've removed them. % are also not required in a set /a but can be required if you are using delayedexpansion.
set /p expects input, so I've used that to read the file. Note that set /a disregards spaces, but set and set /p will include the space before the = in the variablename assigned, and _Counter & _Counter are different variables.
So having the batch in the same directory as the textfile this will work:
REM get input of file as Counter
set /p Counter=<number.txt
REM add a number to Counter and assign it as Counter
set /a "Counter=%Counter%+3
REM empty the file
break>number.txt
REM write Counter in the file
echo %Counter% >> number.txt

How do I take the value of one batch file to another?

Key Generator
#ECHO OFF
COLOR A
ECHO Generating Key!
choice /d y /t 3 > nul
set /p "genkey"="%random%-%random%-%random%-%random%"
PAUSE
EXIT
Batch 2
COLOR A
#ECHO OFF
set /p base=
if %base% == %genkey% GOTO :ecs
:ecs
PAUSE
EXIT
The way I normally do this is by writing to a file and using SET to recall from the file.
For example:
BATCH FILE 1
echo off
set var1=%Random%-%Random%-%Random%
echo %var1%>temp.log
pause
exit
BATCH FILE 2
echo off
set Var1=nul
if EXIST Temp.log (set /p Var1=<Temp.log && del /Q Temp.log)
echo %Var1%
pause
exit
In this case, if you run the second batch file without running the first one, the output will be "nul". However, if you ran the first batch file before the seccond, the output of the first will be displayed.
You can change %Random%-%Random%-%Random% to whatever text or variable you want.
The program acts like the type function, however with this method it prints the contents of the file to a variable.
One last thing to note is that this method will ONLY read the first line of the file. This is useful where you are transfering numbers, then using that number in an operation. If you want to transfer the whole file, you can use a FOR state ment, but also note, the FOR statement will recall the entire into a singe line.

Display a random number, ask the user to input the same number, and continue if the numbers match

I want to show a random number to the user. Then, I want the user to be able to type in the same number. If the two numbers match, the program should continue. If the numbers do not match, a new random number should be generated and the process should start over.
Here is what I have so far:
color 0
cls
echo.
echo type the Number u See %random%
echo.
set Nh=
set /p Nh=Number Here:
if %Nh%==%random% Goto lol
The biggest problem with your existing code is that every time the %random% variable is used, it becomes a different random number, so the random number that is displayed in your code is not the random number that is used for checking the user's input. This means that there is only a 1/32768 chance that they will be able to progress correctly.
You should set the initial random number to a different variable so that the value will not change.
:show_number
color 0
cls
echo.
set rand_num=%random%
echo The number you see: %rand_num%
echo.
set /p "nh=Number here: "
if "%nh%" equ "%rand_num%" goto lol
goto show_number
Just use an else statement, and create a checkpoint called :random.
:random
color 0
cls
echo.
echo type the Number u See %random%
echo.
set /p Nh=Number Here:
if %Nh%==%random% (
Goto lol
) else (
Goto random
)

Batch Time problems when used in echo

I have a problem with my batch program, I want it to show what time / t does in a echo line but when I do this
#Echo off
Set echotime=time /t
echo (%date%)(%echotime%)
Only need XX:XX cause XX:XX:XX,XX Makes it too long for the log file i wanna have for my program
echo (%date%)(%time:~0,5%)
the :~0,5construct takes five chars beginning with char 0 (the first one)

Batch Script Programming -- How to allow a user to select a file by number from a list of files in a folder?

I have a folder with N files in it. I'm trying to figure out how to do the following:
Display a list of the files with numbers next to them for selection:
01 - FileA.pdf
02 - FileB.pdf
03 - FileC.pdf
...
Then, have the user select which file he wants to use by typing in the corresponding number. I have no idea where to even begin with this one.
The following batch script should do what you want, the explanation follows below:
#ECHO OFF
SET index=1
SETLOCAL ENABLEDELAYEDEXPANSION
FOR %%f IN (*.*) DO (
SET file!index!=%%f
ECHO !index! - %%f
SET /A index=!index!+1
)
SETLOCAL DISABLEDELAYEDEXPANSION
SET /P selection="select file by number:"
SET file%selection% >nul 2>&1
IF ERRORLEVEL 1 (
ECHO invalid number selected
EXIT /B 1
)
CALL :RESOLVE %%file%selection%%%
ECHO selected file name: %file_name%
GOTO :EOF
:RESOLVE
SET file_name=%1
GOTO :EOF
First of all this script uses something like an array to store the file names. This array is filled in the FOR-loop. The loop body is executed once for each file name found in the current directory.
The array actually consists of a set of variables all starting with file and with a number appended (like file1, file2. The number is stored in the variable index and is incremented in each loop iteration. In the loop body that number and the corresponding file name are also printed out
In the next part the SET /P command asks the user to enter a number which is then stored in the variable selection. The second SET command and the following IF are used to check if the entered number will give a valid array index by checking for a fileX variable.
Finally the RESOLVE subroutine is used to copy the content of the variable formed by file + the entered number in selection to a variable named file_name that can then be used for further processing.
Hope that gives some hints.

Resources