Link to home
Start Free TrialLog in
Avatar of pphreadr
pphreadrFlag for United States of America

asked on

batch remove quotes from variables

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
	)

Open in new window


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.
ASKER CERTIFIED SOLUTION
Avatar of Steve Knight
Steve Knight
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of pphreadr

ASKER

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.
Avatar of Bill Prew
Bill Prew

Welcome.

~bp
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