(此时出乎意料。< - 是我在运行bathc脚本时遇到的错误

时间:2016-11-23 12:00:25

标签: batch-file windows-scripting

请在下面找到将文件夹从一个位置复制粘贴到另一个位置的程序。 在尝试执行它时,我收到错误:(此时出乎意料。

@echo off

set /p SrcPath= Source file is 
echo %SrcPath%

set /p DestPath= Destination file is 
echo %DestPath%

echo Checking if the package with the same name exists in the Destination Path

if exist %DestPath% (  
                       echo Folder exists
                       echo Do you want to rename the existing folder Y/N
                       set /p Answer=
                       echo %Answer%
                       if %Answer% == y ( echo please suggest what suffix you would like to append e.g. _old, _bkp etc 
                                           set /p Suffix=
                                           move %DestPath% %DestPath%%Suffix%
                                           goto :CopyPackage )


                       if %Answer% == n  echo "please decide what to do" 
                    ) else ( echo "folder doesn't exist"
                                goto :CopyPackage) 





:CopyPackage
ROBOCOPY /s /e %SrcPath% %DestPath%



Output on cmd prompt:
C:\Users\shreyash>Z:\Dev\FolderEx.bat
Source file is C:\New
C:\New
Destination file is C:\New1
C:\New1
Checking if the package with the same name exists in the Destination Path
( was unexpected at this time.
C:\Users\shreyash>Z:\Dev\FolderEx.bat
Source file is C:\New
C:\New
Destination file is C:\New1
C:\New1
Checking if the package with the same name exists in the Destination Path
( was unexpected at this time.
C:\Users\shreyash>Z:\Dev\FolderEx.bat
Source file is "C:\New"
"C:\New"
Destination file is "C:\New1"
"C:\New1"
Checking if the package with the same name exists in the Destination Path
( was unexpected at this time.

请建议需要进行哪些修改!!!

2 个答案:

答案 0 :(得分:0)

命令行经常乱用特殊字符,例如括号,引号等。这可能是一个原因,如果它没有设置到任何地方,cmd行期望它们......

我在阅读本文时找不到问题,因此请尝试澄清cmd-line的括号。

所以尝试

  if %Answer% == n  ( echo "please decide what to do" )

如果不起作用,请尝试:

if %Answer% == y ( 
echo please suggest what suffix you would like to append e.g. _old, _bkp etc  
set /p Suffix= 
move %DestPath% %DestPath%%Suffix% 
goto :CopyPackage )
else ( echo "please decide what to do" ) 
) else ( 
echo "folder doesn't exist" goto :CopyPackage
) 

答案 1 :(得分:0)

尽管有一个匹配数量的括号,我认为你错过了两个括号,一个开头,一个关闭。

首先从以下位置更改if块:

if exist %DestPath% (  
                       echo Folder exists
                       echo Do you want to rename the existing folder Y/N
                       set /p Answer=
                       echo %Answer%
                       if %Answer% == y ( echo please suggest what suffix you would like to append e.g. _old, _bkp etc 
                                           set /p Suffix=
                                           move %DestPath% %DestPath%%Suffix%
                                           goto :CopyPackage )


                       if %Answer% == n  echo "please decide what to do" 
                    ) else ( echo "folder doesn't exist"
                                goto :CopyPackage)

为:

if exist "%DestPath%\" (  
    echo Folder exists
    echo Do you want to rename the existing folder Y/N
    set /p Answer=
    echo %Answer%
    if %Answer%==y (
        echo please suggest what suffix you would like to append e.g. _old, _bkp etc 
        set /p Suffix=
        move "%DestPath%" "%DestPath%%Suffix%"
        goto :CopyPackage
    )
    if %Answer%==n (
        echo "please decide what to do" 
    ) else (
        echo "folder doesn't exist"
        goto :CopyPackage
    )
)

然后应该适当平衡它们。