Perhaps ROBOCOPY can't be used because the game updating batch file should work also on Windows XP. In this case the following batch file could be perhaps used working on Windows NT4 and all later Windows versions:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
pushd "%~dp0"
for /F "delims=" %%I in ('dir "TheGame\*" /A-D /B /S 2^>nul') do call :CopyFile "%%I"
popd
endlocal
exit
:CopyFile
set "SourcePath=%~dp1"
set "TargetPath=%SourcePath:\TheGame\=\TheGameModed\%"
if not exist "%TargetPath%%~nx1" %SystemRoot%\System32\xcopy.exe "%~1" "%TargetPath%" /C /I /Q >nul
goto :EOF
The batch file first creates a local environment.
Next it pushes path of current directory on stack and sets the directory of the batch file as current directory. It is expected by this batch file being stored in the directory containing the subdirectories TheGame
and TheGameModed
.
Then command DIR is executed to output
- the names of just all files because of
/A-D
(attribute not directory)
- with name of file only because of
/B
(bare format)
- in specified directory
TheGame
and all subdirectories because of /S
- and with full path also because of
/S
.
This DIR command line is executed in a separate command process started by FOR in background with cmd.exe /C
which captures everything written by this command process respectively by DIR to handle STDOUT.
Read the Microsoft article about Using Command Redirection Operators for an explanation of 2>nul
. The redirection operator >
must be escaped with caret character ^
on FOR command line to be interpreted as literal character when Windows command interpreter processes this command line before executing command FOR which executes DIR command line with using a separate command process started in background.
The FOR option delims=
disables the standard behavior of splitting up each non empty line not starting with a semicolon into strings using space/tab as delimiter. In other words each file name with file extension and full path is assigned to loop variable I
.
The name of each file with file extension and full path is passed to a subroutine called CopyFile
.
The subroutine first assigns just path of source file found in TheGame
directory tree to environment variable SourcePath
. Next a string substitution is used to replace in this path TheGame
by TheGameModed
with including the directory separators on both side for more accuracy.
After having target path for current file in TheGame
directory tree it is checked next if a file with that name in that path exists already in TheGameModed
directory tree.
If the file does not exist, command XCOPY is used to copy this single file to TheGameModed
with automatically creating the entire directory tree if that is necessary. This directory creation feature of XCOPY is the main reason for using XCOPY instead of COPY.
After processing all files in TheGame
directory tree, the initial current directory is restored from stack as well as the initial environment before exiting current command process independent on calling hierarchy and how the command process was started initially.
The commands POPD and ENDLOCAL would not be really necessary with exit
being the next line. I recommend usually to use exit /B
or goto :EOF
instead of EXIT, but goto :EOF fails if command extensions are not enabled and we can't be 100% sure that the command extensions are enabled on starting the batch file although by default command extensions are enabled on Windows.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
call /?
echo /?
endlocal /?
exit /?
goto /?
if /?
popd /?
pushd /?
set /?
setlocal /?
xcopy /?