1

In folder C:\A i have afile.h afile.cpp bfile.h bfile.cpp and some other .h and .cpp files (windows 10)

I want to copy only afile.h afile.cpp bfile.h bfile.cpp to C:\B folder and replace them if files in folder A are newer than folder B

I tried below but did not work, bat run but nothing happens

my copybatch.bat file

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64 
fastddsgen.bat -replace -example CMake aac.idl 
xcopy /y /d C:\A\afile.h C:\B
xcopy /y /d C:\A\afile.cpp C:\B
xcopy /y /d C:\A\bfile.h C:\B
xcopy /y /d C:\A\bfile.cpp C:\B
kingmihi
  • 47
  • 1
  • 8
  • 4
    what about using the same command to run your .bat file on line two as you used on line one! `call fastddsgen.bat …` – Compo May 24 '21 at 11:49
  • Please read my answer on [How to call a batch file that is one level up from the current directory?](https://stackoverflow.com/a/24725044/3074564) It explains the difference between `fastddsgen.bat` and `call fastddsgen.bat`. The four `xcopy` lines should be replaced by `%SystemRoot%\System32\robocopy.exe "C:\A" "C:\B" "afile.h" "afile.cpp" "bfile.h" "bfile.cpp" /XO /R:3 /W:2 /NDL /NFL /NJH /NJS`. Run `robocopy /?` in a [command prompt](https://www.howtogeek.com/235101/) for help on the options of command __ROBOCOPY__. – Mofi May 24 '21 at 14:34
  • BTW: It would be better to reference the batch file `fastddsgen.bat` with full qualified file name instead of just file name and expecting that the current directory on execution of `copybatch.bat` is the directory containing `fastddsgen.bat`. This expectation can be quite easily wrong. There can be used `call "%~dp0fastddsgen.bat"` if the batch file `fastddsgen.bat` is in same directory as `copybatch.bat`. `%~dp0` (drive+path of argument 0) expands to full path of currently processed batch file always ending with a backslash. – Mofi May 24 '21 at 14:39

2 Answers2

1

I used call for 2nd command as well

call "C:\Program Files (x86)\Microsoft Visual Studio\2017\Community\VC\Auxiliary\Build\vcvarsall.bat" x86_amd64 
call fastddsgen.bat -replace -example CMake aac.idl
xcopy /y /d C:\A\afile.h C:\B
xcopy /y /d C:\A\afile.cpp C:\B
xcopy /y /d C:\A\bfile.h C:\B
xcopy /y /d C:\A\bfile.cpp C:\B

Reason to use CALL --> When not using CALL, the current batch file stops and the called batch file starts executing.

kingmihi
  • 47
  • 1
  • 8
0

What's the meaning of /d in your xcopy command? You might think that you are specifying that the destination is to be a directory, but in fact this is to be used for date related things.

The real problem here might be that your computer does not understand whether C:\B is a directory or a file, causing the problem.

Therefore I'd advise you to follow my answer from this other question, and use the following commands:

xcopy /y C:\A\afile.h C:\B\
xcopy /y C:\A\afile.cpp C:\B\
xcopy /y C:\A\bfile.h C:\B\
xcopy /y C:\A\bfile.cpp C:\B\

Good luck

Dominique
  • 16,450
  • 15
  • 56
  • 112
  • 1
    `xcopy /?` says about `/d`: `If no date is given, copies only those files whose source time is newer than the destination time`, so that's fine. Destination folder without trailing backspace is ok too (if the folder exists) (although the backslash can be considered "best practice"). The real problem here is, that the `xcopy` commands are not even executed (due to not `call`ing a batchfile before, as Compo and Mofi already commented) – Stephan May 24 '21 at 15:48
  • ??? So you launch a command, and when that command is "finished", the next ones don't get executed? It looks to me like there's a serious problem in your first command then. This can be "solved" (or worked around) using `start` instead of `call`: the `start` causes another prompt to be opened and the next commands are executed anyway, even if something goes wrong in your first command. I'm afraid you need to focus more on why your first command does not finish. – Dominique May 24 '21 at 16:05
  • 1
    a launched *command* isn't a problem at all, a batchfile **is**, if not `call`ed (as I said) (or `start`ed). When you just execute `script.bat` (without `call` or `start`), you transfer control to it and it never returns to execute the following command of the first script. – Stephan May 24 '21 at 16:42