Access file path containing paranthesis

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
g3kr
Posts: 3
Joined: 01 Sep 2015 21:31

Access file path containing paranthesis

#1 Post by g3kr » 01 Sep 2015 21:37

Hi,

I have a batch script which accepts optional named parameters. When i run the batch script from cmd with parameter(file path with a folder name in this format : XYZ(1234)) it throws an error

) unexpected at this time

Code: Select all

c:\Gayu>test.bat -c "c:\Gayu\Test(123)\abc.xls" 
) was unexpected at this time.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Access file path containing paranthesis

#2 Post by foxidrive » 01 Sep 2015 22:44

We're happy to help you with your batch file - but you have to show it to us.

g3kr
Posts: 3
Joined: 01 Sep 2015 21:31

Re: Access file path containing paranthesis

#3 Post by g3kr » 02 Sep 2015 06:50

This is my batch script

Code: Select all

@ECHO off
setlocal enableextensions enabledelayedexpansion

REM ******************************************************************************
REM DESCRIPTION:
REM Converts xls to XML .
REM MODIFICATION HISTORY:
REM ******************************************************************************
@echo off
setlocal enableextensions enabledelayedexpansion
SET "JARName=abc.jar"
SET "xlsFile=abc.xls"
SET "xmlFile=abc.xml"

:loop
 IF NOT "%1" == "" (
    IF /I "%1"=="/J" (
      SET JARName=%2
        SHIFT
    )
    IF /I "%1"=="/C" (

      SET xlsFile=%2
        SHIFT
    )
    IF /I "%1"=="/O" (
      SET xmlFile=%2
        SHIFT
    )
    IF /I "%1"=="/?" (
   goto :help
   SHIFT
    )
    SHIFT
    GOTO :loop
 )

java -jar %JARName% -c %xlsFile% -o %xmlFile%
PAUSE
GOTO end

:help
   ECHO.
    ECHO Converts XLS into XML Format
    ECHO.
   ECHO %~n0 [/?] [/J [drive:][path]filename] [/C [drive:][path]filename]  [/O [drive:][path]filename]
   ECHO.
   ECHO.  /? Display this help message
   ECHO   /J conversion utility (.JAR)
   ECHO   /C spreadsheet (.XLS)
   ECHO   /O Ouput (.XML)
   ECHO.
   
:end



When i run this batch script from command line and give the value of c (xls file present in test(123) folder) as below

Code: Select all

abc.bat -c "c:\Gatu\Test(123)\xlsFile.xls"


It errors out as:

Code: Select all

 ) unexpected at this time


Any help to resolve this? I will need to know how batch file will access folder names with paranthesis.

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Access file path containing paranthesis

#4 Post by Squashman » 02 Sep 2015 09:05

Shouldn't you be using slashes "/" for your command line arguments. At least that is what it looks like to me. Your batch file checks for /C not -C.

Code: Select all

C:\g3kr>test.bat -c "c:\Gayu\Test(123)\abc.xls"
) was unexpected at this time.

C:\g3kr>test.bat /c "c:\Gayu\Test(123)\abc.xls"
java -jar abc.jar -c "c:\Gayu\Test(123)\abc.xls" -o abc.xml
Press any key to continue . . .

g3kr
Posts: 3
Joined: 01 Sep 2015 21:31

Re: Access file path containing paranthesis

#5 Post by g3kr » 02 Sep 2015 12:41

Even if i use /C instead of -c, its not working.

its not able to access a file path that contains a folder name in this format test(123)

Squashman
Expert
Posts: 4486
Joined: 23 Dec 2011 13:59

Re: Access file path containing paranthesis

#6 Post by Squashman » 02 Sep 2015 12:44

g3kr wrote:Even if i use /C instead of -c, its not working.

its not able to access a file path that contains a folder name in this format test(123)


We are not omniscient. You will have to explain in more detail what is happening.

I took your code and ran it both ways. And you can see from my output that it does work.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Access file path containing paranthesis

#7 Post by foxidrive » 02 Sep 2015 20:17

It's not required, but I confirm Squashman's results.

penpen
Expert
Posts: 2009
Joined: 23 Jun 2013 06:15
Location: Germany

Re: Access file path containing paranthesis

#8 Post by penpen » 03 Sep 2015 06:10

1) If you use the option "-c" then this option is not found in your "IF-construct", so the whole is shifted 1 time and goes back to the label ":loop". Then the filename (encapsulated in doublequotes) loses its encapsulation because "%1" equals ""c:\Gayu\Test(123)\abc.xls"".
2) Add an error handling for unknown parameter.
3) Use "else if" for different cases.
4) Use save parameter access "%~1" instead of "%1" (same for the set command, ...).
5) Always escape ( and ) within echo (and similar).
Resulting code:

Code: Select all

@ECHO off
setlocal enableExtensions enableDelayedExpansion
REM ******************************************************************************
REM DESCRIPTION:
REM Converts xls to XML .
REM MODIFICATION HISTORY:
REM ******************************************************************************
SET "JARName=abc.jar"
SET "xlsFile=abc.xls"
SET "xmlFile=abc.xml"

:loop
IF NOT "%~1" == "" (
   IF /I "%~1"=="/J" (
      SET "JARName=%~2"
      SHIFT
      SHIFT
   ) ELSE IF /I "%~1"=="/C" (
      SET "xlsFile=%~2"
      SHIFT
      SHIFT
   ) ELSE IF /I "%~1"=="/O" (
      SET "xmlFile=%~2"
      SHIFT
      SHIFT
   ) ELSE IF /I "%~1"=="/?" (
      SHIFT
      goto :help
   ) ELSE (
      REM ERROR
      goto :help
   )
   GOTO :loop
)

java -jar "%JARName%" -c "%xlsFile%" -o "%xmlFile%"
PAUSE
GOTO :end

:help
   ECHO.
   ECHO Converts XLS into XML Format
   ECHO.
   ECHO %~n0 [/?] [/J [drive:][path]filename] [/C [drive:][path]filename]  [/O [drive:][path]filename]
   ECHO.
   ECHO.  /? Display this help message
   ECHO   /J conversion utility ^(.JAR^)
   ECHO   /C spreadsheet ^(.XLS^)
   ECHO   /O Ouput ^(.XML^)
   ECHO.
:end
endlocal

penpen

Post Reply