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

arundelrAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Bill BachPresident and Btrieve GuruCommented:
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

0
Shift-3Commented:
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?
0
MikeOM_DBACommented:
Try MUNGE http://www.ss64.com/nt/munge.html

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

0
Introducing Cloud Class® training courses

Tech changes fast. You can learn faster. That’s why we’re bringing professional training courses to Experts Exchange. With a subscription, you can access all the Cloud Class® courses to expand your education, prep for certifications, and get top-notch instructions.

arundelrAuthor Commented:
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
0
Shift-3Commented:
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

0
arundelrAuthor Commented:
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"

0
Shift-3Commented:
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

0
arundelrAuthor Commented:
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
0
Bill BachPresident and Btrieve GuruCommented:
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
0
Shift-3Commented:
Ok, try this.


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, "")
	strText = Replace(strText, vbLf, vbCrLf)
    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

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
arundelrAuthor Commented:
Spot on Shift-3 - thanks ;o)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.