Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Read the last line(footer) of a file

Author  Topic 

siprem
Starting Member

6 Posts

Posted - 2008-12-15 : 10:30:27
Hi All,
I am new to sql server and need help in reading the last line of a flat file. This last line contains the line count of the entire file.
I managed to find the number of lines in the file but am not able to get the last line:

Ths code which returns the number of lines is as below:

SET nocount ON

DECLARE @FileName varchar(8000)
DECLARE @NumLines int
DECLARE @XPCmdString varchar(8000)

SET @FileName = 'F:\Import\RawData\DataBase_Txt\1.txt'
SET @XPCmdString = 'find /V /C " " ' + @FileName

CREATE TABLE #XPOutput (XPLineOut varchar(1000))
INSERT INTO #XPOutput EXEC master..xp_cmdshell @XPCmdString
DELETE FROM #XPOutput WHERE XPLineOut IS NULL

SELECT @NumLines = SUBSTRING (XPLineOut, 12 +
len(@FileName) + 2, 1000) FROM #XPOutput
SELECT * FROM #XPOutput
SELECT @NumLines
Drop table #XPOutput

Please help me

darkdusky
Aged Yak Warrior

591 Posts

Posted - 2008-12-15 : 11:48:39


SET nocount ON

DECLARE @FileName varchar(8000)
DECLARE @NumLines int
DECLARE @XPCmdString varchar(8000)

SET @FileName = 'C:\temp\test.txt'
SET @XPCmdString = 'type ' + @FileName
CREATE TABLE #XPOutput (RowID int identity, XPLineOut varchar(1000))
INSERT INTO #XPOutput exec master.dbo.xp_cmdshell @XPCmdString
DELETE FROM #XPOutput WHERE XPLineOut IS NULL

SELECT @NumLines = (Select count(*) from #XPOutput)
SELECT * FROM #XPOutput
SELECT @NumLines
Select *
from #XPOutput
where RowID = @NumLines -1

Drop table #XPOutput
Go to Top of Page
   

- Advertisement -