Page 1 of 1

Preserve blank lines in a dos for loop

Posted: 13 Sep 2011 09:02
by staro30
Hi,

I use a dos script to alter the txt output of my LDIFDE script. The output contains many lines and blank lines. I change a line from 'changetype: add' to 'objectclass: top objectclass: person'.

It's very good at making the line change but also removes blank lines. I need the blank lines preserving. Please help?

setlocal enabledelayedexpansion
if not exist "%1" (echo file not found..)&goto :eof
for /f "tokens=* delims=" %%a in (%1) do (
if "%%a"=="changetype: add" set write=objectclass: top objectclass: person
echo !write!
(echo !write!)>>%~n1.replaced%~x1
)

I have tried "delims= "

Thanks
Staro

Re: Preserve blank lines in a dos for loop

Posted: 15 Sep 2011 03:14
by staro30
Hi,

I've decided to start again but use VBScript rather than a dos batch script as I don't think it is possible to ignore blank lines unless someone knows better?

Cheers
Staro

Re: Preserve blank lines in a dos for loop

Posted: 15 Sep 2011 08:59
by dbenham
"DOS" batch scripts are tricky to use for text file processing, so using another language is always a good viable option.

But it CAN be done with "DOS". Read through New technic: set /p can read multiple lines from a file for a newly discovered technique that is easy to implement. The only limitation of the new technique is the lines must strictly follow the DOS convention of <carriage return><line feed> for line termination.

Also in that post are examples of more complicated traditional techniques using FOR /F. These are more flexible with regard to line termination, but they require SETLOCAL / ENDLOCAL within the loop, thus making it difficult to preserve variable assignments across loop iterations. (Difficult, but not impossible)

Dave Benham