Jump to content

Problem with Xcopy


Recommended Posts

I'm using a batch script to copy a backup of my local files to a shared drive on another computer in my network. I'm having a problem with Xcopy's error codes.


@echo off

:::::::::::::::::::::::::::::::
::::Script Global Variables::::
:::::::::::::::::::::::::::::::

SET localDir=C:\temp\
SET destDir=\\Autumn\documents\daniel\backup\
SET base=%computername%
SET backupName=%base%%date:~-4,4%%date:~-10,2%
::SET suffix=%date:~-4,4%%date:~-10,2%
SET errorLog=backup.log
SET numArchives=2

:::::::::::::::::::::::::::::::::::::::
::: Do not modify beyond this point::::
:::::::::::::::::::::::::::::::::::::::

echo Running system backup
echo.
echo System backup started %date:~-10,10% %time:~0,8%>>%errorLog%

::--Always local update & copy-- (Current version)::

echo Checking for existing backup...
xcopy %destDir%%backupName%.rar %localDir% /y 2>>%errorLog%
if errorlevel 4 (
if not exist %destDir% (
echo Error: Destination folder not found >> %errorLog%
goto error
)
echo None found. New file will be created.
)
echo Backing up local files...
"C:\Program Files\winrar\winrar" u -as -os -ep3 -r -t -ilog%errorLog%.winrar.log -inul -x@backup_excl.lst %localDir%%backupName% @backup.lst
echo Done.
echo Copying backup to remote folder...
xcopy %localDir%%backupName%.rar %destDir% /y 2>>%errorLog%
del %localDir%%backupName%.rar 2>>%errorLog%
FOR /f "skip=%numArchives% delims=" %%I IN ('DIR %destDir%%base%*.rar /A-D /B /O-D') DO del "%destDir%%%I" 2>>%errorLog%

echo Backup complete
goto error2
:error
echo Backup stopped unexpectedly! >> %errorLog%
echo Backup stopped unexpectedly! See error log for details.
PAUSE
:error2
echo System backup finished %date:~-10,10% %time:~0,8%>>%errorLog%
echo.>>%errorLog%

:: WinRAR <command> -<switch1> -<switchN> <archive> <files...> <@listfiles...> <path_to_extract\>

:: Winrar switches:
:: -ac handle "archive" file attributes
:: -as synchronize (delete files from archive)
:: -ep3 expand paths to full (including drive letter)
:: -ilog error log location
:: -inul disable error messages
:: -n include files/lists
:: -os important for NTFS file system backups
:: -r recurse subfolders
:: -u update files (same as 'u' command)
:: -t test files after archiving
:: -x exclude files/lists

Edited by MageLuingil
Link to comment
Share on other sites


The following are the 'exit codes' for Xcopy:

errorlevel 0 - Files were copied without error.

errorlevel 1 - No files were found to copy.

errorlevel 2 - The user pressed CTRL+C to terminate xcopy.

errorlevel 4 - Initialization error occurred. There is not enough memory or disk space, or you entered an invalid drive name or invalid syntax on the command line.

errorlevel 5 - Disk write error occurred.

Your problem is therefore not with the errorlevel returned it is with your script. There are better ways of checking for a valid drive etc. if you use an if exist statement or check for the drive first then you'd be more certain that the errorlevel of 4 referred to a lack of disk space or memory.

Also remember that for example if errorlevel 2 means if the errorlevel is 2 or greater, so if you wish to check against specific levels then either check them in reverse:

If Errorlevel 5 Goto DiskWrite
If Errorlevel 4 Goto NotEnough
If Errorlevel 2 Goto Abort
If Errorlevel 1 Goto NoSource

or use If %errorlevel% [GEQ|GTR|LSS|LEQ|EQU|NEQ] [0|1|2|3|4|5] Goto […]

Link to comment
Share on other sites

Also remember that for example if errorlevel 2 means if the errorlevel is 2 or greater

I didn't realize this, that's good to know. However, I still don't think that helps. The problem is, I'm currently using errorlevel 4 to check if there's already a backup file on the remote folder, so I can't use it for both that and checking if the drive is valid. I guess maybe I ought to change my first check to an if exist then ... maybe that'll be easier.

It just seemed to me it was returning an errorlevel 4 where it should have been returning a 1. I didn't know about if errorlevel n or greater thing, that helps. Thanks.

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...