Solved

Batch file not removing last blank line

Posted on 2013-01-24
4
1,869 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
  • 2
4 Comments
 
LVL 52

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 68

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 52

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these r…
YESTERDAY YESTERDAY.BAT is inspired by a previous article I wrote entitled: TOMORROW.BAT (http://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_4196-Advanced-Batch-File-Programming-TOMORROW-BAT.html). The crux of this batch f…
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

911 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now