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)
Related
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
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.
i was toying around with cmd a bit and wanted to write a little application which involves a simple feature to read a counter from a txt file, then work with it and at the end raise the counter by one.
set /p x=<file.txt
...
set /a y=x+1
echo %y%>file.txt
Problem is it always returns "ECHO ist eingeschaltet (ON)." which translates to ECHO is turned on (ON) for some reason. Could somebody please explain where it comes from and how to fix it? I dont need anything fancy. I just want it to work and know where my mistake is.
At first, I want to show you how your echo command line should look like:
> "file.txt" echo(%y%
Here is your original line of code again:
echo %y%>file.txt
The reason for the unexpected output ECHO is on./ECHO is off. is because the echo command does not receive anything to echo (type echo /? and read the help text to learn what on/off means). Supposing y carries the value 2, the line expands to:
echo 2>file.txt
The number 2 here is not taken to be echoed here, it is consumed by the redirection instead; according to the article Redirection, 2> constitutes a redirection operator, telling to redirect the stream with the handle 2 (STDERR) to the given file. Such a handle can reach from 0 to 9.
There are some options to overcome that problem:
inserting a SPACE in between the echoed text and the redirection operator:
echo %y% >file.txt
the disadvantage is that the SPACE becomes part of the echoed text;
placing parentheses around the echo command:
(echo %y%)>file.txt
placing the redirection part at the beginning of the command line:
>file.txt echo %y%
I prefer the last option as this is the most general and secure solution. In addition, there is still room for improvement:
quote the file path/name to avoid trouble in case it contains white-spaces or other special characters;
use the odd syntax echo( to be able to output everything, even an empty string or literal strings like on, off and /?;
> "file.txt" echo(%y%
Hint:
To see what is actually going on, do not run a batch file by double-clicking on its icon; open a command prompt window and type its (quoted) path, so the window will remain open, showing any command echoes and error messages. In addition, for debugging a batch file, do not put #echo off on top (or comment it out by preceding rem, or use #echo on) in order to see command echoes.
Echo on means that everything that is executed in the batch is also shown in the console. So you see the command and on the following line the result.
You can turn this off with the echo off command or by preceding a # sign before the command you want to hide.
so
::turns of the echo for the remainder of the batch or untill put back on
::the command itself is not shwn because off the #
#echo off
set /p x=<file.txt
...
::the following won't be shown regardless the setting of echo
#set /a y = x+1
echo %y% > file.txt
EDIT after first comment
because your command echo %y%>file.txt doesn't work, you need a space before the > symbol, now you get the result of echo which gives you the current setting of echo
here a working sample, I put everything in one variable for sake of simplicity.
echo off
set /p x =< file.txt
set /a x += 1
echo %x% > file.txt
i'm making a automatic file writing system that writes random numbers onto a .txt file, I need it to also write .bat, the problem is I'm a newbie a batch!
This is a common problem, I like to call it the autosave, this is the code:
(
echo %YourVariable%
) > Mylittlevariable.sav
What does it do? What it does is when the script reaches this code, it writes the number for %YourVariable% in a .sav file, the .sav can easily be changed to .txt or another format.
I want it to load the variable now! The code:
< Mylittlevariable.sav (
set /p YourVariable=
)
Well I want it to write random numbers! This is a code for random numbers:
(
echo %random%
echo %random%
) > Random#s.txt
So now we get how this works? Now the batch file, before having a command in an autosave batch writer you have to put an echo, just how echo makes a statement in the cmd, it types everything after it in an autosave, too. The code for a basic .bat:
(
echo #echo off
echo title Basic Autosave Batch
echo :LOOP
echo set SUPERVAR=15
echo cls
echo echo Hello, world!
echo echo SUPERVAR: %%SUPERVAR%%
echo pause
echo goto LOOP
) > Mybasicbatch.bat
So, know we know what this does, correct? I hope I helped you!
PS the double amount of percent signs is necessary, if you had the normal amount the code wouldn't be read properly!
Basically when I am running this script, after runprog.exe returns (echos in cmd prompt) everytihng in the do ( ) section.
#echo off
set NODES=(server1.com server2.com)
for %%i in %NODES% do (
echo Log stuff... >> logfile.txt
runprog.exe /switch %%i
if %ERRORLEVEL%==0 (echo success) else (echo fail)
sleep 5
)
Edit: #echo off is at the top of the script.
The problem is in this sleep 5 command: it is a custom batch file that you have.
The funny thing is that if I run your batch file on my computer the exact same thing is happening, and I most probably have a different 'sleep' batch file than you. Mine contains the following:
#echo off
ping -n %1 127.0.0.1 > NUL 2>&1
Replacing sleep 5 with call sleep 5 fixes the problem here.
I have no idea why. Ask Microsoft.
As Mike Nakis said, it's the batch file sleep.
It's not important if sleep.bat contains #echo off or not.
The problem is that starting a batch file from another batch file transfers the control to the new batch file but doesn't return to the caller.
But in your case you have a FOR-loop which is completly cached by the cmd.exe.
That's the cause why the first batch doesn't stops immediately.
But when the second loop runs, the cmd.exe has leaved the batch-file-mode and is now in the cmd-line-mode.
You could control this by adding an echo line.
#echo off
set "world=" -- unset the world variable
for /L %%n in (1 1 3) do #(
call echo Hello %%n %%world%%
sleep.bat 1
)
The output will be
1 Hello
2 Hello %world%
3 Hello %world%
That's because the cmd-line-mode doesn't remove percent expansions when the variable is unset.
The CALL sleep.bat solves the problem, as then control simply returns to the caller, as expected.