Solved

Batch file not removing last blank line

Posted on 2013-01-27
5
1,812 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 51

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 51

Expert Comment

by:Bill Prew
ID: 38830714
0

Featured Post

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
In this video, we discuss why the need for additional vertical screen space has become more important in recent years, namely, due to the transition in the marketplace of 4x3 computer screens to 16x9 and 16x10 screens (so-called widescreen format). …
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

757 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

19 Experts available now in Live!

Get 1:1 Help Now