如何在批处理文件使用时从UNC路径更新批处理文件

时间:2014-07-23 16:34:23

标签: windows batch-file cmd xcopy

我有一个批处理文件(batch1.bat),它检查UNC共享上的更新版本,然后运行一些命令。但是,问题是我想更新共享上的批处理文件,以便将来的更新在运行批处理时应用于所有用户。问题是批处理文件在检查时没有更新为运行。

示例:

mkdir C:\program\Tools
xcopy /D /Y /e "\\UNC_Share\program\*.*" "C:\program\"

将批处理文件复制到Tools文件夹。

我创建了一个名为update.bat的新文件,它调用上面的xcopy命令,然后打开原始批处理文件(batch1.bat)来完成它的工作。但是,无法弄清楚如何让batch1.bat调用更新,关闭并重新打开批处理而不在循环中调用更新。

  1. 用户打开(运行)batch1.bat。
  2. Batch1.bat调用update.bat并关闭batch1.bat,以便它可以从UNC共享更新。
  3. 更新完成后,update.bat调用batch1.bat,然后运行工具。
  4. UNC上的文件共享

    \\UNC_Share\program\
        batch1.bat
        update.bat
    \\UNC_Share\program\Tools
        some .exe and .dll files
    

1 个答案:

答案 0 :(得分:0)

这没有问题,因为在不使用命令call的情况下从批处理文件中运行批处理文件会导致停止处理当前批处理文件并继续处理已启动的批处理文件。

<强> batch1.bat:

@echo off
rem Set directory of batch file as current working directory.
cd /D "%~dp0"

rem Check if update was already done before. If the environment variable
rem "UpdateDone" defined in batch file update.bat not set, run the update
rem if the batch file for updating exists at all. Pass as parameter the
rem name of this batch file to restart it after the update.
if "%UpdateDone%"=="" if exist "\\UNC_Share\program\update.bat" "\\UNC_Share\program\update.bat" %0

rem Remove the environment variable "UpdateDone" after update processed.
set UpdateDone=

rem Put here any commands which should be executed by this batch file.
echo Batch1 is processing, please wait ...

<强> update.bat:

@echo off
echo Updating tools, please wait ...

rem Target directory automatically created because of switch /I if not exist.
xcopy /D /Y /E /I /Q "\\UNC_Share\program\*" "C:\program\Tools\" 1>nul

rem If there was no argument passed on running this batch file, there
rem is nothing else to do. Otherwise it is expected that the first
rem argument is the name of the batch file which started this batch
rem file for updating the tools and therefore start this batch file
rem now again after setting the environment variable "UpdateDone".
if not "%~1"=="" (
   set UpdateDone=yes
   %1
)