none
Dealing with speech marks in batch files RRS feed

  • Question

  • Hi

    I have a batch file that processes files in a given folder. It takes the folder path as a parameter (%1) and then processes certain files within that folder. 

    My problem is that the folder parameter passed to the batch file needs to be enclosed in speechmarks as they often contain spaces. My batch file then appends the files to the parameter which breaks the command. 

    Here's an example

    mybatchfile.bat:

    echo %1\mypdf.pdf

    end:

    The command line;

    mybatchfile.bat "c:\temp\folder"

    results in an output of;

    "c:\temp\folder"\mypdf.pdf

    I need the speech marks to be moved so that they enclose the complete pdf path, thus:

    "c:\temp\folder\mypdf.pdf"

    Really I need to insert the "\mypdf.pdf" string before the last speech mark. 

    Any ideas how?


    Olly

    Monday, January 6, 2014 9:15 PM

Answers

  • A replaceable parameter (%1, %2, %3) will contain the quotation marks if they are included.

    To do what you want, copy the content of the replaceable parameter into an environment variable and remove the quotes:


    @echo off
    setlocal enableextensions
    set DIR=%~1
    echo Path with quotes: "%DIR%\MyPDF.pdf"
    endlocal
    

    See the help information in call /? for more information about the syntax.

    Bill

    Monday, January 6, 2014 9:23 PM
    Moderator

All replies

  • A replaceable parameter (%1, %2, %3) will contain the quotation marks if they are included.

    To do what you want, copy the content of the replaceable parameter into an environment variable and remove the quotes:


    @echo off
    setlocal enableextensions
    set DIR=%~1
    echo Path with quotes: "%DIR%\MyPDF.pdf"
    endlocal
    

    See the help information in call /? for more information about the syntax.

    Bill

    Monday, January 6, 2014 9:23 PM
    Moderator
  • Hi

    I have a batch file that processes files in a given folder. It takes the folder path as a parameter (%1) and then processes certain files within that folder. 

    My problem is that the folder parameter passed to the batch file needs to be enclosed in speechmarks as they often contain spaces. My batch file then appends the files to the parameter which breaks the command. 

    Here's an example

    mybatchfile.bat:

    echo %1\mypdf.pdf

    end:

    The command line;

    mybatchfile.bat "c:\temp\folder"

    results in an output of;

    "c:\temp\folder"\mypdf.pdf

    I need the speech marks to be moved so that they enclose the complete pdf path, thus:

    "c:\temp\folder\mypdf.pdf"

    Really I need to insert the "\mypdf.pdf" string before the last speech mark. 

    Any ideas how?


    Olly

    This example is not correct.  Quotes passed to a path command are not a problem when the command is executed.

    >>mybatch.bat
    dir %1\*.log

    Is called

    mybatch "c:\windows"

    Try it.  It works just as expected.


    ¯\_(ツ)_/¯

    Monday, January 6, 2014 9:48 PM