0
-TheGame/
     - Game files/
             -> file1.whatever
             -> file2.whatever
             -> file3.whatever
     -> Launcher.exe

 -TheGameModed/
     - Game files/
             -> file1.whatever        (the moded file)
     -> Launcher.exe                  (the moded launcher)

I made a mod on a game and i want to create an installer for people to play my game. In order to preventing backup problems (if the player want to revert to vanilla) i will put the mod folder aside the game folder. The mod folder contain only the "moded files" and in want to make a batch that will copy file from the game folder that are not already present in the destination (even if there are not the same)

Is this right :
xcopy "../TheGame" "../TheGameModed" /q /s /e

There is a documentation here but i didn't find what i'm looking for : https://www.computerhope.com/xcopyhlp.htm

I found only this :

/U Copies only files that already exist in destination. But i need the opposite (Copies only files that doesn't exist in destination)

P.S. : When the batch copy files, it ask me if i want to overwrite or not, and since i have only few filesit is not so hard to type n few times. But the mod will be deleted if someone type y (that would be bad) and maybe next mod will contain more files :[

Mister Aqua
  • 369
  • 2
  • 4
  • 16
  • You should use `/Y : Suppresses prompting to confirm you want to overwrite an existing destination file.` – Hackoo Dec 01 '17 at 20:38
  • `echo N|xcopy "../TheGame" "../TheGameModed" /q /s /e` – Squashman Dec 01 '17 at 20:43
  • @Hackoo - that will silently overwrite existing files - the exact opposit of what the querent desires. – Jeff Zeitlin Dec 01 '17 at 20:43
  • 1
    It should be noted that `XCOPY` is severely deprecated; you should look into using [`ROBOCOPY`](https://ss64.com/nt/robocopy.html), which has quite different options. – Jeff Zeitlin Dec 01 '17 at 20:46
  • Squashman, the echo N| skip "pause", how to do keep the "pause" ? – Mister Aqua Dec 01 '17 at 20:50
  • @MisterAqua, I have no idea what you are talking about. – Squashman Dec 01 '17 at 21:07
  • I mean that in my bat, if the copy ends succesfully, i will have : - echo Mod installed (new line) -pause – Mister Aqua Dec 01 '17 at 21:17
  • "pause" will wait for an input from the user. – Mister Aqua Dec 01 '17 at 21:20
  • @MisterAqua Piping an N to xcopy just answers the overwrite question for you. I have no idea what this has to do with you putting a pause in your code. If you have additional code that you do not understand, edit your question with the additional code and clarify what the problem is with the additional code. Please read the help for comments before you write another comment. Especially the section about notifying people. – Squashman Dec 01 '17 at 21:34
  • 1
    @MisterAqua - check `ROBOCOPY` [***File Selection Options*** examples](https://social.technet.microsoft.com/wiki/contents/articles/1073.robocopy-and-a-few-examples.aspx#File_Selection_Options) **ROBOCOPY** * * ***[file…] [options]*** robocopy "C:\Source Folder\*.*" "D:\Destination Folder\*.*" /xx `/xx Excludes extra files that exist in the destination directory` – Gourav Dec 02 '17 at 04:58

1 Answers1

1

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 in 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 /?
Mofi
  • 38,783
  • 14
  • 62
  • 115