允许 Bat 脚本根据错误采取行动

Allow Bat Script to act upon error

我有一个 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

希望这是有道理的!

您要查找的命令是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

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