j-heart
asked on
How to Remove Blank Lines from text file in msdos?
I have a program that generates a CSV file with extra blank lines at the end. It is never the same number of lines and the file name changes each time the program is run but always starts out with "APPTS" and ends with the "TXT" extension.
I am looking for a batch file that will remove all the blank lines but keep the file name the same.
It also needs to work with wildcards.
Running it from the command line should look something like this:
Clean.bat appts*.txt
I am looking for a batch file that will remove all the blank lines but keep the file name the same.
It also needs to work with wildcards.
Running it from the command line should look something like this:
Clean.bat appts*.txt
findstr . myfile.txt > myNewFile.txt
ASKER
This works only if I know the file name in advance.
If I use a wildcard it appends the file name to the beginning of each line.
So if I run:
findstr . myfile.txt > myNewFile.txt
The output to file myNewFile.txt is perfect.
But if I run
findstr . myfile*.txt > myNewFile.txt
Then each line starts with the filename:
"myfile.txt:"
Since the program that generates the CSV file generates a rather random series of underscores and numbers after the "appts" begining, I need to be able to use wildcards.
If I use a wildcard it appends the file name to the beginning of each line.
So if I run:
findstr . myfile.txt > myNewFile.txt
The output to file myNewFile.txt is perfect.
But if I run
findstr . myfile*.txt > myNewFile.txt
Then each line starts with the filename:
"myfile.txt:"
Since the program that generates the CSV file generates a rather random series of underscores and numbers after the "appts" begining, I need to be able to use wildcards.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Sorry, the redirection got lost in line 6:
@echo off
setlocal
set FileMask=%~1
for %%a in ("%FileMask%") do (
ren "%%a" "%%~na.old"
findstr . "%%~dpna.old" >"%%a"
)
@echo off
setlocal
set FileMask=%~1
for %%a in ("%FileMask%") do (
ren "%%a" "%%~na.old"
findstr . "%%~dpna.old" >"%%a"
)