views:

50

answers:

2

I've creating a dos 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 as 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
A: 

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.

ewall
I tried this and could not get it to work in the context of the FOR loop. The process still fails when I replace the one line echo with a SET VAR=%* then echo %VAR:~1,-1% >> %outfile%.
Are you passing more than one item (`%*`)? If not, `%1` is the first item passed: `Loop:; SET ITEM=%1; ECHO %ITEM:~1,-1%`
ewall
@ewall: To just trim off quotes from `%H`, you can use `%~H` (which will do nothing, should there be no quotes).
@simplybest55 Oh, awesome... I didn't know it was that simple!
ewall
A: 

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
esac