Help with XP cmd batch file removing blank lines with a space.

I am running a batch file on a data file.  the data file is bulk copied into a database.  The last line of the data file (which is passed to me and I do not have any control over it's creation) has a SPACE on a blank line.  

I need to get rid of that space and blank line.   This is Xp cmd and the file is a text file.  I have tried using the for statement in my batch and that looks like this: for /F "tokens=*delims=" %%G IN (%2) DO Echo %%G >> %1

This gets rid of blank lines great, but if there is a space on a blank line, this will result in tossing in at the end of the file the state of echo.  Any help would be great.
UnderSevenAsked:
Who is Participating?
 
oBdACommented:
I copied and pasted what you're using, changed only the file names, and it's working just fine here.
Input file was this:

Te|st.

Fun & More
Something (in brackets)
Larger>than and Smaller<than
"An ampersand & in quotes"
A caret ^ and a per%cent

The next line has a single space followed by a line break:
 

And the resulting output file was:

Te|st.
Fun & More
Something (in brackets)
Larger>than and Smaller<than
"An ampersand & in quotes"
A caret ^ and a per%cent
The next line has a single space followed by a line break:

without the last line with the space.
That's a real text file, not a binary file?
You can try to remove/uncomment the "@echo off" at the beginning to check what's happening, but I currently see no reason why it wouldn't work on your machine.
0
 
oBdACommented:
Try if this works for you (depends a bit on the characters appearing in the data file):

@echo off
setlocal enabledelayedexpansion
set OutputFile=%~1
set InputFile=%~2
for /f "delims=" %%a in ('type "%InputFile%"') do (
  set Line=%%a
  if not "!Line: =!"=="" >>"%OutputFile%" echo %%a
)

Btw: currently, you're adding a space at the end of each line as well.
0
 
UnderSevenAuthor Commented:
How can I not be adding said space?  though it isn't as big a deal so long as the lines are still uniform, my format file can handle that much.
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.

 
oBdACommented:
Yes, reading it again, my comment sounds misleading; anyway, my above script doesn't do that; it's just in your current script, because you have a space between the last character of the string in %%G and the redirection:
Echo %%G >> %1
You can prevent easiest that by starting with the redirection, as I did above:
>>"%OutputFile%" echo %%a
0
 
UnderSevenAuthor Commented:
Also the file will contain quotations and commas which must remain.
0
 
oBdACommented:
Try the script above, then use fc.exe (fc OriginalFileName ProcessedFileName) to check for differences; fc should only show differences where empty lines were removed.
0
 
UnderSevenAuthor Commented:
this script doesn't appear to be creating the output file.  When I try to run the script outside of a bat (adjusting the vars to work) It comes up with the word more? and I don't know what it is wanting, but I seem to be doing something wrong.  copying the script word for word didn't help me either.  
here is exactly how I made it look

@echo off
setlocal enabledelayedexpansion
set OutputFile=c:\data\ALL_TAT_TOTAL.TXT
set InputFile=E:\Importdata\Dataarchive\ALL_TAT_20071008.TXT
for /f "delims=" %%a in ('type "%InputFile%"') do (
  set Line=%%a
  if not "!Line: =!"=="" >>"%OutputFile%" echo %%a
)
0
 
Steve KnightIT ConsultancyCommented:
You might also be able to just use findstr:

findstr /V /R /C:" " oldfile.txt>newfile.txt

I think that will work but can't check right now sorry.

Steve
0
 
UnderSevenAuthor Commented:
Findstr is good except there needs to be allowance for spaces, just not a space on an otherwise blank line.  There are plenty of lines in the file that contains spaces somewhere in the data.  Just at the end of the file it doesn't include any other information.
0
 
Steve KnightIT ConsultancyCommented:
Fair comment with findstr though I had missed a ^ character out which would only exclude lines that START with a space (but not sadly those that only contain a space, it could be a longer line that starts with one):

findstr /V /R /C:"^ " oldfile.txt>newfile.txt

Good luck anyway, was just passing!

Steve
0
 
UnderSevenAuthor Commented:
Well I had two solutions, both worked in the end.  I would like to thank everyone who submitted responses.
0
 
Steve KnightIT ConsultancyCommented:
No problem.  Thanks for the assist. Sorry for missing the ^ off first time, was from memory!

Steve
0
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.

All Courses

From novice to tech pro — start learning today.