I am generating a Windows Batch script to copy a file, only when the source is updated. If the file is updated, I want to delete another file.
The problem is that I can't find a simple way to trigger an event when the file update happens.
I thought of using %ERRORLEVEL%
but this gives 0 whether file is copied or not.
I also thought of saving the xcopy output to a text file and then processing the file but this just seems to a little impractical 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%
I explain the method used in command line posted by Aacini on 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 three environment variables defined above.
endlocal
First, it is very important on copying a single file with XCOPY that the destination directory path ends with a backslash as otherwise XCOPY would prompt if the destination is a directory or a file.
XCOPY, as used here, always outputs to handle STDOUT as last line an information message with the number of files being copied at beginning, even when nothing is copied or when an error occurred.
The command FOR executing XCOPY in a separate command process in background captures this output to handle STDOUT and processes it line by line.
Empty lines and lines starting with a semicolon are ignored with using the default options as used here with no eol=
option. The other lines are processed with splitting each line up into strings separated by spaces or horizontal tabs on using default delimiters as used here because of no delims=
option. Because of not using tokens=
option just the first space/tab separated string of each line is assigned to loop variable I
and the rest of the line is ignored.
The current value of the loop variable I
is assigned on each processed line to environment variable CopiedFilesCount
replacing its previous value.
The first space/tab separated string on last line output by XCOPY is the number of copied files which is here on copying just a single file either 0
or 1
. So finally after FOR loop execution finished the environment variable CopiedFilesCount
has either 0
or 1
as value.
The value is compared with GREATER THAN operator of command IF 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, execute there the following commands, and read the displayed help pages for each command, entirely and carefully.
del /?
echo /?
endlocal /?
for /?
if /?
rem /?
set /?
setlocal /?