Solved

Batch file not removing last blank line

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

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 54

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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

Question has a verified solution.

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

The following is a collection of cases for strange behaviour when using advanced techniques in DOS batch files. You should have some basic experience in batch "programming", as I'm assuming some knowledge and not further explain the basics. For some…
Use this article to create a batch file to backup a Microsoft SQL Server database to a Windows folder.  The folder can be on the local hard drive or on a network share.  This batch file will query the SQL server to get the current date & time and wi…
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

738 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