Solved

Batch file not removing last blank line

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

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 69

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 53

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Suggested Solutions

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…
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 …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…

808 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