Remove quotes from user input string

June 15, 2009 at 11:27:08
Specs: Windows XP -- batch scripting
I am new at this so forgive my ugly batch script (it's just plain old windows batch scripting language) . I am trying to normalize user input so that directory path entered by user would end up without quotes if it was entered with quotes: e.g.:
user input: "C:\Documents and Settings" becomes C:\Documents and settings

And the path that doesn't have spaces and is entered without quotes will stay without quotes:
e.g.: E:\Testing\BatchScripts stays E:\Testing\BatchScripts

The reason I need this is so that when I pass back the string to the script I can add quotes. Otherwise, if I add quotes and the string already has quotes I'll get the quotes twice and the path will be invalid. Or if I don't have the script add quotes and I get a path without quotes, the path will be invalid too.

How do you remove quotes from user input and do nothing if they are not there. Or may be I am going at it from a totally wrong angle and there is another way to take care of this problem?

Here's my batch:


REM User input -- directory to be processed
set /p FILEDIR=Enter directory path for files to be examined:
REM user input -- output directory
set /p OUTPUTPATH_FILENAME=Enter desired output path and filename:

REM if file exists go to alreadyexist
If exist %OUTPUTPATH_FILENAME% goto alreadyexist
REM need to change drives here because I am on drive E and fits.bat is on drive C
CD /D "C:Program files"
for %%f in ("%FILEDIR%\*.*") do fits -i "%%f" >> "%OUTPUTPATH_FILENAME%"

REM if output file exists ask to append or overwrite
set /p FILEEXISTS=This file already exists. Append or delete? Enter 'a' for append and 'd' for delete:
CD /D "C:Program files"

for %%f in ("%FILEDIR%\*.*") do fits -i "%%f" >> "%OUTPUTPATH_FILENAME%"


See More: Remove quotes from user input string

Report •

June 15, 2009 at 12:19:22
"C:\Documents and Settings" becomes C:\Documents and settings and directory

I don't know how, but I'll stay tuned.

If at first you don't succeed, you're about average.


Report •

June 15, 2009 at 12:37:29
oops -- I meant

"C:\Documents and Settings" becomes
C:\Documents and settings

Report •

June 15, 2009 at 14:00:04
To remove double quotes from user's command tail parameters refer to them in the script body as

%~1 or %~2 and so on.

I.e. the variable modifier ~ removes the double quotes embracing the parameter if they are. It is just aimed to the normalization you need. E.G

mybatch "C:\Documents and Settings" Hello


C:\Documents and Settings

if referenced as


Report •

Related Solutions

June 16, 2009 at 10:28:54
Thank you IVO.
It works on command line but I can't get it to work as part of a batch.
I tried to add %~ to %OUTPUTPATH_FILENAME%
as in %~OUTPUTPATH_FILENAME% but it's not working.

Report •

June 16, 2009 at 15:56:22
Try something like this (untested):


(PS I like the fact this forum allows you to vote your own entry as a "great answer"! Please disregard the "great answer" vote, it was just me testing.)

Report •

June 17, 2009 at 02:02:26
What I suggested does not work for environment variables (the variables you set up by the SET command and refer as %VAR%).

To strip away double quotes from those vars use klint's hint or the substring manipulation statement

  set MyVar=%MyVar:"=%

Report •

June 17, 2009 at 08:23:02
Both IVO's and klint's methods worked perfectly!
Thank you guys!

Report •

Ask Question