Solved Attempting to change blank lines in text files

June 16, 2012 at 12:57:42
Specs: Windows XP

I know that there is a way to remove blank lines in text files. However I have one that I need to replace blank lines with something different. I should also mention that I am attempting to do this within a batch file.

::example.txt
one fine day

two fine days

to be
::example2.txt
one fine day
separator charactors
two fine days.

It seems like there would be an easy way to do something like this. I was thinking something with a for statement, as there are half a dozen occurances of the blank in the file.

:: mike


See More: Attempting to change blank lines in text files

Report •

#1
June 16, 2012 at 17:53:11
Actually, this is quite difficult, if not impossible, using native batch, unless a third-party process is involved (a stream editor or something like it, which is actually doing all the work). This is because FOR unconditionally removes blank lines, even ones with spaces or tabs. I would recommend either qbasic, which is extremely simple to work with at this level, or vbscript, or visual basic. Here's a bare-bones vbscript:

set xin=wscript.stdin
set cout=wscript.stdout
'set xout=wscript.stderr
do while not xin.atendofstream
x=xin.readline
if x="" then x="new data replacing blank lines"
cout.writeline(x)
loop
'end of script

to run the script, you need to have cscript.exe available, and use it like this (where your saved vbscript is called noblanks.vbs)::
cscript /nologo noblanks.vbs <rawfile.txt >newfile.txt
If you want to dis-allow lines that are all space, use rtrim(x) in line 5:
x=rtrim(xin.readline)
You can streamline the commandline by dropping the cscript command into a batchfile: @echo off & cscript /nologo <%1 >%2
or use batch prompting for input and output files if you want more safety.
here's a qbasic snippet in case you're inclined that way:
INPUT "SOURCE FILE: ", INFIL$
OPEN INFIL$ FOR INPUT AS #1
DO WHILE NOT EOF(1)
LINE INPUT #1, K$
IF LTRIM$(K$) = "" THEN K$ = "NEW DATA REPLACES BLANK LINES"
PRINT K$
LOOP
CLOSE
END


Report •

#2
June 17, 2012 at 01:49:16
✔ Best Answer
There is a technique I read over on dostips that makes this a little easier using set /p:

@echo off
SetLocal EnableDelayedExpansion
(
    for /f %%n in ('type test.txt^|find /c /v ""') do for /l %%l in (1 1 %%n) do (
        set var=
        set /p "var="
        if defined var (
            echo(!var!
        ) else echo(sperating characters
    )
) < test.txt > newfile.txt
pause


Report •

#3
June 17, 2012 at 07:29:45
Thank you Judago this accomplishes what i need without any problems. I am trying to translate a bunch of text files into openpl songs for a camp i volunteer with. You just saved me a couple hours copying and pasting verse by verse. I have used dostips for their str manipulation page before, I don't know why i didn't check for this.

To nbrane thank you for the efforts with the vbs script, I am looking forward to working with it as I become more familiar with vbs.

:: mike


Report •

Related Solutions

#4
June 18, 2012 at 02:41:51
No problem.

I have used dostips for their str manipulation page before, I don't know why i didn't check for this.

It's hard to find a lot of stuff on dostips because they don't seem to do write-ups of, or sticky, the "definitive threads". Threads get buried and the easiest way to find them is to know what your looking for.


Report •

Ask Question