Computing.Net > Forums > Programming > batch copy file on Ping Errorlevel=

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Click here to start participating now! Also, check out the New User Guide.

batch copy file on Ping Errorlevel=

Reply to Message Icon

Name: naimc
Date: February 26, 2009 at 09:37:18 Pacific
OS: Vista,2008
CPU/Ram: na
Product: N/a / NA
Subcategory: Batch
Comment:

Hi, I have a simple script that copies a file to each computer contained in txt file.

I'm using the For /f to read the computer names, then I ping the computer and only if it's on the network do I try to copy the file.

The problem is the that the Ping cmd does not return the proper errorlevel when used in the For loop. When I used ping form cmd line it does return the proper ErrorLevel. I thought the problem was caused by the lack of the DelayedExpansion but using !ErrorLevel! instead of %EverLevel% does not work.

The script works but it ties to copy files even on computer that are not pingable, I've set Xcopy to ignore error but it takes the script a long time to run as it times out on the machine that are not reachable.

Any suggestions ?

Naim


Setlocal enabledelayedexpansion

FOR /F "tokens=1" %%a IN (mtl_computers.txt) do (
@echo Copying file to computer %%a
ping -1 n %%a >nul
if ERRORLEVEL 0 (xcopy custom.prf "\\%%a\c$\Program Files\Microsoft Office\custom.prf" /U /C /Y) else (echo do nothing)
)

EndLocal
pause
exit




Response Number 1
Name: Mechanix2Go
Date: February 26, 2009 at 11:57:30 Pacific
+1
Reply:

You have a couple problems.

One is your usage.

If errorlevel 0

means 0 *OR MORE* so it will always resolve to TRUE and is therefore meaningless.

The other is a quirk of PING. It returms 1 only if it fails to resolve. Otherwise it returns 0 whether the IP is alive or fead.

===============================
@echo off & setLocal EnableDelayedExpansion

FOR /F "tokens=1" %%a IN (mtl_computers.txt) do (
ping -n 1 %%a | find "Reply" > nul
if not ERRORLEVEL 1 (
xcopy custom.prf "\\%%a\c$\Program Files\Microsoft Office\custom.prf" /U /C /Y
) else (
echo do nothing
)
)


=====================================
If at first you don't succeed, you're about average.

M2



Response Number 2
Name: naimc
Date: February 26, 2009 at 13:22:25 Pacific
+1
Reply:

Ok thank you M2,

My computer list came from AD so all of the account have valid DNS names, given that I changed the following :

ping -n 1 %%a | find "unreachable." > nul
if ERRORLEVEL 1 (

As I did some test and realized that the "Reply" keyword was found on Ping request for both reachable and unreachable computers, but "unreachable" gave me the computers I wanted to skip.

One thing I don't understand When I looked for example of code I found some people using if %ErrorLevel%==1 and others if ERRORLEVEL 1 .
What's the difference between both ?



Response Number 3
Name: Mechanix2Go
Date: February 26, 2009 at 21:02:05 Pacific
+1
Reply:

if %ErrorLevel%==1

mens EXACTLY 1

if ERRORLEVEL 1

means 1 OR MORE

=============================
@echo off & setLocal EnableDelayedExpansion

find /v aaa

echo EL is %errorlevel%

find /v aaa

if ERRORLEVEL 1 echo 1 or more

find /v aaa

if %ErrorLevel%==1 echo exactly 1


=====================================
If at first you don't succeed, you're about average.

M2



Reply to Message Icon

Related Posts

See More


Sorting similar text line... temporary set system loca...



Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Google Ads



Results for: batch copy file on Ping Errorlevel=

Batch copy files on txt file incl. subdir. www.computing.net/answers/programming/batch-copy-files-on-txt-file-incl-subdir/20053.html

copy file on filesize condition DOS www.computing.net/answers/programming/copy-file-on-filesize-condition-dos/13911.html

Win2k batch: copy only new files? www.computing.net/answers/programming/win2k-batch-copy-only-new-files/10386.html