I have the following problem:
A batch file needs some parameters. Some of them contain space characters,
so that I have to quote them. But in the batch-file, i wouldn't the quotes.
How do I remove the quote at the beginning and the end (not in th text)?
for example:
example.bat xyz "abc def"
should be:
%1 = xyz
%2 = abc def (and NOT "abc def")
BUT
example.bat "abc "def" ghi"
should be: abc "def" ghi
Has anybody some idea?
The batch-file should work with Win NT 4.0, Windows 2000 and Windows XP
Thanks in advance
Juerg
In, for example, Windows 2000, use the %~1 syntax:
====Begin cut-and-paste (omit this line)
@ECHO OFF
ECHO. %%1=%1
:: Use %~1 to remove "quotes"
ECHO. %%~1=%~1
====End cut-and-paste (omit this line)
============Screen capture Windows 2000 simulated in Win95
C:\WORK>demo.bat "Look after Mr Bond. See that some harm comes to him."
%1="Look after Mr Bond. See that some harm comes to him."
%~1=Look after Mr Bond. See that some harm comes to him.
C:\WORK>
============End screen capture
--
William and Linda Allen
Batch File Course, Syllabus and Lesson index: http://www.allenware.com/
If it was just for Win 2K/XP you could use the built-in parameter
substitution. From CALL's help screen:-
Substitution of batch parameters (%n) has been enhanced. You can
now use the following optional syntax:
%~1 - expands %1 removing any surrounding quotes (")
So in your batch, %2 would be '"abc def"' and %~2 'abc def' (without
the single quotes)
NT doesn't have this feature, but it has something similar which
_may_ work for you. Its %~n1 which expands %1 to a file name only
(without quotes). I say it may work for because if your parameter
is a valid path/filename, you'll likely to get 'unexpected' results.
For something more reliable in NT, see this thread: -
Subject: Removing Quotes
Newsgroups: alt.msdos.batch.nt
Date: 2002-06-09 06:26:05 PST
http://groups.google.com/groups?threadm=3d03528c.11246551%40news.demon.co.uk
Rather than use something like Franks/Simons function for NT/2K/XP
I'd probably just use it for NT and use the built-in parameter
substitution for 2K/XP. You'd just need to determine the O/S at the
start of you script - you'll find many examples if you search this
group. HTH
--
Ritchie
From the syntax (Set /?):
...
May also specify substrings for an expansion.
%PATH:~10,5%
would expand the PATH environment variable, and then use only the 5
characters that begin at the 11th (offset 10) character of the expanded
result. If the length is not specified, then it defaults to the
remainder of the variable value. If either number (offset or length) is
negative, then the number used is the length of the environment variable
value added to the offset or length specified.
%PATH:~-10%
would extract the last 10 characters of the PATH variable.
%PATH:~0,-2%
would extract all but the last 2 characters of the PATH variable.
...
This only does the first parameter, but it should get you started:
@Echo Off
SetLocal
Set FIRST=%1&Echo 1:[%FIRST%]
Set LEFT=%1
Set LEFT=%LEFT:~0,1%
Echo LEFT:[%LEFT%]
Set RIGHT=%1
Set RIGHT=%RIGHT:~-1%
Echo RIGHT:[%RIGHT%]
If ^%LEFT%==^" Set FIRST=%FIRST:~1%
If ^%RIGHT%==^" Set FIRST=%FIRST:~0,-1%
Echo 1:[%FIRST%]
EndLocal
"Juerg Sommer" <som...@email.com> wrote in message
news:b7m4kq$2f0uv$1...@ID-4933.news.dfncis.de...
Thanks a lot your answers.
String replacement with SET would be the best solution.
For all other users who are still searching for a solution:
I removed the double-quotes and replaced single quotes with double-quotes,
so I had to enter the command like example.bat "abc 'def' ghi" and get
abc "def" ghi
the batch-file looks like:
set test=%1
set test=%test:"=%
set test=%test:'="%
Greetings and nice Easter
Juerg