Delete blank lines not working

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Delete blank lines not working

#1 Post by aisha » 18 Apr 2017 11:50

Hello all,
We have the following code for deleting blank lines in a TXT file, but it is failing to remove the blank lines - any advice?

findstr /V /R /C:"^$" c:\salad\yarn\buzzard.txt >>c:\salad\yarn\buzzard2.txt
del "c:\salad\yarn\buzzard2.txt"
rename "c:\salad\yarn\buzzard2.txt" "buzzard.txt"


- Aisha

elzooilogico
Posts: 128
Joined: 23 May 2016 15:39
Location: Spain

Re: Delete blank lines not working

#2 Post by elzooilogico » 18 Apr 2017 12:05

simply

Code: Select all

@echo off

set "src_file=c:\salad\yarn\buzzard.txt"
set "dst_file=c:\salad\yarn\buzzard2.txt"

copy /y "%src_file%" "%dst_file%" 2>NUL || (echo Couldn't find %src_file% & goto :eof)

>"%src_file%" (
  for /F "tokens=*" %%1 in (%dst_file%) do echo(%%1
)


for /F will skip all blank lines.

Note the for block in parens, so dest file is opened and closed only once.

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: Delete blank lines not working

#3 Post by aisha » 18 Apr 2017 13:06

That's almost there, but we still have a blank line at the end.
Is there a way to remove that?

sorry if i was not clear

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Delete blank lines not working

#4 Post by miskox » 18 Apr 2017 13:39

aisha wrote:Hello all,
We have the following code for deleting blank lines in a TXT file, but it is failing to remove the blank lines - any advice?

findstr /V /R /C:"^$" c:\salad\yarn\buzzard.txt >>c:\salad\yarn\buzzard2.txt
del "c:\salad\yarn\buzzard2.txt"
rename "c:\salad\yarn\buzzard2.txt" "buzzard.txt"


- Aisha


Why do you delete your output file in line 2 of your code?

This* works for me:

Code: Select all

findstr /rvx /c:" *" in_file.txt>out_file.txt


Saso

* got this solution here viewtopic.php?t=6167

aisha
Posts: 26
Joined: 28 Sep 2016 06:40

Re: Delete blank lines not working

#5 Post by aisha » 18 Apr 2017 14:08

thank you re deleting the 2.txt file - that was a mistake on my part

the posted "solution" definitely removes empty lines but still preserves the last line as blank, so I am still trying to research this thing and figure out why

will definitely post the solution if i find it too

Aisha

aGerman
Expert
Posts: 4678
Joined: 22 Jan 2010 18:01
Location: Germany

Re: Delete blank lines not working

#6 Post by aGerman » 18 Apr 2017 15:45

You may use a hybrid script. Even if most of the code is JScript save it with extension .bat

Code: Select all

@if (@a)==(@b) @end/*

:: Batch
@echo off &setlocal
cscript //nologo //e:jscript "%~f0" "in_file.txt" "out_file.txt"
pause

:: JScript
exit /b&::*/ try {
  var objFSO = new ActiveXObject('Scripting.FileSystemObject'),
      objFileIn = objFSO.OpenTextFile(WScript.Arguments(0)),
      objFileOut = (WScript.Arguments.Count() > 1) ? objFSO.OpenTextFile(WScript.Arguments(1), 2, true) : WScript.StdOut,
      line = objFileIn.ReadLine(),
      newline = false;
 
  if (line.length) {
    objFileOut.Write(line);
    newline = true;
  }
  while (!objFileIn.AtEndOfStream) {
    line = objFileIn.ReadLine();
    if (line.length) {
      if (newline) { objFileOut.WriteLine(); }
      objFileOut.Write(line);
      newline = true;
    }
  }
}
catch(e) { WScript.StdErr.WriteLine(e.message); }

Steffen

miskox
Posts: 630
Joined: 28 Jun 2010 03:46

Re: Delete blank lines not working

#7 Post by miskox » 19 Apr 2017 01:35

aisha wrote:thank you re deleting the 2.txt file - that was a mistake on my part

the posted "solution" definitely removes empty lines but still preserves the last line as blank, so I am still trying to research this thing and figure out why

will definitely post the solution if i find it too

Aisha



This might help:

Code: Select all

copy /b in_file.txt in_file.tmp&&move /y in_file.tmp in_file.txt


See viewtopic.php?t=5373

Saso

Post Reply