允许Bat Script在出错时执行操作

时间:2015-12-16 02:12:50

标签: batch-file error-handling cmd xcopy robocopy

我有一个bat文件,可根据用户输入创建文件夹和子文件夹。我想代码告诉我它正在尝试创建的文件夹已经存在,而且(如果可能的话),如果文件夹已经存在,我希望代码将子文件夹复制到预先存在的文件夹中。

我的代码 -

@echo off
echo.
:EnterName
set "dest=""
set /P "dest=Enter Name: "
set "dest=%dest:"=%"
if "%dest%" == "" cls & goto EnterName

set "findest=Z:\ProjectIT\copy\%dest%"

robocopy Z:\ProjectIT\copy\xcopy "%findest%" /e /NFL /NDL /NJH /NJS

echo Construction folder has been created for "%dest%"
echo.

pause

希望有道理!

1 个答案:

答案 0 :(得分:0)

您要查找的命令是if exist

@echo off
echo.
:EnterName
set "dest=""
set /P "dest=Enter Name: "
set "dest=%dest:"=%"
if "%dest%"=="" cls & goto EnterName

if exist %findest% (
    echo This folder already exists!
    echo All subfolders will be copied to the existing folder.
)

set "findest=Z:\ProjectIT\copy\%dest%"

robocopy Z:\ProjectIT\copy\xcopy "%findest%" /e /NFL /NDL /NJH /NJS

echo Construction folder has been created for "%dest%"
echo.

pause

您无需修改​​代码即可复制子文件夹。如果该文件夹存在,则所有内容都将被复制到其中。