Page 1 of 1

Delete blank lines not working

Posted: 18 Apr 2017 11:50
by aisha
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

Re: Delete blank lines not working

Posted: 18 Apr 2017 12:05
by elzooilogico
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.

Re: Delete blank lines not working

Posted: 18 Apr 2017 13:06
by aisha
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

Re: Delete blank lines not working

Posted: 18 Apr 2017 13:39
by miskox
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

Re: Delete blank lines not working

Posted: 18 Apr 2017 14:08
by aisha
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

Re: Delete blank lines not working

Posted: 18 Apr 2017 15:45
by aGerman
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

Re: Delete blank lines not working

Posted: 19 Apr 2017 01:35
by miskox
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