Problem with exporting commands to other batch file - batch-file

When I use the command echo pause >nul >>batchfile.bat from a cmd program it only prints out pause in batchfile.bat. How can I make it print pause >nul in batchfile.bat?
I've tried putting it in "", '', and doubling the arrows for nul >>nul and >>>nul but it printed either
pause; 'pause or showed an error "unexpected >`
at >>>nul.
echo pause >nul >>startserver.bat
I took it from my batch file which creates a command pause >nul and writes it into startserver.bat, however I only get a pause without >nul as output on startserver.bat
I need it to give output pause >nul in startserver.bat.
What I get is only pause, the >nul gets basically lost.

> is getting interpreted as redirection (and immediately overridden by the following >>). You have to escape it to make it interpreted literally. CMD's escape character is the caret, so do this:
echo pause ^>nul >>startserver.bat

Related

Check arguments in batch file

I'm trying to make a batch script that behave almost like a Linux command in terms of arguments. Here is the idea of the script.
When I run the script with the scenarios described in the code it seems to work fine. The problem that I have is coming when I tried to test the program with wrong parameters. For the 1st parameter being either -manual or -automat and the 2nd parameter being wrong the behave is normal, the program prints "Invalid Argument".
The problem that I encounter is when the 1st argument is not -manual or -automat. In this case I get the error: goto was unexpected at this time.
Does any1 have any idea why this is happening and how can I solve the problem?
#echo off
IF %1!==! goto Result0
IF %1==-manual IF %2!==! goto Result1_manual
IF %1==-automat IF %2!==! goto Result1_auto
IF %1==-manual IF %2==1 goto Result2_manual
IF %1==-manual IF %2==2 goto Result3_manual
IF %1==-automat IF %2==1 goto Result2_auto
IF %1==-automat IF %2==2 goto Result3_auto
:done
echo "Invalid argument"
pause
cmd /k
:Result0
echo "Result0"
pause
cmd /k
:Result1_manual
echo "Result1_manual"
pause
cmd /k
:Result2_manual
echo "Result2_manual"
pause
cmd /k
:Result3_manual
echo "Result3_manual"
pause
cmd /k
:Result1_auto
echo "Result1_auto"
pause
cmd /k
:Result2_auto
echo "Result2_auto"
pause
cmd /k
:Result3_auto
echo "Result3_auto"
pause
cmd /k
If "%1"=="-manual" goto Result1_manual
If parameters might be missing enclose with another character. If %1 in blank and you don't
If ==-manual goto Result1_manual
an illegal syntax, and if you do
If ""=="-manual" goto Result1_manual
a legal syntax that resolves to false.
IF %2!==!
will only be true if %2 is blank. If that is your intention don't use the quote character. It sometimes has special meaning.

My Batch file loop stops because of open file

I am trying to build a batch file that pings multiple devices on our network and continues logging ping results data in an output file in an infinite loop. However, the infinite loop gets hung up because the output file is open. Once I manually close the output file, the loop begins another iteration and logs more data. How do I automate this step? I've gone through so many options with taskkill, but none of them will close the output file for some reason. Other Notepad files close, but not the output file running on notepad.
Thanks for you help! Code is below:
#echo off
if exist C:\Users\Tsgadmin\Desktop\data\computers.txt goto Label1
echo.
echo Cannot find C:\Users\Tsgadmin\Desktop\data\computers.txt
echo.
Pause
goto :eof
:Label1
:loop
echo ================================================= >> C:\Users\Tsgadmin\Desktop\ping_firepanels_output.txt
echo PingTest executed on %date% at %time% >> C:\Users\Tsgadmin\Desktop\ping_firepanels_output.txt
for /f %%i in (C:\Users\Tsgadmin\Desktop\data\computers.txt) do call :Sub %%i
notepad C:\Users\Tsgadmin\Desktop\ping_firepanels_output.txt
choice /n/t:c,<10>/c:cc
echo ================================================= >> C:\Users\Tsgadmin\Desktop\ping_firepanels_output.txt
echo. >> C:\Users\Tsgadmin\Desktop\ping_firepanels_output.txt
start notepad.exe
for /f "tokens=2" %%x in ('tasklist ^| findstr notepad.exe') do set PIDTOKILL=%%x
taskkill /F /IM notepad.exe > nul
goto loop
goto :eof
:Sub
echo Testing %1
ping -n 1 %1 >> C:\Users\Tsgadmin\Desktop\ping_firepanels_output.txt | find /i "(0% loss)"
echo %1 Testing done
echo %1 Testing done >> C:\Users\Tsgadmin\Desktop\ping_firepanels_output.txt
Here is your batch code rewritten for this task:
#echo off
setlocal EnableExtensions DisableDelayedExpansion
set "LogFile=%USERPROFILE%\Desktop\ping_firepanels_output.txt"
set "ListFile=%USERPROFILE%\Desktop\data\computers.txt"
if exist "%ListFile%" goto PrepareForPings
echo/
echo Cannot find file: "%ListFile%"
echo/
endlocal
pause
goto :EOF
rem Delete existing log file before running the echo requests.
rem Get just file name with file extension without path from
rem log file name with path specified at top of the batch file.
:PrepareForPings
del "%LogFile%" 2>nul
for /F %%I in ("%LogFile%") do set "LogFileName=%%~nxI"
rem Always terminate (not kill) running Notepad instance with having
rem the log file opened for viewing before running first/next test run.
:PingLoop
%SystemRoot%\System32\taskkill.exe /FI "WINDOWTITLE eq %LogFileName% - Notepad" >nul 2>nul
echo =================================================>>"%LogFile%"
>>"%LogFile%" echo PingTest executed on %DATE% at %TIME%
echo/>>"%LogFile%"
for /F "usebackq" %%I in ("%ListFile%") do (
echo Testing %%I ...
%SystemRoot%\System32\ping.exe -n 1 -w 500 %%I>nul
if errorlevel 1 (
echo %%I is not available in network (no reply^).>>"%LogFile%"
) else echo %%I is available.>>"%LogFile%"
echo %%I testing done.
)
echo =================================================>>"%LogFile%"
echo/>>"%LogFile%"
start "" %SystemRoot%\notepad.exe "%LogFile%"
echo/
%SystemRoot%\System32\choice.exe /C NY /N /T 10 /D Y /M "Run again (Y/n): "
echo/
if errorlevel 2 goto PingLoop
endlocal
In general it is advisable to define environment variables with names of files specified multiple times in the batch file at top to make it easier to modify them in future.
On referencing those file environment variables it is strongly recommended to enclose the name in double quotes to get a working batch file also when file name with path contains a space character or one of these characters: &()[]{}^=;!'+,`~
If a file name enclosed in double quotes is specified as text file of which lines to read in a for /F command line, it is necessary to use option usebackq to get interpreted the file name enclosed in double quotes as file name and not as string to process by FOR.
The DosTips forum topic ECHO. FAILS to give text or blank line - Instead use ECHO/ explains why it is better to use echo/ instead of echo. to output an empty line.
The TASKKILL command used to send Notepad the terminate signal for a graceful termination should be send only to the Notepad instance having the log file opened and not any other perhaps running Notepad instance.
An ECHO line redirected to a file with > or >> with a space left to redirection operator results in having this space also written as trailing space into the file. For that reason there should be no space between text to write into the file and redirection operator. A space right to > or >> would be no problem as not written into the file.
When a variable text is output on an ECHO line redirected into a file which could end with 1, 2, 3, ... 9, it is necessary to specify the redirection from STDOUT into the file with >> at beginning of the line as otherwise 1>>, 2>>, ... would be interpreted different as expected on execution of the ECHO command line. Read also the Microsoft article about Using Command Redirection Operators.
There is no subroutine necessary for this task. A command block starting with opening parenthesis ( and matching ) can be used here too. That makes the execution of the loop a bit faster, not really noticeable faster, but nevertheless faster.
There is a text written with echo into the log file containing also a closing parenthesis ) not within a double quoted string. This ) would be interpreted as matching ) for opening ( of true branch of IF condition. It is necessary to escape ) with caret character ^ to get ) interpreted as literal character by Windows command interpreter.
PING exits with exit code 1 if the echo request was not replied. Otherwise on successful reply the exit code is 0. It is better to evaluate the exit code via errorlevel than filtering the language dependent output.
New instance of Notepad with the log file to view is started by this batch file using command start to run Notepad in a separate process running parallel to command process executing the batch file. Otherwise the execution of the batch file would be halted as long as the started Notepad instance is not closed by the user. That different behavior can be easily seen on removing start "" at beginning of the command line starting Notepad.
The command CHOICE gives the user of the batch file the possibility to exit the loop by pressing key N (case-insensitive) within 10 seconds. Otherwise the user prompt is automatically answered with choice Y and the loop is executed once again by first terminating running Notepad.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
choice /?
del /?
echo /?
endlocal /?
for /?
goto /?
if /?
pause /?
ping /?
set /?
setlocal /?
start /?
taskkill /?
See also Windows Environment Variables for details on environment variables USERPROFILE and SystemRoot as used in this batch file.

ECHO is off error when printing line to a file

SET NEWLINE=^& echo.
FIND /C /I "telemetry.microsoft.com" %WINDIR%\system32\drivers\etc\hosts > NUL 2>&1
IF %ERRORLEVEL% NEQ 0 ECHO %NEWLINE%^0.0.0.0 telemetry.microsoft.com>>%WINDIR%\System32\drivers\etc\hosts
New to batch scripting. I seem to be getting and ECHO is off error which seems to have something to do with a variable not being set? Thanks
I would use the nonhacky method of outputting a newline via echo.:
IF ERRORLEVEL 1 (echo.&echo 0.0.0.0 telemetry.microsoft.com)>>%WINDIR%\System32\drivers\etc\hosts
Explanation of the problem:
After expansion the first echo (that is inside newline variable) doesn't have anything to output and thus it displays its status.
. should be at the beginning so that it's appended to the first echo to actually output the newline
No space needed between echo and %newline% so that the added dot follows echo
^ between %newline% and the following text is not needed as there's nothing to escape
Both echo commands should be surrounded with ( ) to indicate output redirection scope
So your original code might be like this:
SET NEWLINE=.^& echo
FIND /C /I "telemetry.microsoft.com" %WINDIR%\system32\drivers\etc\hosts > NUL 2>&1
IF %ERRORLEVEL% NEQ 0 (ECHO%NEWLINE% 0.0.0.0 telemetry.microsoft.com)>>%WINDIR%\System32\drivers\etc\hosts

Looping a command in a Batch file

cd \Users\Kurashima\AppData\Local\Android\sdk\platform-tools
adb devices
adb shell screenrecord /storage/ext_sd/vid001.mp4
adb shell screenrecord /storage/ext_sd/vid002.mp4
adb shell screenrecord /storage/ext_sd/vid003.mp4
Before getting to my question, I must first make a disclaimer. I am a beginner in writing batch files.
What I want to do is to make those last three lines into a loop that will execute any number of times, incrementing the number in the file name, until the Ctrl+C command is entered. I know how to make a simple loop, using :start and goto start, but I'd end up overwriting the same file every time. How do I avoid that?
Here is a complete script. Remove all REM to make it operational for you.
Note pause >NUL command waits for pressing any key (without prompting due to >NUL); after pressing Ctrl+C it will ask for Terminate batch job (Y/N)?.
Remove that pause >NUL line if you want to work unattended; the Ctrl+C functionality should retain; if does not retain, then use Ctrl+Break
#ECHO OFF >NUL
#SETLOCAL enableextensions disabledelayedexpansion
rem cd \Users\Kurashima\AppData\Local\Android\sdk\platform-tools
rem adb devices
set /A "ii=0"
:start
set /A "ii+=1"
set "ss=000%ii%"
set "ss=%ss:~-3%"
echo adb shell screenrecord /storage/ext_sd/vid%ss%.mp4
rem adb shell screenrecord /storage/ext_sd/vid%ss%.mp4
pause >NUL
if %ii% LSS 999 goto :start
:endlocal
#ENDLOCAL
goto :eof

"Echo is On" Output to text file blank

for /f %%C in ('Find /V /C "" ^< mamegamelist.txt') do set Count=%%C
echo The file has %Count% lines.
echo %Count%>gamecount.txt
set /p texte=< mamegamelist.txt
echo %texte%>currentgame.txt
set/a played=3379-%Count%
echo %played%>played.txt
pause
Everything works fine except for this line:
set/a played=3379-%Count%
echo %played%>played.txt
It evaluates correctly, but it doesn't write out anything to the file.
If I change the arithmetic expression to something like this:
set/a played=%Count%+2
echo %played%>played.txt
It will work perfectly fine. I'm pretty sure I don't have any unnecessary spaces anywhere. Any help appreciated!
You don't tell us what numbers you are using.
If mamegamelist.txt is empty or has fewer than 10 lines, you get an echo is off message from the first redirected echo.
A single digit before a redirector redirects an output stream to the file. Somethimes the syntax can be tricky to formulate.
Try
>played.txt echo %played%
(and follow the bouncing ball for any other possible exactly-1-digit-before-a-redirector instructions, like to gamecount.txt)
By placing the redirector at the start of the line, any problem with the syntax is avoided.

Resources