When I use timeout (timeout /t2) with echo off I am getting output as below:
#echo off
timeout /t 2
#echo on
Output
-->Waiting for 0 seconds, press a key to continue ..
I want to hide the above text in cmd.
Redirect the output with >NUL, as #echo off only supresses the output of the command line itself, not of the output of the command.
#echo off
timeout /t 2 > nul
Try this:
#echo off
timeout 5 >nul
#echo on
Related
I want to enable logging to a batch file which should log all the contents of command prompt including input/output/error. So I have tried something like the following but it results in a empty log file without any contents. It looks, I am missing something. Below is the batch file.
#echo off
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit
exit /b 0
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root% >> %LOGFILE%
UPDATE.EXE E:\class.ora
ping 127.0.0.1 -n 6 > nul
The log file is empty because you suppressed the output with the command
echo off
Use echo on instead before commands that shall be logged. The command cd usually doesn't send anything to stdout. That's why you got an empty file.
Instead of this ping thingy I recommend
timeout 6
Try this one:
#echo off
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
echo on
call :logit >>%LOGFILE%
exit /b 0
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora
#timeout 6 >nul
Edit: (beacause asked in a comment)
The log file name can also include the date and time. You need to change the SET LOGFILE command appropriately. You can experiment, starting with this:
SET LOGFILE=C:\Users\xason\Desktop\Logs\log-%date%-%time%.txt
cd command has not STDOUT. You probably need to append STDOUT of your batch file to a text file. Run your batch file from cmd:
batch_file.bat >> "C:\Users\xason\Desktop\Logs\logs.txt"
Or, inside your batch file:
#echo off
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit >>"%LOGFILE%"
exit /b 0
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora
ping 127.0.0.1 -n 6 > nul
Or even:
#echo off
SET LOGFILE=C:\Users\xason\Desktop\Logs\logs.txt
call :logit
exit /b 0
:logit
set root=C:\ORACLE\ORA_DRIVERS
cd %root%
UPDATE.EXE E:\class.ora >> %LOGFILE%
ping 127.0.0.1 -n 6 > nul
Okay, so I'm trying to automate an ARM based CPU stress test program (it runs through the command prompt, and requires quite a bit of user input). What I'd like to do, is use for /f to watch the output, and run a few different sendkeys scripts when it see's their respective prompt strings. I've tried making two very barebones batch files to test this out:
The first is a simple batch file that asks for 3 separate inputs
#echo off
REM This is a file that asks for inputs
set /p q1="Please press 1: "
echo.
set /p q2="Please press 2: "
echo.
set /p q3="Please press 3: "
echo.
echo All buttons have been pressed,
echo.
echo button 1 was: %q1%
echo button 2 was: %q2%
echo button 3 was: %q3%
echo.
set /p foo="Press Enter to finish..."
The second is a batch file that runs the first (^) and looks for "Please Press 1: " in the output
#echo off
echo We will now launch the input command
echo.
timeout .5
echo in 5...
timeout 1
echo 4...
timeout 1
echo 3...
timeout 1
echo 2...
timeout 1
echo 1...
timeout 1
echo Launching...
for /f "delims= " %%i in ('Input.bat ^| find /i "Please press 1:"') do (
echo we did it
)
echo Did you make the right decisions?
set /p foo=
What I get as a result of this is a blank command prompt right after the "Launching..." Echo. If I press Enter four times, it comes back with the "we did it" echo along with the "Did you make the right decisions?" echo. So, finally on to the meat of my question. Is there any way to keep for /f from redirecting the stdout, as well as, is there any way to get for /f () do () to happen while the command is running?
So based on your request, sounds like you're trying to read strings from your batch1 script from a new batch2 script. To do this you will have to export your variables to a text document. From there, we can read the text document and gather the variables. If I am completely wrong on your request (It's a little hard to understand your request) then my oligopolies, hope those few tips can help you out at least.
To export files you need to use >>
For example: Echo This will be line one! >> Yourfile.txt
As another note, when you are done with the script, use goto :eof to exit.
Here is your 1st batch file:
#ECHO OFF
#DEL /Q %~dp0\strings.txt
REM This batch file asks for inputs
set /p q1="Please press 1: "
echo %q1% >> strings.txt
echo.
set /p q2="Please press 2: "
echo %q2% >> strings.txt
echo.
set /p q3="Please press 3: "
echo %q3% >> strings.txt
echo.
echo All buttons have been pressed,
echo.
echo button 1 was: %q1%
echo button 2 was: %q2%
echo button 3 was: %q3%
echo.
set /p foo="Press Enter to finish..."
goto :eof
Here is your 2nd batch file:
#ECHO OFF
echo We will now launch the input command.
echo.
echo in 5...
PING localhost -n 2 >nul
echo 4...
PING localhost -n 2 >nul
echo 3...
PING localhost -n 2 >nul
echo 2...
PING localhost -n 2 >nul
echo 1...
PING localhost -n 2 >nul
CLS
echo Launching...
:: Do action for each string. Use %%G to call the variable.
for /f "delims== tokens=*" %%G in (strings.txt) do (
echo Working on string: %%G
)
echo Did you make the right decisions?
pause > nul
DEL /Q %~dp0\strings.txt
goto :eof
Please keep in mind that instead of using timeout 1 you can actually use PING localhost -n 2 >nul so it does not actually freeze the prompt it's self.
I've searched over this website and i know now how to excecute all .exe in a folder, and this is how i can do that (with batch file .bat) :
for /r "." %%a in (*.exe) do start "" "%%~fa"
Is there a way to execute them with a timer (2s for instance) between each .exe file ?
for /r "." %%a in (*.exe) do start "" "%%~fa"&timeout /t 2 >nul
& separates commands, >nul directs the timeout conversation to the bit-bucket so it isn't seen.
2 options to produce a time out in batch are:
use timeout /t SEC where you replace SEC with the seconds you want it to time. Add the /nobreak option if you don't want the user to stop the timer by pressing any key. But be careful with this one: it doesn't guarantee a SEC seconds timer, it only guarentees it will make the system sleep for a timeinterval between SEC and SEC - 1 seconds
A second more accurate option is to use ping -n S 127.0.0.1 > nul. As there is 1 second between each ping this will result in a timeout of S-1 seconds
So for 2 seconds you could do:
for /r "." %%a in (*.exe) do (
start "" "%%~fa"
timeout /t 2 /nobreak
REM Or ping -n 3 127.0.0.1 > nul`
)
You can use timeout command. To wait 2 seconds, just use this between each of the .exe files.
timeout /T 2
I am trying to write the below code, and I want it to display the error before it exits, but whenever I run it, it exits right away.
#echo off
cd "haha"
if %errorlevel% 1 (
echo Failure reason given is %errorlevel%
sleep 5
exit /b %errorlevel%
)
echo files deleted successfully!
del /Q *
pause
I have also tried timeout /t 5 doesn't seem to work.
This should work :
#echo off
cd "haha" 2>nul
if %errorlevel%==1 (
echo Failure reason given is %errorlevel%
timeout /t 5
exit /b %errorlevel%
)
The correct syntax is
if errorlevel 1 (
meaning if errorlevel is 1 or greater
OR
if %errorlevel% geq 1 (
which means the same thing.
As it stands, cmd is interpreting you code as
if 1 1 (
(assuming errorlevel is 1). This is a syntax error, so cmd shows a message. If you are running by point-click and giggle, the window will close. If you are running from the prompt, then cmd will report a syntax error.
Be warned however that executing an exit statement from the prompt will abort the cmd instance. Better to use goto :eof unless you have good reason otherwise.
timeout /t 5 should work, but will generate a countdown.
timeout /t 5 >nul should appear simply to wait. The issue is that you have to solve the if syntax first, else the timeout instruction won't be reached.
Is it possible to make an if statement for batch based off a user opening a program?
I tried this:
color c
echo off
cls
:props
if open WINWORD.exe (echo a & pause 10 & exit)
goto props
Doing so will simply post the error 'WINWORD was unexpected at this time.' and kill the command prompt.
What I am trying to achieve is a batch file that will:
Look if anyone is opening WINWORD.exe
Once somebody has opened the file the command prompt will display 'a'
Exit the command prompt after 10 seconds once 'a' was displayed.
You can search for the process using tasklist.exe, together with find to count the instances:
set PROC=winword.exe
tasklist /FI "IMAGENAME eq %PROC%" | find /C /I "%PROC%"
I used a variable %PROC% to specify the process (*.exe) name just for the sake of convenience.
find will also exit with the return code (ErrorLevel) of 1 if no instances have been found. With a simple if statement you can do what you requested:
if not ErrorLevel 1 ((echo a) & timeout /T 10 /NOBREAK & exit)
I replaced the pause 10 by the timeout command because pause waits for the user to press any key (any arguments are ignored). The switch /NOBREAK means to ignore any key presses.
The parenthesis around echo a avoids a trailing space to be echoed as well.
So all together, the following should do what you asked for:
color c
echo off
set PROC=winword.exe
cls
:props
tasklist /FI "IMAGENAME eq %PROC%" | find /C /I "%PROC%" > nul
if not ErrorLevel 1 ((echo a) & timeout /T 10 /NOBREAK & exit)
timeout /T 1 /NOBREAK > nul
goto props
The (optional) > nul portion after find prevents it from displaying the found number of instances.
The second delay time timeout /T 1 has been inserted to avoid massive CPU load within this loop structure.