Solved

Batch file not removing last blank line

Posted on 2013-01-24
4
2,039 Views
Last Modified: 2013-01-29
Here is code for batch file. It generate text file but insert blank line at the end of file. I wrote subroutines to remove the blank lines but still no luck. please help me out on this

batch file file

@echo off
SETLOCAL ENABLEDELAYEDEXPANSION



bcp tbltest out "C:\Data\CAAGIndividualsFile.TXT" -c -SQLINST\T -testuser -Ptest
call :StripBlankLines CAAGIndividualsFile.TXT


goto :eof
:StripBlankLines
For %%x in ("%~1") do set OutF=temp_%%~nx.txt
if exist "%OutF%" del "%OutF%"
for /F "usebackq delims=" %%B in (%*) do (
	call :TrimWS %%B
	if not "!Line!"=="" >>"%OutF%" echo !Line! 
)
goto :eof

:TrimWS
set Line=%*
goto :eof 

:End
               

Open in new window



And find the attached text file
individualsFile.TXT
0
Comment
Question by:sonawanekiran
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
4 Comments
 
LVL 55

Assisted Solution

by:Bill Prew
Bill Prew earned 333 total points
ID: 38817318
If you want to elliminate the blank lines from a file give this approach a try, it should take care of everything.

findstr /V /R /B /E /C:" *" in.txt>out.txt

~bp
0
 
LVL 17

Author Comment

by:sonawanekiran
ID: 38817456
@billprew : Thanks for reply. Could you please modify the script for me.. please? I am really new to batch scripting. This is first time I am working with.
0
 
LVL 70

Assisted Solution

by:Qlemo
Qlemo earned 167 total points
ID: 38817810
Your example text file does not contain any blank line. But if, your lines 14 to 17 can be replaced by
   find /v "" < "%1" > "%OutF%"
and lines 19 and following are not required any longer.
0
 
LVL 55

Accepted Solution

by:
Bill Prew earned 333 total points
ID: 38818457
Should be as simple as this.  Note that using FINDSTR will eliminate all blank lines, even ones that have some spaces on them but no data.  Since you had some trimming going on and such that felt like the right approach.  FIND will only remove truly empty lines.

@echo off
setlocal EnableDelayedExpansion

bcp tbltest out "%TEMP%\CAAGIndividualsFile.TXT" -c -SQLINST\T -testuser -Ptest
findstr /V /R /B /E /C:" *" "%TEMP%\CAAGIndividualsFile.TXT">"C:\Data\CAAGIndividualsFile.TXT"

Open in new window

~bp
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

VALIDATING DATES One method of validating dates is to jam the date into the DATE command and see if it accepts it by examining the system's errorlevel value. A non-zero result indicates failure. A typical example might look something like the fol…
You may have already been in the need to update a whole folder stucture using a script. Robocopy does it well and even provides a list of non-updated files in a log (if asked to). Generally those files that were locked by a user or a process by the …
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question