Solved

batch remove quotes from variables

Posted on 2014-01-09
5
1,486 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 56

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 56

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

WordPress Tutorial 4: Recommended Plugins

Now that you have WordPress installed, understand the interface, and know how to install new parts, let’s take a look at our recommended plugins.

Question has a verified solution.

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

This article summaries thoughts and ideas from two years of sustained use. It provides good reasoning to make the jump to Windows 10.
When asking a question in a forum or creating documentation, screenshots are vital tools that can convey a lot more information and save you and your reader a lot of time
With the advent of Windows 10, Microsoft is pushing a Get Windows 10 icon into the notification area (system tray) of qualifying computers. There are many reasons for wanting to remove this icon. This two-part Experts Exchange video Micro Tutorial s…
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
Suggested Courses

617 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