开发者

Batch file issue with ampersand & in an input record

I've creating a batch file to process a file of input records. I've cut out the majority of the code, so here is the problem. When it reads the 3rd record, with the ampersand, the job does not write the output record, UNLESS i put double quotes around the %%H variable. However, when I do this, it adds the " to the first and last byte of the record. I want to basically copy the record, and I'm doing some other processing a开发者_Go百科s well.

Is there a way to either strip the " from the front and back that are being appended, or is there a way for ECHO to handle the ampersand? thanks!

The input file has three records:

HOW NOW BROWN COW
JACK AND JILL
JUST YOU & ME

Script is as follows:

set infile=D:\file.txt
set outfile=D:\outfile.txt

for /F "tokens=*" %%H in (%infile%) do (CALL :Loop "%%H")

rem for /F "tokens=*" %%H in (%infile%) do (CALL :Loop %%H)

GOTO :Loop_Exit

:Loop
    echo %* >> %outfile%
:Loop_Exit
pause


You could use the variable substring operators to trim off the first and last characters, the quote marks.

Thus, if VAR="Quoted String" then the command echo %VAR:~1,-1% will not show the quotes.


Here you go, this does the trick via the %~ operator to remove quotes:

@echo off

set infile=e:\file.txt
set outfile=e:\outfile.txt

for /F "tokens=*" %%H in (%infile%) do (
    call :Loop "%%H"
)

exit /b 0

:Loop
    for %%a in (%*) do echo %%~a >> %outfile%
exit /b 0

OUTPUT:

E:\>foo2

E:\>type outfile.txt
HOW NOW BROWN COW
JACK AND JILL
JUST YOU & ME
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜