Receive notification from xcopy if file copy

I am creating a Windows batch script to copy the file only when the source is updated. If the file is updated, I want to delete another file.

The problem is that I cannot find an easy way to trigger an event when the file is updated.

I thought about using it %ERRORLEVEL%

, but this gives 0 whether the file is copied or not.

I also thought about saving the xcopy output to a text file and then processing the file, but does that just seem unattractive for such a simple task?

Any other ideas?

Code so far

SET SOURCEFILE=%CD%\source.txt
SET DELFILE=%CD%\toDelete.txt
SET DESTDIR=%WINDIR%\Deployment\ 

xcopy "%SOURCEFILE%" "%DESTDIR%" /c /d /q /y

REM IF File is updated, delete %DELFILE%

      

+3


source to share


1 answer


Explanation of the method used in the Aacini command line for the entire batch code:

@echo off
setlocal EnableExtensions DisableDelayedExpansion

set "SourceFile=source.txt"
set "DeleteFile=toDelete.txt"
set "DestinationDirectory=%SystemRoot%\Deployment\"

for /F %%I in ('%SystemRoot%\System32\xcopy.exe "%SourceFile%" "%DestinationDirectory%" /C /D /Q /Y 2^>nul') do set "CopiedFilesCount=%%I"

if %CopiedFilesCount% GTR 0 del "%DeleteFile%"

rem Add here more command lines using one of the 3 environment variables define above.
endlocal

      

First, it is very important when copying a single file with XCOPY that the destination directory path ends with a backslash, otherwise XCOPY will ask if the destination is a directory or a file.

XCOPY , which is used here, always outputs an informational message for STDOUT processing as the last line with the number of copied files at the start, even when nothing is copied or when an error occurs.

The FOR command , which runs XCOPY in a separate command process in the background, captures this output for STDOUT processing and processes it line by line.

Blank lines and lines starting with a semicolon are ignored using the default options used here, no option eol=

. Other lines are processed by breaking each line into lines separated by spaces or horizontal tabs, using the default delimiters used here, due to the lack of an option delims=

. Due to not using the option tokens=

, only the first line separated by space / tab of each line is assigned to the loop variable I

and the rest of the line is ignored.



The current value of the loop variable is I

assigned on each processed line to an environment variable CopiedFilesCount

that replaces its previous value.

The first line, separated by space / tab, on the last line output by XCOPY is the number of copied files that is here when copying only one file 0

, either or 1

. So, after the FOR loop finishes executing, the environment variable CopiedFilesCount

has a value 0

or 1

as a value.

The value is compared with the GREATER THAN statement of the IF command to determine if the file was copied, in which case the other file is deleted.

To understand the commands used and how they work, open a Command Prompt window, run the following commands there, and carefully read all the help pages displayed for each command.

  • del /?

  • echo /?

  • endlocal /?

  • for /?

  • if /?

  • rem /?

  • set /?

  • setlocal /?

+1


source







All Articles