【发布时间】:2017-01-25 12:39:56
【问题描述】:
我正在尝试创建一个仅在文件 Startapp.bat 退出时运行和复制内容的 .Bat 文件。这个 startapp 文件是用户在 github 上提交代码时创建的。
我有以下脚本。
taskkill /F /IM Webshop.exe
::%~dp0 means the current folder where this .bat is executed from
SET dest=%~dp0productionEnv
IF EXIST "%~dp0startApp.bat" (
if not exist "%dest%" mkdir "%dest%"
xcopy /Y /s "%~dp0Webshop\bin\Debug" "%dest%"
SET webDest="%dest%/webContent"
if not exist %webDest% mkdir %webDest%
xcopy /Y /s "%~dp0Webshop\webContent\web" %webDest%
copy /Y "%~dp0startApp.bat" "%dest%/startApp.bat"
START "" "%dest%/startApp.bat"
del "%~dp0startApp.bat"
echo "Deleted startApp.bat"
) ELSE (
echo "startApp.bat file not found"
)
但它不起作用。有时它会同时回显已删除的消息和未找到文件的消息,而这是不可能的。它应该回显这些消息中的任何一个,但不能同时回显这两个消息。这就是为什么会有 if else 的原因。
请帮忙!
【问题讨论】:
-
您在代码块中设置变量,文件路径在 Windows 中通常使用反斜杠
-
echo "%~dp0startApp.bat"的结果是什么? -
您在哪个环境下工作? Pre-XP OS 对“IF EXIST”有不同的处理方式。
-
关键是delayed expansion,因为您正在编写和读取同一个带括号的代码块中的变量。对于 Windows
cmd中的路径,请始终使用\作为分隔符。
标签: windows batch-file