Tuesday, 30 March 2010

Remove quotes from batch file parameters

No doubt a boring old school trick for the Powershell users among you, so, like, over batch files but handy in case of emergency:

If you supply a quoted parameter to a batch file (e.g. a directory path) and later need to remove the quotes to accommodate additional manipulation, handle the incoming parameter as %~1 instead of %1. 

To remove a trailing slash, do this:

IF "%SOLUTION_DIR:~-1%"=="\" SET SOLUTION_DIR=%SOLUTION_DIR:~0,-1%

Thanks Chuck and Jon!

2 comments:

  1. Just what I needed!
    Had been trying to solve:

    SET Ext=%1
    ...
    IF "%Ext%" EQU "*" ECHO You passed a wildcard

    kept giving me the output if I passed the asterisk double quoted:

    IF ""*"" EQU "*" ECHO You passed a wildcard

    Switched my line to:
    SET Ext=%~1

    and all works perfectly!

    Thanks,
    S.C.

    ReplyDelete

Spam comments will be deleted

Note: only a member of this blog may post a comment.