DOS - XCopy Copy Tips

This section provides answers to common file copy questions.  Click here to view the XCOPY command reference.

 

1      Copy new and newer files only. 1

2      Copy newer files only, destination must exist. 1

3      Copy new files only. 1

4      Additional Task for each File to be copied. 1

5      Preserving the Directory Structure during Copy. 1

6      Test for Open File. 2

 

 

 

1        Copy new and newer files only

Description

XCOPY’s /D option makes sure that only new and newer files will be copied.

source can be a file mask e.g.: *.*, my*.*, my*.log, directory\*.*

destination can be a directory e.g.: ., directory

Code

xcopy "%source%" "%destination%" /D /Y

2        Copy newer files only, destination must exist

Description

XCOPY’s /U option makes sure that files will only be copied as part of an update.

source can be a file mask e.g.: *.*, my*.*, my*.log, directory\*.*

destination can be a directory e.g.: ., directory

Code

xcopy "%source%" "%destination%" /D /U /Y

3        Copy new files only

Description

XCOPY’s /L option will not perform the actual copy but will list the files that would be copied without the /L option.  The resulting file list can be used to perform further checks before the actual copy.  In this example a FOR command parses the resulting file list and runs an additional if not exist check in order to make sure that no file gets overwritten.

source can be a file mask e.g.: *.*, my*.*, my*.log, directory\*.*

destination can be a directory e.g.: ., directory

Code

for /f %%a in ('xcopy "%source%" "%destination%" /L /Y') do (

    if not exist "%destination%.\%%~nxa" xcopy "%%a" "%destination%" /Y

)

4        Additional Task for each File to be copied

Description

Having the ability to retrieve a list of files that would be copied without actually performing the copy allows executing additional tasks for each file listed.  Performing the copy itself becomes optional.

The example shown here outputs a nice message for each file being copied and removes all text file from the destination folder that have the same name as the file to be copied but the extension .txt.

source can be a file mask e.g.: *.*, my*.*, my*.log, directory\*.*

destination can be a directory e.g.: ., directory

Code

for /f %%a in ('xcopy "%source%" "%destination%" /L /Y') do (

    echo.Copying file '%%a' and removing corresponding .txt files

    if exist %%~dpna.txt del %%~dpna.txt

    xcopy "%%a" "%destination%" /Y

)

5        Preserving the Directory Structure during Copy

Description

To copy files for backup purpose it is often necessarily to preserve the directory structure in the backup location.  I.e. In order to backup all *.pst files under the “c:\Documents and Settings” directory and copy them to “c:\pstbackup\ Documents and Settings\...” the code shown here can be used.

Note: You may think that a simple “xcopy "C:\Documents and Settings\*.pst" "c:\pstbackup\" /Y/U/S” does the job, but testing showed that it will always returns ”File not found - *.pst”.  So here a solution that does work:

Code

set sourcedir=c:\Documents and Settings\*.pst

set backupdir=c:\pstbackup

for /f "tokens=*" %%a in ('dir "%sourcedir%" /s/b') do (

    xcopy "%%a" "%backupdir%.%%~pa" /Y/U

)

6        Test for Open File

Description

The copy command can be used to check wither a file is open without modifying it.

 

How it works:

copy /-y will causes prompting to confirm you want to overwrite the file.
echo.N will abort the copy operation so that the copy really only checks whether the file is open.
>NUL will make sure everything happens quietly without showing anything on the screen.

If the file (to be copied) is not open then the copy operation will be successfully aborted by piping the 'N' into the conformation. Since such abort indicates success, the code behind the && will be executed.

 

If the file is open then the copy operation will fail and the code behind the || will be executed.

Code

echo.N|copy /-y NUL "%filename%">NUL&&(

    echo.%filename% is free!

    rem leave this rem here at the block end!!!

) || (

    echo.%filename% is in use!

)