Skip to content
View in the app

A better way to browse. Learn more.

MSFN

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Problem with Xcopy

Featured Replies

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

I checked levels 1-5 and they all returned true
I suppose you should check the levels 5-1.

if errorlevel n is true for n and higher

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 […]

  • Author
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.

Create an account or sign in to comment

Recently Browsing 0

  • No registered users viewing this page.

Account

Navigation

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.