Solved

batch remove quotes from variables

Posted on 2014-01-09
5
1,266 Views
Last Modified: 2014-01-11
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.
0
Comment
Question by:pphreadr
  • 2
  • 2
5 Comments
 
LVL 43

Accepted Solution

by:
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

by:Bill Prew
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
	)

Open in new window

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
	)

Open in new window

~bp
0
 

Author Closing Comment

by:pphreadr
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

by:Bill Prew
ID: 39772863
Welcome.

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
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

Resolve Critical IT Incidents Fast

If your data, services or processes become compromised, your organization can suffer damage in just minutes and how fast you communicate during a major IT incident is everything. Learn how to immediately identify incidents & best practices to resolve them quickly and effectively.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Remote control Windows CE 7 96
SCCM 2012 R2, Rollback KB updates 4 27
psched 3 20
Performance monitor user data collector set error 6 24
An article on effective troubleshooting
A project that enables an administrator to perform actions within a user session context not just at the time of login but any time later on day(s) or week(s) later.
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

828 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question