-2

Please find below a program to copy-paste the folder from one location to another. While trying to execute it I am getting the error as : ( was unexpected at this time.

@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.

Please suggest what modifications are required!!!

Shreyash
  • 1
  • 2
  • 2
    Once again: [delayed expansion](http://ss64.com/nt/delayedexpansion.html)... – aschipfl Nov 23 '16 at 12:36
  • This does not seem to be the primary problem... When reading the Output it seems the topmost statement is causing the crash already or am I wrong? – geisterfurz007 Nov 23 '16 at 13:11

2 Answers2

0

Command Line is often messing around with special characters, such as brackets, quotes etc. And it is maybe a reason, if it is not set everywhere, where cmd line expects them...

I can not find an issue in reading this, so try to clarify brackets for cmd-line.

So try to

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

if it does not work try:

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

Despite having a matching number of parentheses, I think that you've missed two parentheses, one opening and one closing.

Start by changing your if block from:

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)

to:

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

and they should then be properly balanced.

Compo
  • 30,301
  • 4
  • 20
  • 32