In my script I'm using timeout to set a maximum timelimit for executing a command.
start 7z.exe t %%f
if errorlevel 2 (DEL %%f & echo File broken. Reload File!)
timeout /t 3600
If the execution lasts longer than an hour the timeout should break the execution. That's working without any problems.
But how can I ignore the timeout counting and proceed my script automatically if the command finishes before the timestamp?
As curretly written, start command without /W switch does not affect/change errorlevel! Proof:
==> hfhgf
'hfhgf' is not recognized as an internal or external command,
operable program or batch file.
==> echo %errorlevel%
9009
==> start "" ping.exe localhost
==> echo %errorlevel%
9009
==> start "" /W ping.exe localhost
==> echo %errorlevel%
0
Use /W or /WAIT switch (start application and wait for it to terminate) and remove all that timeout line as follows. I guess that your code snippet could be do part of a for loop:
for %%f in (*.zip) do (
(call )
start "" /W 7z.exe t "%%f"
if errorlevel 2 (
echo File broken. Reload File %%f
DEL "%%f"
)
)
On (call ) explanation, see Dave Benham's reply to setting ERRORLEVEL to 0 question:
If you want to force the errorlevel to 0, then you can use this
totally non-intuitive, but very effective syntax: (call ). The space
after call is critical. If you want to set the errorlevel to 1, you
can use (call). It is critical that there not be any space after
call.
Related
I am trying to adjust some code which is shown below and hitting walls.
The commandline appears as:
cmd.exe /U /C "C:\Program Files\StorageCraft\ShadowProtect\VerifyImages.cmd <PathOfDirectoryWhichContainsImageFiles> <PathToOutputLogFile>
The code basically runs an image verify command against all md5 files in a directory. The problem is that some directories have >200 md5 files and I only want to verify the files created in the last 24 hrs.
I have been able to create a list of the files created in the last 24hrs and output to a text file using a powershell command.
Is it possible to adjust the script below so that it reads the text file line by line and runs the VERIFY_SUB against each? I have tried using the FOR /F command with little luck to this point.
Thanks in advance.
REM *** START OF MAIN ROUTINE ***
SETLOCAL
PUSHD
CD /D %~dp0
REM Strip the outer quotes off of the directory parameter
SET PARAM_DIR=%1
SET PARAM_DIR=###%PARAM_DIR%###
SET PARAM_DIR=%PARAM_DIR:"###=%
SET PARAM_DIR=%PARAM_DIR:###"=%
SET PARAM_DIR=%PARAM_DIR:###=%
REM Strip the outer quotes off of the output log file parameter
SET PARAM_OUTPUT_FILE=%2
SET PARAM_OUTPUT_FILE=###%PARAM_OUTPUT_FILE%###
SET PARAM_OUTPUT_FILE=%PARAM_OUTPUT_FILE:"###=%
SET PARAM_OUTPUT_FILE=%PARAM_OUTPUT_FILE:###"=%
SET PARAM_OUTPUT_FILE=%PARAM_OUTPUT_FILE:###=%
FOR %%A IN ("%PARAM_DIR%\*.md5") DO (call :VERIFY_SUB "%%A" "%PARAM_OUTPUT_FILE%")
POPD
ENDLOCAL
GOTO :EOF
REM *** END OF MAIN ROUTINE ***
:VERIFY_SUB
#ECHO VERIFYING MD5 FILE %1
#ECHO VERIFYING MD5 FILE %1 >> %2
image.exe v %1 >> %2
#ECHO. >> %2
#ECHO. >> %2
#ECHO. >> %2
GOTO :EOF
Does anyone knows how to control many command prompt windows through one . What exactly I would like to do is start many command windows and then run multiple commands in all of them through a batch file. Such as starting adb shell logcat in one, kmsg in one and if kmsg stops then turn red, and similar things.
For that I need to be able to listen to events from other command lines and also send commands to many command prompt one after the other.
Thanks your reply is appreciated.
If you know how to do it in perl that would also work. Plzz help!!
Updated;
Update Notes:
Took note of Jeb's suggestion and took his advice as well as Endoro's
Okay, this will be a little complicated because it needs batch files to write into some sort of file and another batch file getting / grabbing the data from said file. In order to do this, we must produce the "sender / terminal / MAIN window" for your batch file;
The script i am writing for you as of now can only support 4 Batch files being controlled by a mother batch file.
#echo off
:a
title Main Terminal
echo ---------------------------
set /p prompt1="Command 1: "
set /p prompt2="Command 1: "
set /p prompt3="Command 1: "
set /p prompt4="Command 1: "
if defined prompt echo %prompt% > com1.rsm
if defined prompt2 echo %prompt2% > com2.rsm
if defined prompt3 echo %prompt3% > com3.rsm
if defined prompt4 echo %prompt4% > com4.rsm
:: .RSM file extension means ReSource Module; I made it myself :3
goto a
Receiver
#echo off
title Reciever 1
:check
if EXIST com1.rsm goto get
timeout /t 1 >nul
echo Waiting for packet
goto check
:get
set /p prompt1=<com1.rsm
%prompt1%
del com1.rsm
goto check
Receiver 2
#echo off
title Reciever 2
:check
if EXIST com2.rsm goto get
timeout /t 1 >nul
echo Waiting for packet
goto check
:get
set /p prompt=<com2.rsm
%prompt%
del com1.rsm
goto check
Receiver 3
#echo off
title Reciever 3
:check
if EXIST com3.rsm goto get
timeout /t 1 >nul
echo Waiting for packet
goto check
:get
set /p prompt=<com3.rsm
%prompt%
del com1.rsm
goto check
Receiver 4
#echo off
title Reciever 4
:check
if EXIST com4.rsm goto get
timeout /t 1 >nul
echo Waiting for packet
goto check
:get
set /p prompt=<com4.rsm
%prompt%
del com1.rsm
goto check
You're welcome;
SonorousTwo
1 echo off
2
3 echo Please enter a date
4 set /p a=
5 echo %a%
6 for /D %%d in (M:\Serienbriefauftrag\*) do (
7 if not exist %%d\Erledigt\*.bat echo %%d
8 )
9 pause
10 for /d %%b in (dir M:\Serienbriefauftrag\%%d /T:C)
11 echo %%b
12 pause
The Code works fine until line 10 (syntax error) the aim is, to get the creation date of the folder compare it with "%a%" and and if it's under the entered date "%a%", the folder should be moved. but somehow...
found a way to get the creation date
if not exist %%d\Erledigt\*.bat echo %%~td|findstr /i /l
the new task is to make the output "calculateable"
Two things:
1) In line 10 %%d is undefined (empty), since the scope of the first loop (where it is defined) is left in line 8.
You can set another variable to %%d to be able to use it after the loop ends, but be careful. In batch using set in for loops is a bit tricky. To get around the aweful use of delayed variable expansion, I would suggest to you staying inside of the loop while doing your work.
#echo off
set /p a=Please enter a date:
for /d %%d in (M:\Serienbriefauftrag\*) do (
if not exist %%d\Erledigt\*.bat (
:: Do whatever you want with %%d in here
echo "%%d"
)
)
pause
2) The syntax of your second loop is wrong. You are missing the do before the loop body and /d lets the loop iterate over all folders in a specified directory. dir M:\Serienbriefauftrag\%%d /T:C is not a directory but a command.
If you want to iterate over the output of that command, you have to use /f and put the contents of the paranthesis in single quotes, like that ('dir M:\Serienbriefauftrag\%%d /T:C').
I need to know that before any attempt to do anything with such file.
Not sure about locked directories (does Windows have that?)
But detecting if a file is being written to by another process is not difficult.
#echo off
2>nul (
>>test.txt echo off
) && (echo file is not locked) || (echo file is locked)
I use the following test script from another window to place a lock on the file.
(
>&2 pause
) >> test.txt
When I run the 2nd script from one window and then run the 1st script from a second window, I get my "locked" message. Once I press <Enter> in the 1st window, I get the "unlocked" message if I rerun the 1st script.
Explanation
Whenever the output of a command is redirected to a file, the file of course must be opened for write access. The Windows CMD session will attempt to open the file, even if the command does not produce any output.
The >> redirection operator opens the file in append mode.
So >>test.txt echo off will attempt to open the file, it writes nothing to the file (assuming echo is already off), and then it closes the file. The file is not modified in any way.
Most processes lock a file whenever they open a file for write access. (There are OS system calls that allow opening a file for writing in a shared mode, but that is not the default). So if another process already has "test.txt" locked for writing, then the redirection will fail with the following error message sent to stderr - "The process cannot access the file because it is being used by another process.". Also an error code will be generated upon redirection failure. If the command and the redirection succeed, then a success code is returned.
Simply adding 2>nul to the command will not prevent the error message because it redirects the error output for the command, not the redirection. That is why I enclose the command in parentheses and then redirect the error output to nul outside of the parens.
So the error message is effectively hidden, but the error code is still propagated outside of the parens. The standard Windows && and || operators are used to detect whether the command inside the parens was successful or failed. Presumably echo off will never fail, so the only possible reason for failure would be the redirection failed. Most likely it fails because of a locking issue, though technically there could be other reasons for failure.
It is a curious "feature" that Windows does not set the %ERRORLEVEL% dynamic variable to an error upon redirection failure unless the || operator is used. (See File redirection in Windows and %errorlevel%). So the || operator must read the returned error code at some low level, not via the %ERRORLEVEL% variable.
Using these techniques to detect redirection failure can be very useful in a batch context. It can be used to establish locks that allow serialization of multiple events in parallel processes. For example, it can enable multiple processes to safely write to the same log file at the "same" time. How do you have shared log files under Windows?
EDIT
Regarding locked folders. I'm not sure how Windows implements this, perhaps with a lock. But if a process has an active directory involving the folder, then the folder cannot be renamed. That can easily be detected using
2>nul ren folderName folderName && echo Folder is NOT locked || echo folder is LOCKED
EDIT
I have since learned that (call ) (with a space) is a very fast command without side effects that is guaranteed to succeed with ERRORLEVEL set to 0. And (call) (without a space) is a fast command without side effects that is guaranteed to fail with ERRORLEVEL 1.
So I now use the following to check if a file is locked:
2>nul (
>>test.txt (call )
) && (echo file is not locked) || (echo file is locked)
In addition to great answer from dbenham, the following form finally help me understand used technique:
( type nul >> file.txt ) 2>nul || echo File is locked!
type nul command gives an empty output and does not affect the current echo setting like echo off command in orginal.
If you want to use if–then–else condition remember of correct order - success statement (&&) is going first and alternate statement (||) is going second:
command && (echo Command is successful) || (echo Command has failed)
If you download and install the Windows Server 2003 Resource Kit Tools there is a utility called oh.exe that will list open file handles for a given file:
http://www.microsoft.com/en-us/download/details.aspx?id=17657
Once you install it, reboot your machine and you'll be able to use the utility. You can see all the options in the Help and Support center as well as by typing oh /? in the command prompt.
(Info from : http://windowsxp.mvps.org/processlock.htm )
Note, the writing of a message stating the file status was less helpful than a batch command that set a return code. For example, return code 1 if file is locked.
#echo off
2>nul (
>>test.tmp echo off
) && (EXIT /B 0) || (EXIT /B 1)
Other answers resulted in side-effects for me. For instance, the following from this answer will cause file watchers to trigger:
COPY /B app.exe+NUL app.exe
And the following from the top answer here would overwrite any changes made to the target file:
2>nul (
>>test.txt (call )
) && (echo file is not locked) || (echo file is locked)
On modern version of Windows, you can call into Powershell to accomplish this task with zero side-effects:
powershell -Command "$FileStream = [System.IO.File]::Open('%FILE%', 'Open', 'Write'); $FileStream.Close(); $FileStream.Dispose()" && (echo File is not locked) || (echo File is locked)
This will not modify the file or its metadata at all, even when it isn't locked.
Example usage
I use this method in my custom git mergetool script for merging Excel files. The way a git mergetool works is that it waits for the script shell to exit, then checks if the target file was modified, prompting with "XX.yyy seems unchanged. Was the merge successful [y/n]?" if it wasn't. However, Excel (at least the version I'm using) does not spawn a new process for each file it opens. So if Excel is already open, the script will exit immediately, and git will detect no changes to the file, resulting in that prompt.
So I devised the method above, and I use it like below:
REM block until MERGED is closed
:loop
powershell -Command "$FileStream = [System.IO.File]::Open('%MERGED%', 'Open', 'Write'); $FileStream.Close(); $FileStream.Dispose()" >NUL 2>NUL || (goto :loop)
Incidentally, dbenham's solution also seems to be an effective way to find out if a process is running. It was the best solution I found for the following application:
start /b "job1.exe >> job1.out"
start /b /wait "job2.exe >> job2.out"
::wait for job1 to finish using dbenham's code to check if job1.out is in use
comparejobs.exe
Just i want to share with you an example of my script based on #dbenham's trick
Description of this script : Check_Locked_Files.bat :
This script can scan and check for locked files on a set of folders that can be modified into the script; for example, i have chosen those set of folders to be scanned :
Set Folders=^
^ "%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup"^
^ "%UserProfile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"^
^ "%ProgramFiles%\Internet Explorer"^
^ "%ProgramFiles%\Skype"^
^ "%ProgramFiles%\TeamViewer"^
^ "%WinDir%\system32\drivers"^
^ "%Temp%"
The output result is in HTML format for more readability.
If the file is locked we show it in red color otherwise we show it in green color.
And the whole script is : Check_Locked_Files.bat
#echo off
Rem This source is inspired from here
Rem hxxps://stackoverflow.com/questions/
Rem 10518151/how-to-check-in-command-line-if-a-given-file-or-directory-is-locked-used-by-any?answertab=active#tab-top
Rem Thanks for dbenham for this nice trick ;)
Mode con cols=90 lines=5 & color 9E
Title Scan and Check for Locked Files by Hackoo 2017
set "LogFile=%~dp0%~n0.html"
(
echo ^<html^>
echo ^<title^> Scan and Check for locked files by Hackoo 2017^</title^>
echo ^<body bgcolor^=#ffdfb7^>
echo ^<center^>^<b^>Log Started on %Date% # %Time% by the user : "%username%" on the computer : "%ComputerName%"^</b^>^</center^>
)> "%LogFile%"
echo(
echo --------------------------------------------------------------------------
echo Please Wait a while ....... Scanning for locked files is in progress
echo --------------------------------------------------------------------------
Rem We Play radio just for fun and in order to let the user be patient until the scan ended
Call :Play_DJ_Buzz_Radio
Timeout /T 3 /nobreak>nul
cls
Set Folders=^
^ "%ProgramData%\Microsoft\Windows\Start Menu\Programs\Startup"^
^ "%UserProfile%\AppData\Roaming\Microsoft\Windows\Start Menu\Programs\Startup"^
^ "%ProgramFiles%\Internet Explorer"^
^ "%ProgramFiles%\Skype"^
^ "%ProgramFiles%\TeamViewer"^
^ "%WinDir%\system32\drivers"^
^ "%Temp%"
#For %%a in (%Folders%) Do (
( echo ^<hr^>^<font color^=DarkOrange^>^<B^>Folder : %%a^</B^>^</font^>^<hr^>) >> "%LogFile%"
#for /f "delims=" %%b in ('Dir /A-D /s /b "%%~a\*.*"') do (
Call :Scanning "%%~nxb"
Call:Check_Locked_File "%%~b" "%LogFile%"
)
)
(
echo ^<hr^>
echo ^<center^>^<b^>Log ended on %Date% # %Time% on the computer : "%ComputerName%"^</b^>^</center^>
echo ^</body^>
echo ^</html^>
)>> "%LogFile%"
Start "" "%LogFile%" & Call :Stop_Radio & exit
::***********************************************************************************
:Check_Locked_File <File> <LogFile>
(
2>nul (
>>%1 (call )
) && ( #echo ^<font color^=green^>file "%~1"^</font^>^<br^>
) || (
#echo ^<font color^=red^>file "%~1" is locked and is in use^</font^>^<br^>
)
)>>%2 2>nul
exit /b
::***********************************************************************************
:Scanning <file>
cls
echo(
echo --------------------------------------------------------------------------
echo Please Wait a while... Scanning for %1
echo --------------------------------------------------------------------------
exit /b
::***********************************************************************************
:Play_DJ_Buzz_Radio
Taskkill /IM "wscript.exe" /F >nul 2>&1
Set "vbsfile=%temp%\DJBuzzRadio.vbs"
Set "URL=http://www.chocradios.ch/djbuzzradio_windows.mp3.asx"
Call:Play "%URL%" "%vbsfile%"
Start "" "%vbsfile%"
Exit /b
::**************************************************************
:Play
(
echo Play "%~1"
echo Sub Play(URL^)
echo Dim Sound
echo Set Sound = CreateObject("WMPlayer.OCX"^)
echo Sound.URL = URL
echo Sound.settings.volume = 100
echo Sound.Controls.play
echo do while Sound.currentmedia.duration = 0
echo wscript.sleep 100
echo loop
echo wscript.sleep (int(Sound.currentmedia.duration^)+1^)*1000
echo End Sub
)>%~2
exit /b
::**************************************************************
:Stop_Radio
Taskkill /IM "wscript.exe" /F >nul 2>&1
If Exist "%vbsfile%" Del "%vbsfile%"
::**************************************************************
:: Create the file Running.tmp
ECHO %DATE% > Running.tmp
ECHO %TIME% >> Running.tmp
:: block it and do the work
(
>&2 CALL :Work 30
) >> Running.tmp
:: when the work is finished, delete the file
DEL Running.tmp
GOTO EOF
:: put here the work to be done by the batch file
:Work
ping 127.0.0.1 -n 2 -w 1000 > NUL
ping 127.0.0.1 -n %1 -w 1000 > NUL
:: when the process finishes, the execution go back
:: to the line after the CALL
In case you want to use this in a Cygwin Bash, here are the one-liners:
# To lock a file: (in a different window)
cmd.exe /C "( >&2 pause ) >> test.txt"
#Press any key to continue . . .
# To test if a file is locked (with text)
cmd.exe /C "2>nul ( >>test.txt (call ) ) && (echo ok) || (echo locked)"
#locked
# To test if a file is locked (with POSIX exit code)
cmd.exe /C "2>nul ( >>test.txt (call ) ) && (exit /b 0) || (exit /b 1)"
echo $?
#1
In case of windows network share you can try powershell command:
Get-SmbOpenFile
For example execute on file server command as administrator:
Get-SmbOpenFile | Where-Object -Property Path -match "file.txt"
I want to continuously ping a server and see a message box when ever it responds i.e. server is currently down. I want to do it through batch file.
I can show a message box as said here Show a popup/message box from a Windows batch file
and can ping continuously by
ping <servername> -t
But how do I check if it responded or not?
The question was to see if ping responded which this script does.
However this will not work if you get the Host Unreachable message as this returns ERRORLEVEL 0 and passes the check for Received = 1 used in this script, returning Link is UP from the script. Host Unreachable occurs when ping was delivered to target notwork but remote host cannot be found.
If I recall the correct way to check if ping was successful is to look for the string 'TTL' using Find.
#echo off
cls
set ip=%1
ping -n 1 %ip% | find "TTL"
if not errorlevel 1 set error=win
if errorlevel 1 set error=fail
cls
echo Result: %error%
This wont work with IPv6 networks because ping will not list TTL when receiving reply from IPv6 address.
The following checklink.cmd program is a good place to start. It relies on the fact that you can do a single-shot ping and that, if successful, the output will contain the line:
Packets: Sent = 1, Received = 1, Lost = 0 (0% loss),
By extracting tokens 5 and 7 and checking they're respectively "Received" and "1,", you can detect the success.
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
:loop
set state=down
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%b"=="xunreachable." goto :endloop
if "x%%a"=="xReceived" if "x%%c"=="x1," set state=up
)
:endloop
echo.Link is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
Call it with the name (or IP address) you want to test:
checklink 127.0.0.1
checklink localhost
checklink nosuchaddress
Take into account that, if your locale is not English, you must replace Received with the corresponding keyword in your locale, for example recibidos for Spanish. Do a test ping to discover what keyword is used in your locale.
To only notify you when the state changes, you can use:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=down
for /f "tokens=5,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%a"=="xReceived" if "x%%b"=="x1," set state=up
)
if not !state!==!oldstate! (
echo.Link is !state!
set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
However, as Gabe points out in a comment, you can just use ERRORLEVEL so the equivalent of that second script above becomes:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
set oldstate=neither
:loop
set state=up
ping -n 1 !ipaddr! >nul: 2>nul:
if not !errorlevel!==0 set state=down
if not !state!==!oldstate! (
echo.Link is !state!
set oldstate=!state!
)
ping -n 2 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
I know this is an old thread, but I wanted to test if a machine was up on my system and unless I have misunderstood, none of the above works if my router reports that an address is unreachable. I am using a batch file rather than a script because I wanted to "KISS" on pretty much any WIN machine. So the approach I used was to do more than one ping and test for "Lost = 0" as follows
ping -n 2 %pingAddr% | find /I "Lost = 0"
if %errorlevel% == 0 goto OK
I haven't tested this rigorously but so far it does the job for me
I have made a variant solution based on paxdiablo's post
Place the following code in Waitlink.cmd
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=%1
:loop
set state=up
ping -n 1 !ipaddr! >nul: 2>nul:
if not !errorlevel!==0 set state=down
echo.Link is !state!
if "!state!"=="up" (
goto :endloop
)
ping -n 6 127.0.0.1 >nul: 2>nul:
goto :loop
:endloop
endlocal
For example use it from another batch file like this
call Waitlink someurl.com
net use o: \\someurl.com\myshare
The call to waitlink will only return when a ping was succesful. Thanks to paxdiablo and Gabe. Hope this helps someone else.
Here's something I found:
:pingtheserver
ping %input% | find "Reply" > nul
if not errorlevel 1 (
echo server is online, up and running.
) else (
echo host has been taken down wait 3 seconds to refresh
ping 1.1.1.1 -n 1 -w 3000 >NUL
goto :pingtheserver
)
Note that ping 1.1.1.1 -n -w 1000 >NUL will wait 1 second but only works when connected to a network
thanks to #paxdiablo and #Jan Lauridsen
this is my modification to check and IP (local machine), good for case which connection is dropped either from dhcp server or any other issue, tested Under WinXP PRO SP3
checkconnection.cmd:
#setlocal enableextensions enabledelayedexpansion
#echo off
set ipaddr=8.8.8.8
:loop
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%b"=="xunreachable." goto :endloop
if "x%%b"=="xtimed out." goto :endloop
if "x%%a"=="xReceived" if "x%%c"=="x1," goto :up
)
:endloop
set state=Down
echo.Connection is !state!
ping -n 2 127.0.0.1 >nul: 2>nul:
echo starting Repairing at %date% %time%>>D:\connection.log
call repair.cmd>>D:\connection.log
ping -n 10 127.0.0.1 >nul: 2>nul:
goto :loop
endlocal
:up
set state=Up
echo.Connection is !state!
ping -n 6 127.0.0.1 >nul: 2>nul:
cls
goto :loop
endlocal
if no ping response from google DNS then start repair, i had static IP set for this purpose but it should work with dinamic as well.
repair.cmd:
route -f
ipconfig /release
ipconfig /renew
arp -d *
nbtstat -R
nbtstat -RR
ipconfig /flushdns
ipconfig /registerdns
cls
My Best Regards
You can ping without "-t" and check the exit code of the ping. It reports failure when there is no answer.
Simple version:
for /F "delims==, tokens=4" %a IN ('ping -n 2 127.0.0.1 ^| findstr /R "^Packets: Sent =.$"') DO (
if %a EQU 2 (
echo Success
) ELSE (
echo FAIL
)
)
But sometimes first ping just fail and second one work (or vice versa) right? So we want to get success when at least one ICMP reply has been returned successfully:
for /F "delims==, tokens=4" %a IN ('ping -n 2 192.168.1.1 ^| findstr /R "^Packets: Sent =.$"') DO (
if %a EQU 2 (
echo Success
) ELSE (
if %a EQU 1 (
echo Success
) ELSE (
echo FAIL
)
)
)
I hope this helps someone. I use this bit of logic to verify if network shares are responsive before checking the individual paths. It should handle DNS names and IP addresses
A valid path in the text file would be
\192.168.1.2\'folder' or \NAS\'folder'
#echo off
title Network Folder Check
pushd "%~dp0"
:00
cls
for /f "delims=\\" %%A in (Files-to-Check.txt) do set Server=%%A
setlocal EnableDelayedExpansion
ping -n 1 %Server% | findstr TTL= >nul
if %errorlevel%==1 (
ping -n 1 %Server% | findstr "Reply from" | findstr "time" >nul
if !errorlevel!==1 (echo Network Asset %Server% Not Found & pause & goto EOF)
)
:EOF
I've modified PaxDiablo's code slightly to better fit with what I was doing and thought I'd share. My objective was to loop through a list of IP addresses to see if any were left on, and this code is to be run at the end of the last shift of the weekend to check if everyone is following the instructions to shut down all PCs before they go home.
Since using a goto in a for loop breaks out of all for loops not just the lowest nested for loop, PaxDiablo's code stopped processing further IP addresses when it got to one that was unreachable. I found that I could add a second variable to track that it was unreachable rather than exiting the loop and now this code is now running perfectly for me.
I have the file saved as CheckPCs.bat and though I'm guessing it's not proper coding practice, I do have the IP addresses listed below the code along with a description which in my case is the physical location of the PC. As mentioned by others you will have to modify the code further for other localizations and for IPV6.
#setlocal enableextensions enabledelayedexpansion
#echo off
for /f "tokens=2,3 delims=/ skip=16" %%i in (CheckPCs.bat) do (
set ipaddr=%%i
set state=shut down
set skip=0
for /f "tokens=5,6,7" %%a in ('ping -n 1 !ipaddr!') do (
if "x%%b"=="xunreachable." set skip=1
if !skip!==0 if "x%%a"=="xReceived" if "x%%c"=="x1," set state=still on
)
echo %%i %%j is !state!
)
pause
endlocal
rem /IP ADDRESS0/COMPUTER NAME0
rem /IP ADDRESS1/COMPUTER NAME1
Some notes if you do need to modify this code:
for /f "tokens=2,3 delims=/ skip=16" %%i in (CheckPCs.bat) do (
The for loop is processing CheckPCs.bat one line at a time. Skip is telling it to ignore the first 16 lines and go straight to the IP addresses. I'm using / as a delimiter for no particular reason but note that if you are pinging web addresses instead of IP, you'll have to change the delimiter. Something like the pipe character | would work. Of course since the line is commented out with rem, rem becomes the first token which means I only want to work with tokens 2 and 3 which will be the IP address and PC description. The latter field is optional, you'd just have to modify the code to remove it.
You should probably modify the terminology used for state and for echo %%i %%j is !state! so that the terminology is clear and concise for you. If you want to record the state somewhere, you can just feed it into a text file by appending >> file.txt to the line. You might want to also add a date/time in that case.
Lastly, something people with proper training in coding these might know, the way a batch for loop with tokens works (in simple terms) is each section of the text is split up at each delimiter, the default being space, and then it is assigned to a %% variable whose name begins at whichever character you specify and then increases up the ascii character list. This means if I specify to start at %%i, the next token will be %%j, then %%k and so on. If I used %%B, next would be %%C, then %%D etc. There can be a maximum of 31 tokens per another thread I read on the topic.
Following #Dan W answer:
#echo off
set Server=192.168.0.18
setlocal EnableDelayedExpansion
:checkhost
ping -n 1 %Server% | findstr TTL= >nul
if %errorlevel%==1 (
ping -n 1 %Server% | findstr "Reply from" | findstr "time" >nul
if !errorlevel!==1 (echo Network Asset %Server% Not Found & goto checkhost)
)
I've seen three results to a ping - The one we "want" where the IP replies, "Host Unreachable" and "timed out" (not sure of exact wording).
The first two return ERRORLEVEL of 0.
Timeout returns ERRORLEVEL of 1.
Are the other results and error levels that might be returned? (Besides using an invalid switch which returns the allowable switches and an errorlevel of 1.)
Apparently Host Unreachable can use one of the previously posted methods (although it's hard to figure out when someone replies which case they're writing code for) but does the timeout get returned in a similar manner that it can be parsed?
In general, how does one know what part of the results of the ping can be parsed? (Ie, why might Sent and/or Received and/or TTL be parseable, but not host unreachable?
Oh, and iSid, maybe there aren't many upvotes because the people that read this don't have enough points. So they get their question answered (or not) and leave.
I wasn't posting the above as an answer. It should have been a comment but I didn't see that choice.
#!/bin/bash
logPath="pinglog.txt"
while(true)
do
# refresh the timestamp before each ping attempt
theTime=$(date -Iseconds)
# refresh the ping variable
ping google.com -n 1
if [ $? -eq 0 ]
then
echo $theTime + '| connection is up' >> $logPath
else
echo $theTime + '| connection is down' >> $logPath
fi
Sleep 1
echo ' '
done