Computing.Net > Forums > Programming > Remove quotes from user input string

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Remove quotes from user input string

Reply to Message Icon

Name: libcoder272
Date: June 15, 2009 at 11:27:08 Pacific
OS: Windows XP -- batch scripting
Subcategory: Batch
Comment:

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?

Thanks
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
:alreadyexist
set /p FILEEXISTS=This file already exists. Append or delete? Enter 'a' for append and 'd' for delete:
IF '%FILEEXISTS%'=='d' DEL "%OUTPUTPATH_FILENAME%"
CD /D "C:Program files"

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

:END



Sponsored Link
Ads by Google

Response Number 1
Name: Mechanix2Go
Date: June 15, 2009 at 12:19:22 Pacific
Reply:

"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.

M2


0

Response Number 2
Name: libcoder272
Date: June 15, 2009 at 12:37:29 Pacific
Reply:

oops -- I meant

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


0

Response Number 3
Name: IVO
Date: June 15, 2009 at 14:00:04 Pacific
Reply:

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

generates

C:\Documents and Settings
Hello

if referenced as

%~1
%~2


0

Response Number 4
Name: libcoder272
Date: June 16, 2009 at 10:28:54 Pacific
Reply:

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.


0

Response Number 5
Name: klint
Date: June 16, 2009 at 15:56:22 Pacific
Reply:

Try something like this (untested):

for %%f in (%OUTPUTPATH_FILENAME%) do set OUTPUTPATH_FILENAME=%%~f

(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.)


0

Related Posts

See More



Response Number 6
Name: IVO
Date: June 17, 2009 at 02:02:26 Pacific
Reply:

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:"=%


0

Response Number 7
Name: libcoder272
Date: June 17, 2009 at 08:23:02 Pacific
Reply:

Both IVO's and klint's methods worked perfectly!
Thank you guys!


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Remove quotes from user input string

user specified input file in c++ www.computing.net/answers/programming/user-specified-input-file-in-c/5829.html

Create a Functional Listbox www.computing.net/answers/programming/create-a-functional-listbox/5062.html

C: string input size! www.computing.net/answers/programming/c-string-input-size/8434.html