当我在批处理文件中运行下面的代码时,它只会执行第一行而不关闭命令窗口

时间:2016-01-25 19:18:42

标签: batch-file

最初没有在命令前面调用,命令窗口也不会关闭,而是在论坛上读取可能使它工作的地方。现在命令窗口关闭,但它不会复制文件,文件确实存在。在我在每个命令前面调用它之前,它只会执行第一行而不会关闭命令窗口。不知道下一步该尝试什么。感谢

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")

2 个答案:

答案 0 :(得分:0)

您对Call命令的使用不正确。它只接受文件路径参数,并用于调用其他命令/批处理文件或同一命令文件中的子程序/函数:

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

      CALL :label [parameters]

      CALL internal_cmd

由于您在不同时间执行相同的过程,因此应使用两个批处理文件:一个控制流,另一个调用另一个批处理文件:

<强> 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
)

正如您所看到的,mainBatch.cmd文件调用第二个批处理文件,该文件执行主处理。这是使用Call命令

的正确方法

答案 1 :(得分:0)

如前所述,在CALL命令前添加IF不是答案。

至于为什么你的程序挂起,我不确定。我怀疑它可能是一个相当大的文件你正在复制(和/或你的驱动器特别慢),它只需要一些时间来完成复制,然后再转到下一行。您可以通过在命令提示符下运行IF ... COPY ...命令并计算完成所需的时间来测试此操作。

相关问题