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
 SQL Server 2000 Forums
 SQL Server Development (2000)
 Scan folder for text files

Author  Topic 

tgunther
Starting Member

4 Posts

Posted - 2007-08-10 : 15:09:55
I have written a stored procedure that when executed scans the text files in a folder, grabs the information and places it into a table, then moves the text file to an archive directory. I would now like to make this automatic, so that when a file is placed in the data folder it automatically executes the sp. Is there a way in SQL to have the directory scanned and if a file is added it executes the sp? Thanks in advance!

ashley.sql
Constraint Violating Yak Guru

299 Posts

Posted - 2007-08-10 : 15:16:47
can u post the procedure.

Ashley Rhodes
Go to Top of Page

tkizer
Almighty SQL Goddess

38200 Posts

Posted - 2007-08-10 : 15:43:32
You will need a third party tool to know when a file has been placed in the folder. Otherwise, I guess you could run the stored procedure every minute through a job. Use xp_fileexists to verify if it's there first.

Tara Kizer
Microsoft MVP for Windows Server System - SQL Server
http://weblogs.sqlteam.com/tarad/
Go to Top of Page

tgunther
Starting Member

4 Posts

Posted - 2007-08-10 : 16:41:22
Thanks for the help, I will keep working on this to see if I can figure it out. Here is the stored procedure:

CREATE procedure ImportFiles
@FilePath varchar(1000) = 'c:\Transfer\' ,
@ArchivePath varchar(1000) = 'c:\Transfer\Archive\' ,
@FileNameMask varchar(1000) = '*.txt' ,
@MergeProc varchar(128) = 'MergeBCPData'

AS

set nocount on

declare @ImportDate datetime
select @ImportDate = getdate()

declare @FileName varchar(1000) ,
@File varchar(1000)

declare @cmd varchar(2000)

create table ##Import (s varchar(40), a varchar(40), b varchar(40), c varchar(40), d varchar(40), e varchar(40), f varchar(40), g varchar(40), h varchar(40), i varchar(40), j varchar(40),
k varchar(40), l varchar(40), m varchar(40), n varchar(40), o varchar(40))
create table #Dir (s varchar(8000))

/*****************************************************************/
-- Import file
/*****************************************************************/
select @cmd = 'dir /B ' + @FilePath + @FileNameMask
delete #Dir
insert #Dir exec master..xp_cmdshell @cmd

delete #Dir where s is null or s like '%not found%'
while exists (select * from #Dir)
begin
select @FileName = min(s) from #Dir
select @File = @FilePath + @FileName

select @cmd = 'bulk insert'
select @cmd = @cmd + ' ##Import'
select @cmd = @cmd + ' from'
select @cmd = @cmd + ' ''' + replace(@File,'"','') + ''''
select @cmd = @cmd + ' with (FIELDTERMINATOR= ''\r'''
select @cmd = @cmd + ',ROWTERMINATOR = ''['')'

truncate table ##Import

-- import the data
exec (@cmd)
print @cmd


-- remove filename just imported
delete #Dir where s = @FileName

exec @MergeProc @filename



-- Archive the file
select @cmd = 'move ' + @FilePath + @FileName + ' ' + @ArchivePath + @FileName
exec master..xp_cmdshell @cmd
end
drop table ##Import
drop table #Dir
GO
Go to Top of Page

ashley.sql
Constraint Violating Yak Guru

299 Posts

Posted - 2007-08-10 : 17:20:47
you can even use if exists file exec proc bvy selecting job type as CMDExec else netsend user file does not exist. u can run this every X minutes as per your requirement.

Ashley Rhodes
Go to Top of Page

Jim77
Constraint Violating Yak Guru

440 Posts

Posted - 2007-08-13 : 11:53:01
I have being using this stored procedure filechecker (that doesn't use xp_fileexists) for a while now it is run every hour of the day until it receives a output of CompleteIsaleNormal which then triggers another bulk insert sp.

I know some of the code is not the greatest but what I like about it is it checks not only for a file existing or not but also if it contains 0kb which is quite handy.




CREATE PROCEDURE FileChecker
@itemsalestatus varchar (20) output
as

DECLARE @exists as varchar(300)
DECLARE @result int
DECLARE @rowc as int
DECLARE @path as varchar(300)
DECLARE @sql as varchar(300)
DECLARE @icount as int

select @exists = '"' + 'DIR /B \\server\filename'+ '.'+'def'+'"'
EXEC @result = master..xp_cmdshell @exists , no_output
-- if it does then bulk insert it into the tmpisaleimport table

IF (@result = 0)
begin
select @path =''''+ '\\server\filename'+'.'+'def'+''''
Set @sql='BULK INSERT [tmpisaleimport]FROM '+@path
Exec(@sql)
Set @rowc = @@rowcount
IF @rowc = 0
Begin
-- this occurs when there is a 0kb file although it still physically exists
INSERT INTO file_noexist_table (StoreNumber, [Description])
VALUES ('103','File does exist but has no data')
End
ELSE
Begin
--increment the counter variable if it does exist
SELECT @icount = @icount + 1
INSERT dbo.itemSalesImportStoreSuccess (StoreNumber)
VALUES ('103')
End

end
ELSE
-- if it doesn't plug it into the the error table which shows all failed store imports
begin
INSERT INTO itemsales_stores_input_error(StoreNumber, [Description])
VALUES ('103','File does not exist')
end

IF @icount = 1
begin
--the file exists
set @itemsalestatus = 'CompleteIsaleNormal'
end
Go to Top of Page

tgunther
Starting Member

4 Posts

Posted - 2007-09-05 : 11:02:56
Using the original stored procedure is there a way that as it reads the text file to start from the second line in the text file? I need to skip the first line.
Go to Top of Page
   

- Advertisement -