0

Originally did not have call in front of the command and the command window would not close, but read on the forum where that might make it work. Now the command window closes, but it doesn't copy the files and the files do exist. Before i put call in front of each command it would execute the first line only and would not close the command window. Not sure what to try next. Thanks

call if exist "e:\DiDiver\models\all_gl_post_sum.mdl" (copy /y "e:\DiDiver\models\all_gl_post_sum.mdl" "e:\DiDiver\models\all_gl_post_sum_0ld.mdl")
call if exist "e:\DiDiver\models\all_gl_post_sum.md0" (copy /y "e:\DiDiver\models\all_gl_post_sum.md0" "e:\DiDiver\models\all_gl_post_sum_0ld.md0")
call if exist "e:\DiDiver\models\all_gl_post_sum.md1" (copy /y "e:\DiDiver\models\all_gl_post_sum.md1" "e:\DiDiver\models\all_gl_post_sum_0ld.md1")
call if exist "e:\DiDiver\models\all_gl_post_sum.md2" (copy /y "e:\DiDiver\models\all_gl_post_sum.md2" "e:\DiDiver\models\all_gl_post_sum_0ld.md2")
call if exist "e:\DiDiver\models\all_gl_post_sum.md3" (copy /y "e:\DiDiver\models\all_gl_post_sum.md3" "e:\DiDiver\models\all_gl_post_sum_0ld.md3")
call if exist "e:\DiDiver\models\all_gl_post_sum.md4" (copy /y "e:\DiDiver\models\all_gl_post_sum.md4" "e:\DiDiver\models\all_gl_post_sum_0ld.md4")
call if exist "e:\DiDiver\models\all_gl_post_sum.md5" (copy /y "e:\DiDiver\models\all_gl_post_sum.md5" "e:\DiDiver\models\all_gl_post_sum_0ld.md5")
call if exist "e:\DiDiver\models\all_gl_post_sum.md6" (copy /y "e:\DiDiver\models\all_gl_post_sum.md6" "e:\DiDiver\models\all_gl_post_sum_0ld.md6")
call if exist "e:\DiDiver\models\all_gl_post_sum.md7" (copy /y "e:\DiDiver\models\all_gl_post_sum.md7" "e:\DiDiver\models\all_gl_post_sum_0ld.md7")
call if exist "e:\DiDiver\models\all_gl_post_sum.md8" (copy /y "e:\DiDiver\models\all_gl_post_sum.md8" "e:\DiDiver\models\all_gl_post_sum_0ld.md8")
SomethingDark
  • 13,229
  • 5
  • 50
  • 55
John L
  • 3
  • 1
  • 1
    Why are you using a `CALL` in front of all those `IF` commands? – Squashman Jan 25 '16 at 19:24
  • you can't [`CALL IF`](http://stackoverflow.com/questions/19445153/why-i-cant-call-if-and-for-neither-in-batch-nor-in-the-cmd) – npocmaka Jan 25 '16 at 19:33
  • Because i search on this forum and saw where that was recommended on a similar situation. – John L Jan 25 '16 at 19:33
  • @JohnL, I do not see any reason why your code will not work without the CALL. – Squashman Jan 25 '16 at 19:39
  • I don't either, however when i run it, it executes the first line and then the command window hangs open and i have to close it. – John L Jan 25 '16 at 19:44
  • @JohnL, could you please provide a link to that recommended solution you mentioned? – aschipfl Jan 25 '16 at 19:53
  • Here ya go. http://stackoverflow.com/questions/4036754/why-does-only-the-first-line-of-this-windows-batch-file-execute-but-all-three-li – John L Jan 25 '16 at 19:57
  • @JohnL, the reason for that `CALL` in that instance is because they are calling out to another batch file. Is this your entire batch file? What is the name of your batch file? – Squashman Jan 25 '16 at 20:24
  • Yes this is the entire batch and it's call all_gl_post_sum_test.bat. – John L Jan 25 '16 at 21:08

2 Answers2

0

Your use of the Call command is incorrect. It takes a file path parameter only, and is used to call other command/batch files, or subroutines/functions in the same command file:

Syntax
      CALL [drive:][path]filename [parameters]

      CALL :label [parameters]

      CALL internal_cmd

Since you are doing the same process various times, you should use two batch files: one two control the flow, and call the other that does the process:

MainBatch.cmd:

for %k in (1 2 3 4 5 6 7 8) do (
    call copyFiles.cmd %k
)

CopyFiles.cmd:

set rootPath=e:\DiDiver\models\all_gl_post_sum
if exist "%rootPath%.md%1" (
    copy /y "%rootPath%.md%1" "%rootPath%_0ld.md%1"
    echo Copied %rootPath%.md%1
)

As you can see the mainBatch.cmd file calls the second batch file, which does the main processing. That is the correct way to use the Call command

Rodrigo Murillo
  • 13,080
  • 2
  • 29
  • 50
0

As already stated, adding CALL in front of your IF commands isn't the answer.

As to why your program hangs, I'm not sure. I suspect it may be a fairly large file you're copying (and/or your drives are particularly slow), and it just needs to take some time to complete the copy before moving on to the next line. You can test this by running your IF ... COPY ... command at a command prompt and timing how long it takes to complete.

Wes Larson
  • 1,042
  • 8
  • 17
  • You are correct. The files are large and I was being impatient. Using Windows Explorer I could see that the first file had been copied, but when i checked the batch file looked like it was stuck. Sorry for any inconvenience I've caused anyone. – John L Jan 25 '16 at 21:39