Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

batch remove quotes from variables

Posted on 2014-01-09
5
Medium Priority
?
1,720 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 320 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 59

Assisted Solution

by:Bill Prew
Bill Prew earned 180 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 59

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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Windows 10 Creator Update has just been released and I have it working very well on my laptop. Read below for issues, fixes and ideas.
Ever visit a website where you spotted a really cool looking Font, yet couldn't figure out which font family it belonged to, or how to get a copy of it for your own use? This article explains the process of doing exactly that, as well as showing how…
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

916 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