pphreadr
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%%MyF ile%"
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.
Results of above
the Directory is c:\Users\Tony\Documents\Fo rt Worth ToDo\
the File is to do.txt
the File - Path is "c:\Users\Tony\Documents\F ort Worth ToDo\to do.txt"
C:\Program Files (x86)
The system cannot find the file specified.
Error occurred while processing: c:\Users\Tony\Documents\Fo rt.
The system cannot find the file specified.
Error occurred while processing: Worth.
The system cannot find the path specified.
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\Fo
the File is to do.txt
the File - Path is "c:\Users\Tony\Documents\F
C:\Program Files (x86)
The system cannot find the file specified.
Error occurred while processing: c:\Users\Tony\Documents\Fo
The system cannot find the file specified.
Error occurred while processing: Worth.
The system cannot find the path specified.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Welcome.
~bp
~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
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
ASKER
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.