2016-11-23 27 views
-2

請在下面找到一個程序,將文件夾從一個位置複製粘貼到另一個位置 試圖執行它時,錯誤爲:(此時是意外(此時出乎意料)< - 是我在運行bathc腳本時遇到的錯誤

@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

再一次:[延遲擴展](http://ss64.com/nt/delayedexpansion.html)... – aschipfl

+0

這似乎不是主要問題...閱讀輸出時,它似乎是最重要的聲明已經造成了車禍還是我錯了? – geisterfurz007

回答

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 
) 
0

儘管有括號匹配數量,我認爲你已經錯過了兩個括號,一個開口和一個收盤。

開始通過它來改變你,如果塊:

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 
    ) 
) 

,然後他們應該是適當的平衡。

相關問題