[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

check rerturn code/error code from copy command?

Posted on 2006-06-08
6
Medium Priority
?
490 Views
Last Modified: 2008-03-10
Hello,

I'm copying a file to a folder and I want to ensure that the file was copied correctly.  In routine #1 below I copy the file, then check if the file exist in the remote folder.  This works fine, but it is flawed in the case where another application might remove the file from the remote location before I get to check if it was copied successfully.

What I'd really like to do is check the return code/error code of the copy command after I copy the file to the remote folder.  Can someone show me how?

Many thanks,
WL.

# routine #1
for /f "delims=" %%a in ('dir /b *.abc') do (
    copy "%%a" "%remote%%%a"
    if not exist "%remote%%%a" (
      ERROR ROUTINE
    ) else (
      SUCCESS ROUTINE
    )    
)

:: routine #2
for /f "delims=" %%a in ('dir /b *.abc') do (
    copy "%%a" "%remote%%%a"
    if RETURN CODE FROM COPY = SUCCESS (
      ERROR ROUTINE
    ) else (
      SUCCESS ROUTINE
    )    
)
0
Comment
Question by:windylad
  • 3
  • 2
6 Comments
 
LVL 10

Accepted Solution

by:
For-Soft earned 600 total points
ID: 16860843
What you are looking for is ERRORLEVEL code.


    if ERRORLEVEL 1 ERROR ROUTINE

should do the trick.
0
 
LVL 10

Expert Comment

by:For-Soft
ID: 16860855
From Windows 2000 documentation:

errorlevel number

Specifies a true condition only if the previous program run by Cmd.exe returned an exit code equal to or greater than number.
0
 

Author Comment

by:windylad
ID: 16861973
hello again,

I've tried using if %errorlevel% neq 0, but it looks like errorlevel is not getting set for me in this batch file (I'm including the whole scipt below - script #1).  The thing is, if I create a more basic script (see script #2) errorlevel is getting set as expected and the batch runs as expected.  

To generate an error on the copy command, I'm setting the permissions of the foler I'm trying to write to as DENY.

Any help is appreciated,
WL

::
:: script #1
::
@echo off
set home=C:\test\
set remote=C:\test\remote\
set uploaded=C:\test\uploaded\
set log=C:\test\upload.log

echo %date% %time% START >> %log%

:: test that dirs exists/are online
if not exist %home% goto home_dir_dne
if not exist %remote% goto remote_dir_dne
if not exist %uploaded% goto uploaded_dir_dne

cd %home%

:: check if the file has been uploaded before, if not upload, then copy to uploaded folder
for /f "delims=" %%a in ('dir /b *.abc') do (
  if not exist "%uploaded%%%a" (
    copy "%%a" "%remote%%%a"
    if %errorlevel% neq 0 (
      echo %date% %time% ERROR %%a not copied to %remote% >> %log%
    ) else (
      echo %date% %time% COPY %%a copied to %remote% >> %log%
    )    
    copy "%%a" "%uploaded%%%a"
    if %errorlevel% neq 0 (
      echo %date% %time% ERROR %%a not copied to %uploaded% >> %log%
    ) else (
      echo %date% %time% COPY %%a copied to %uploaded% >> %log%
    )    
  )
)
goto end

:home_dir_dne
echo %date% %time% ERROR %home% does not exist/is offline >> %log%
goto end

:remote_dir_dne
echo %date% %time% ERROR %remote% does not exist/is offline >> %log%
goto end

:uploaded_dir_dne
echo %date% %time% ERROR %uploaded% does not exist/is offline >> %log%
goto end

:end
echo %date% %time% FINISH >> %log%

@echo on



::
:: script #2
::
@echo off

cd C:\test\
copy file1.abc C:\test\remote\file1.abc

if %errorlevel% neq 0 (
 echo error
) else (
 echo success
)

pause
@echo on
0
NEW Veeam Agent for Microsoft Windows

Backup and recover physical and cloud-based servers and workstations, as well as endpoint devices that belong to remote users. Avoid downtime and data loss quickly and easily for Windows-based physical or public cloud-based workloads!

 
LVL 10

Expert Comment

by:GuruGary
ID: 16863663
Insetad of:
if %errorlevel% neq 0

Try:
if errorlevel 1

If the errorlevel is 1 or higher, the IF will be TRUE
0
 
LVL 10

Expert Comment

by:For-Soft
ID: 16864188
Right.
Errorlevel is not just another system variable. It works a bit different way.
0
 

Author Comment

by:windylad
ID: 16868672
Thanks all, I had to give the points to the For-Soft.  GuruGary, thanks for pointing me back on track, I though it would be better to test if errorlevel was not 0.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

One of my most closely kept secrets is revealed in this discussion How to output text on the same line This question was recently posted in EE by Simon336697 (http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_2459…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question