Solved

Batch file not removing last blank line

Posted on 2013-01-27
5
1,912 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
[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
  • 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 54

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 54

Expert Comment

by:Bill Prew
ID: 38830714
0

Featured Post

MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

Question has a verified solution.

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

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…
An article on effective troubleshooting
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

733 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