Question:
Can you help me with this advanced windows batch scripting problem for experts only?
boris
2008-10-10 08:00:16 UTC
Ok, this is a toughie to reproduce. This is part of a larger problem, but I've narrowed down the issue to something very specific. If I can solve this problem, I will solve the bigger problem in the most elegant way possible. I would like a solution to this specific problem....

Heres a batch file, lets call it: "test1.bat"

test1.bat:

@ECHO OFF
SET TMPTEXT=%~1%
ECHO %TMPTEXT%

Now, on the command line call the batch file like so:

test1 "blah "something;something else""

The output should be:

blah "something

I want the entire string output. I need a method of escaping the semi-colon, or the double-quotes, or some way to get that batch file output the entire string not splitting it up.

What I'm trying to achieve is: I have a text file with environment variables in it. I want to output all the lines in the text file, from a batch file, and substitute the environment variables. I'm using a for /f loop with a call to the above batch file to replace the environment variables... my only problem is semicolons in the command are throwing everything off!

I would be really surprised and grateful if anyone can solve this problem.
Four answers:
Raymond B
2008-10-11 21:22:09 UTC
goto :start

Windows batch interpreter sees the semi-colon as an argument separator. The only way to escape this is by surrounding it with double quotes, and you are on the right track.



Your best bet will be to use %* to get the whole command line. You could then use a for loop to strip the quotes. Here is your batch file modified to do this:



:start

@ECHO OFF

SET TMPTEXT=%~1%

ECHO %TMPTEXT%

echo %*



REM // remove the quotes

for /f "tokens=*" %%a in ("%*") do (

echo %%~a

)



exit /b 0



Here is the output:

F:\>test "blah "something;something else""

blah "something

"blah "something;something else""

blah "something;something else"



Please leave a comment if this helps and you have any questions.



Thanks

Raymond
anonymous
2008-10-10 08:24:38 UTC
Its been a while. This is why I left dos in '87 ;)



found this ...

3/4 way down http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/Q_21567899.html



@echo off

setlocal

set Work=%Path%

set Work=C:\WinNT\system32;D:\Test\;D:\"Tools";"D:\Te;st"

:: *** Replace quotes with an asterisk; we need to check for a single quote later on, which doesn't really work in batch.

set Work=%Work:"=*%

echo %Work%

set InQuote=0

set PathComplete=0

:loop

set Char=%Work:~0,1%

set Work=%Work:~1%

if not defined Work set PathComplete=1

:: *** the semicolon is a delimiter only if found outside a pair of quotation marks:

if "%Char%"==";" (

if %InQuote%==0 set PathComplete=1

)

:: *** Quotation marks have been replaced with "*";

:: *** drop them, if present, and keep track of whether we're in or out:

if "%Char%"=="*" (

if %InQuote%==1 (

set InQuote=0

) else (

set InQuote=1

)

set Char=

)

:: *** If the current path is complete, do something, then start again;

:: *** otherwise ad the character read to the current path.

if %PathComplete%==1 (

call :TokenComplete

set PathComplete=0

set CurrentPath=

) else (

set CurrentPath=%CurrentPath%%Char%

)

if defined Work goto loop

goto leave



:TokenComplete

:: *** Remove any trailing backslashes for a consistent path:

if "%CurrentPath:~-1%"=="\" set CurrentPath=%CurrentPath:~0,-1%

echo [%CurrentPath%]

goto :eof



:leave
shaddix
2016-10-07 01:14:32 UTC
jogs my memory of a narrative a chum advised me some chum of his. may be worth bearing it techniques. He set up a community and presented each and all of the kit for an company. They paid many times each and each month yet have been given slightly twitchy in direction of the tip. very final fee became into approximately £5,000 and none became into forth coming. So he purely waited for the panic telephone call. particular sufficient 30 days previous the fee date he have been given it. the full device had close down. He went in and demanded £10,000 in money to instruct the full device back on. After a pair of hours they coughed up! What he did became into software the full device to close down 30 days while they could have paid. As he advised them in case you had paid your bill on time i could have given the code to cancel that little bit of programming because of the fact no one ought to crack the instructions he put in! ethical of the story is he became into 5 grand greater appropriate off for an hours artwork!
anonymous
2008-10-10 09:33:04 UTC
echo %1



will echo the entire command line including the quotation marks and the semicolons.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...