Solved

Batch file not removing last blank line

Posted on 2013-01-24
4
1,804 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 51

Assisted Solution

by:Bill Prew
Bill Prew earned 333 total points
Comment Utility
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
Comment Utility
@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
Comment Utility
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 51

Accepted Solution

by:
Bill Prew earned 333 total points
Comment Utility
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

Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

Join & Write a Comment

When you receive another warning that your shared drive is almost full and you have asked your users to clean out old files again and again, here is a single command that may help. This command will place all the files that have not been used rec…
How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

772 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

13 Experts available now in Live!

Get 1:1 Help Now