Pages

Saturday, September 14, 2019

Windows batch file - non-blocking command, function/subroutine and date wise file name

Let's go through the below batch file, say ping.bat:

REM REM means remark, it is the comment in windows batch file
REM Here we are extracting year, month and day separately from %date% and deriving sub-string

set year=%date:~10,4%
REM  %date:~Begin from, count of letters upto%

set month=%date:~4,2%
set day=%date:~7,2%

REM We are naming a file along with the current year, month and day with the data derived above
set LOGFILE=pingtest-%year%-%month%-%day%.log

REM A subroutine LOG is called and output redirected to a file
call :LOG > %LOGFILE%

REM Non-blocking program execution, start /b will launch the command as a new process and our batch file will continue execution and finish separately
start /b notepad.exe %LOGFILE%

exit /B

:LOG
ping 192.168.1.1
ping 192.168.1.2
ping 192.168.1.3
ping google.com



No comments :

Post a Comment