Solved

Batch file not removing last blank line

Posted on 2013-01-27
5
1,838 Views
Last Modified: 2013-01-29
I tried both way to remove last line in text file which is output of batch file . See below batch file code

1)

@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




2)

@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


But still both batch file not removing last blank line. See the attached text file which is output from both batch file. There is still blank line at the end of record.

Please help out..
output.TXT
0
Comment
Question by:sonawanekiran
  • 2
  • 2
5 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 38825908
There is no blank line at the end of the attached file; the only thing that is there is a trailing CRLF. In other words, you have something like
Line 1<CR><LF>
Line 2<CR><LF>

Open in new window

If you don't want to have the last CRLF, try this:
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

bcp "select 'Name', 'EIN', 'AccountId', 'Other ID', 'Address Line 1', 'Address Line 2', 'City', 'State', 'Zip Code', 'Country', 'Other Data', 'Provider ID', 'Group ID' union all select * from testdb" queryout "C:\Data\CAAGBusinessFile.txt" -c -test\T_GSS -testuser -mypass
call :StripBlankLines "CAAGBusinessFile.txt"

bcp testdb out "C:\Data\CAAGIndividualsFile.TXT" -c -SNTSMSQL15011A\T_GSS -tesur -Ptest
call :StripBlankLines "CAAGIndividualsFile.TXT"

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

:TrimWS
set Line=%*
goto :eof 

:write
>>"%OutF%"<NUL set /p Dummy=%*
goto :eof

Open in new window

0
 
LVL 17

Author Closing Comment

by:sonawanekiran
ID: 38825916
Thanks Boss.... 500*4   ..  :)
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 38826505
Agreed, there was no blank line at the end of the file you showed.  It is normal for a Windows text file to have a <CR><LF> pair at the end of every line, including the last line.

Just curious, why do you need to strip off the "end of line" from the last line? Typically that won't cause a problem with any program that deals with text files.

~bp
0
 
LVL 17

Author Comment

by:sonawanekiran
ID: 38829827
Actually this text file is using third party tool (I don't know about this). They asked me to remove last blank line. And then I asked to you... :)

Here is one more question for you...

http://www.experts-exchange.com/Programming/Languages/Scripting/Shell/Batch/Q_28012103.html
0
 
LVL 52

Expert Comment

by:Bill Prew
ID: 38830714
0

Featured Post

Are your end users making ugly email signatures?

Have you left it up to your end users to create their own email signatures? Are they forgetting to add the company logo or using garish font colors? Take control and ensure all users have the same email signature.

Question has a verified solution.

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

Suggested Solutions

In this article, I will show you HOW TO: Perform a Physical to Virtual (P2V) Conversion the easy way from a computer backup (image).
How to record audio from input sources to your PC – connected devices, connected preamp to record vinyl discs, streaming media, that play through your audio card: Vista, Windows 7, Windows 8, Windows 8.1 and Windows 10 – both 32 bit & 64.
Windows 8 comes with a dramatically different user interface known as Metro. Notably missing from the new interface is a Start button and Start Menu. Many users do not like it, much preferring the interface of earlier versions — Windows 7, Windows X…
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

863 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

24 Experts available now in Live!

Get 1:1 Help Now