Link to home
Start Free TrialLog in
Avatar of arundelr
arundelr

asked on

Batch script to remove blank Carriage Return lines

Batch script to remove blank Carriage Return lines

I currently have the below script which parses a file and if any lines exist which are empty and contain:

CR
CR/LF
data blah blah CR/LF
CR
CR/LF
more data blah blah CR/LF

it will strip away the empty data and produce:

data blah blah CR/LF
more data blah blah CR/LF

The problem is if an input file is like

CR
data blah blah CR/LF
CR
more data blah blah CR/LF

it fails....  

I would like it to work in either scenario or if not possible then just a new script / modification to handle this new scenario


Many thanks



@echo off
setlocal
rem This script strips out any empty lines terminated with a CR or a CR/LF
rem usage is process_feeds.bat "C:\inputfilename.txt" "C:\outputfilepath"
rem it also makes a backup of the input files
 
if [%2] NEQ [] (call :process %1 %2) else (
 for /f "tokens=*" %%a in ('dir /b /a-d "%source%\*.txt"') do call :process "%source%\%%a" "%dest%"
)
 
goto :eof
 
:process
set input=%~1
set output=%~2\%~nx1
echo Working on %input%
findstr /B /I /R "[!-z]" "%input%" > "%output%"
REM remove the REM from the next line to have it remove the 'source' files.
move "%input%" C:\backup\

Open in new window

Avatar of Bill Bach
Bill Bach
Flag of United States of America image

If you're not opposed to a simple C program, then the process is fairly simple.

Compile and call this program like this:
   REMBLANKS <inputfile >outputfile

Not quite a batch script, but I can build you the EXE (for use in a batch script) if you need it.
#include <stdio.h>
#include <string.h>
 
int main(int argc,char *argv[])
{
        char Buff[512],TempBuff[512];
	int i;
 
	while(!feof(stdin))
	{
		/* Read file, one line at a time, make a copy of each line  */
		if(fgets(Buff,510,stdin)==NULL)
			break;
		strcpy(TempBuff,Buff);
                // Get rid of trailing CR and LF bytes
                for(i=strlen(TempBuff);i>0;i--)
                    if(TempBuff[i]=='\r' || TempBuff[i]=='\n')
                        TempBuff[i]='\0';
                    else
                        break;  
                //If there's anything left, output original string unchanged
		if(strlen(TempBuff))
			fprintf(stdout,"%s",Buff);
	}
        return(0);
}

Open in new window

Can you upload a sample of a text file (using the Attach File checkbox) which causes your current script to fail?  How does it fail?

Would a vbscript solution be acceptable?
Try MUNGE http://www.ss64.com/nt/munge.html

Or use the 'tr' or 'sed' unix commands provided by www.cygwin.com

Avatar of arundelr
arundelr

ASKER

Hi Experts,

Attached is an example of the file

I would be OK to use a VBS because I can just call it from the batch
web.txt
Paste the script below into a text file with a .vbs extension.  Customize the value of the strInput variable with the location of a file to process.  Running the script will create an output file as defined in the strOutput variable.  

If this works correctly then additional code to handle arguments and multiple files can be added.  

If you test a file which does not process correctly, please upload the file along with a description of how it should work differently.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
strInput = "web.txt"
strOutput = "weboutput.txt"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
Set objTextFile = objFSO.OpenTextFile(strInput, ForReading, False, TriStateUseDefault)
strText = objTextFile.ReadAll
strText = Replace(strText, vbCr, "")
objTextFile.Close
 
Set objTextFile = objFSO.OpenTextFile(strOutput, ForWriting, True)
objTextFile.Write strText
objTextFile.Close

Open in new window

Hi Shift-3,

Thanks for your hard work, the only issue is that the file names are hard coded, what I need to be able to do is pass the inputfilename and the output path when I call the script

i.e. Currently I would use

process_feeds.bat "C:\inputfilename.txt" "C:\outputfilepath"

Yes, that was just a proof of concept.  As I said, additional code to handle arguments and multiple files can be added.

Paste the script below into a text file with a .vbs extension.  Customize the value of the strSource variable with the default folder to search (equivalent to the source variable in your batch script).  Customize the value of the strDest variable with the default folder to search (equivalent to the dest variable).  

Running the script should now work in the same manner as your batch script, with or without arguments.


Const ForReading = 1
Const ForWriting = 2
Const TriStateUseDefault = -2
 
On Error Resume Next
 
strSource = "c:\files"
strDest = "c:\output"
 
Set objFSO = CreateObject("Scripting.FileSystemObject")
 
If WScript.Arguments.Count > 1 Then
    Process WScript.Arguments(0), WScript.Arguments(1)
Else
    Set objWMIService = GetObject("winmgmts:\\.\root\cimv2")
 
    Set FileList = objWMIService.ExecQuery _
        ("ASSOCIATORS OF {Win32_Directory.Name='" & strSource & "'} Where " _
            & "ResultClass = CIM_DataFile")
 
    For Each objFile In FileList
        If objFile.Extension = "txt" Then
            Process objFile.Name, strDest
        End If
    Next
End If
 
Sub Process(strInput, strOutput)
    Set objTextFile = objFSO.OpenTextFile(strInput, ForReading, False, TriStateUseDefault)
    strText = objTextFile.ReadAll
    strText = Replace(strText, vbCr, "")
    objTextFile.Close
    
    Set objInput = objFSO.GetFile(strInput)
    strOutputFile = strOutput & "\" & objInput.Name
    Set objTextFile = objFSO.OpenTextFile(strOutputFile, ForWriting, True)
    objTextFile.Write strText
    objTextFile.Close
End Sub

Open in new window

Hr Shift-3,

"As I said, additional code to handle arguments and multiple files can be added."
Ah, sorry I speed read your original notes ;-)

I have done as suggested and it does now behave correctly being called from the batch and setting the source/destinations

The only issue is that its stripping away all the CR

so the file starts

CR
X      0014116908 CR/LF
CR
1      000000000000000000CR/LF
CR
2      000000000000026301263551      HECR/LF
CR
3      000000 kgCR/LF
CR
3      0000000006 kgCR/LF

and ends up

X      0014116908 LF
1      000000000000000000 LF
2      000000000000026301263551      HE LF
3      000000 kg LF
3      0000000006 kg LF

and should be

X      0014116908 CR/LF
1      000000000000000000 CR/LF
2      000000000000026301263551      HECR/LF
3      000000 kg CR/LF
3      0000000006 kg LF

is it possible to change that ?





web.txt
FYI -- I did rebuild by RemFile tool to handle this for you, too, if you don't mind calling a separate application.  It is a free tool available from www.goldstarsoftware.com/tools.asp.  The command will be:
    REMFILE Blanks /E <inputfile >outputfile
ASKER CERTIFIED SOLUTION
Avatar of Shift-3
Shift-3
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Spot on Shift-3 - thanks ;o)