Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
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 settingsAnd the path that doesn't have spaces and is entered without quotes will stay without quotes:
e.g.: E:\Testing\BatchScripts stays E:\Testing\BatchScriptsThe 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

"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

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
Helloif referenced as
%~1
%~2

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.

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

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

![]() |
![]() |
![]() |

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