Preserve blank lines in a dos for loop

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
staro30
Posts: 2
Joined: 13 Sep 2011 08:38

Preserve blank lines in a dos for loop

#1 Post by staro30 » 13 Sep 2011 09:02

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

staro30
Posts: 2
Joined: 13 Sep 2011 08:38

Re: Preserve blank lines in a dos for loop

#2 Post by staro30 » 15 Sep 2011 03:14

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

dbenham
Expert
Posts: 2461
Joined: 12 Feb 2011 21:02
Location: United States (east coast)

Re: Preserve blank lines in a dos for loop

#3 Post by dbenham » 15 Sep 2011 08:59

"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

Post Reply