Solved

Batch file not removing last blank line

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

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 53

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 53

Expert Comment

by:Bill Prew
ID: 38830714
0

Featured Post

Portable, direct connect server access

The ATEN CV211 connects a laptop directly to any server allowing you instant access to perform data maintenance and local operations, for quick troubleshooting, updating, service and repair.

Question has a verified solution.

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

Suggested Solutions

Sometimes drives fill up and we don't know why.  If you don't understand the best way to use the tools available, you may end up being stumped as to why your drive says it's not full when you have no space left!  Here's how you can find out...
When you try to extract and to view the contents of a Microsoft Update Standalone Package (MSU) for Windows Vista, you cannot extract the files from the MSU. Here we are going to explain how to extract those hotfix details without using any third pa…
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…
Windows 8 came with a dramatically different user interface known as Metro. Notably missing from that interface was a Start button and Start Menu. Microsoft responded to negative user feedback of the Metro interface, bringing back the Start button a…

839 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