Solved

# batch remove quotes from variables

Posted on 2014-01-09
1,215 Views
I have had several instances where I need/want to create a variable by combining other variables. i.e. SET PathFile="%CurrentDir%%MyFile%"

I am looking for a way to strip the quotes (") from the variables so I can combine several and enclose all in a single set of quotes for use somewhere in the script. Same issue happens when I need to use an environment variable that has a space.

For some of my needs I have found a way to work around this issue. However I would really like to know if there is a way to do this.

@ECHO OFF
SET CurrentDir=c:\Users\Tony\Documents\Fort Worth ToDo\
SET MyFile=to do.txt
SET PathFile="%CurrentDir%%MyFile%"
ECHO the Directory is %CurrentDir%
ECHO the File is %MyFile%
ECHO the File - Path is %PathFile%
ECHO %ProgramFiles(x86)%
ECHO.
FOR /F "Tokens=*" %%A in ( 'TYPE "%PathFile%" ^| FINDSTR /V /C:"REM"' ) DO (
REM ECHO %MyFile%
ECHO %%A
)


Results of above
the Directory is c:\Users\Tony\Documents\Fort Worth ToDo\
the File is to do.txt
the File - Path is "c:\Users\Tony\Documents\Fort Worth ToDo\to do.txt"
C:\Program Files (x86)

The system cannot find the file specified.
Error occurred while processing: c:\Users\Tony\Documents\Fort.
The system cannot find the file specified.
Error occurred while processing: Worth.
The system cannot find the path specified.
0
• 2
• 2

LVL 43

Accepted Solution

Steve Knight earned 80 total points
ID: 39770298
on phone here at mo. so can't type correct character as it closes ee input box for some reason...

you could do.

set name=%name:"=%

which says replace all " in name with nothing (the bit after the =192 is what it is changed to)

or you can use a tilde in your %%expense syntax, i.e.

%£1 is the first param from commandline without quotes, %%£a is a variable used in for loop.  you can also do it in subroutine, e.g.

call :stripvar "my string with quotes on"
echo It is now %result%
exit /b

:stripvar
set result=%£1
exit /b

each £ char should be tilde.

Steve
0

LVL 53

Assisted Solution

Bill Prew earned 45 total points
ID: 39770944
Since you already have quotes around the variable then you don't want to add another set when you reference that variable.  So you can do one of the following:

(my personal preference is to never include quotes in the variable assignments, and always add them when I reference the variables in the script, if they are needed)

@ECHO OFF
SET CurrentDir=c:\Users\Tony\Documents\Fort Worth ToDo\
SET MyFile=to do.txt
SET PathFile="%CurrentDir%%MyFile%"
ECHO the Directory is %CurrentDir%
ECHO the File is %MyFile%
ECHO the File - Path is %PathFile%
ECHO %ProgramFiles(x86)%
ECHO.
FOR /F "Tokens=*" %%A in ( 'TYPE %PathFile% ^| FINDSTR /V /C:"REM"' ) DO (
REM ECHO %MyFile%
ECHO %%A
)

or
@ECHO OFF
SET CurrentDir=c:\Users\Tony\Documents\Fort Worth ToDo\
SET MyFile=to do.txt
SET PathFile=%CurrentDir%%MyFile%
ECHO the Directory is %CurrentDir%
ECHO the File is %MyFile%
ECHO the File - Path is %PathFile%
ECHO %ProgramFiles(x86)%
ECHO.
FOR /F "Tokens=*" %%A in ( 'TYPE "%PathFile%" ^| FINDSTR /V /C:"REM"' ) DO (
REM ECHO %MyFile%
ECHO %%A
)

~bp
0

Author Closing Comment

ID: 39772859
I want to think both of you for your help. This gives me several ideas that I did not think of.

Steve, I have never seen the syntax for set name=%name:"=% all I have seen is syntax using a tilde (set name=%name:~"=%) and that did not work for environment and assigned variables.
Thanks

Bill

I have had issues with paths/variables with spaces in them. That said I will keep this in mind as I never thought to not quote things with a space.
Thanks to you, too.
0

LVL 53

Expert Comment

ID: 39772863
Welcome.

~bp
0

LVL 43

Expert Comment

ID: 39774088
No problem, lots of useful stuff in batch that isn't immediately obvious, with that syntax you can do things like:

set x=%x:*+=% - replace all upto + with nothing
set x=%x:","=% - replace , with ","

i.e the bit before the : is the "find", the bit after is the "replace".

"find" can include * at the start and matches anything upto first occurance of the next char(s)
"find" or "replace" can be more than one char, e.e.

set x=%x:ABCD=EFGH%

etc.  SET /? shows it though not in great detail

Steve
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question