• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4860
  • Last Modified:

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.
0
UnderSeven
Asked:
UnderSeven
  • 5
  • 4
  • 3
2 Solutions
 
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
 
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
Prepare for your VMware VCP6-DCV exam.

Josh Coen and Jason Langer have prepared the latest edition of VCP study guide. Both authors have been working in the IT field for more than a decade, and both hold VMware certifications. This 163-page guide covers all 10 of the exam blueprint sections.

 
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
 
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
 
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 5
  • 4
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now