dbTalk Databases Forums  

Auto searching a directory and uploading a file into a database

microsoft.public.sqlserver.dts microsoft.public.sqlserver.dts


Discuss Auto searching a directory and uploading a file into a database in the microsoft.public.sqlserver.dts forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Dave
 
Posts: n/a

Default Auto searching a directory and uploading a file into a database - 08-25-2006 , 12:42 PM






I need an example or some ideas - how to do this -

I need to run a job that searches a directory every hour to see if any files
are available for upload - and then upload it into a SQL data base - and
then move the file to another directory.

I know how to manaully upload the file using DTS - and move the file to
another location - i don't know how to search a directory and if there is a
file there - to upload it.

Thanks

Dave



Reply With Quote
  #2  
Old   
Charles Kangai
 
Posts: n/a

Default RE: Auto searching a directory and uploading a file into a database - 08-27-2006 , 04:01 PM






Hi Dave,

Do you know the name of the file? If you do, you can use the
Scripting.FileSystemObject's FileExists method to check whether the file
exists or not. If so, you make the task successful, if not you make it fail.
Something like:

Function Main()
....
set oFSO = CreateObject("Scripting.FileSystemObject")
if oFSO.FileExists("c:\Myfolder\MyFile.txt") then
Main = DTSTaskExecResult_Success
else
Main = DTSTaskExecResult_Failure
end if
End function

You then use Success precedent constraint to go to the next task which
imports the file. The import only happens when the file is found. You then
schedule the package to run every hour.

If you don't know the name of the file, you can use the Folder object's
Files property to get a collection of the files in the folder. You then loop
around the files, each time setting the DataSource property of the Text
Source connection to the file name and importing the file.

Here is a code snippet:
option explicit
Function Main()

Dim oFS, colFile, oFolder, oFile, position, oPkg, cFile, con
Set oPkg = DTSGlobalVariables.Parent
set con = oPkg.Connections("SourceFile")
Set oFS = CreateObject("Scripting.FileSystemObject")
Set oFolder = oFS.GetFolder(DTSGlobalVariables("gvFolder").Value )
set colFile = oFolder.Files
position = 0
for each cFile in colFile
position = position + 1
if position = cint(DTSGlobalVariables("gvFileCurrent").value) then
set oFile = cFile
con.DataSource = DTSGlobalVariables("gvFolder").Value & "\" & oFile.Name
end if
next
if position > DTSGlobalVariables("gvFileCurrent").value then
Main = DTSTaskExecResult_Success
exit function
end if

Main = DTSTaskExecResult_Success
End Function

Hope this helps.

Charles Kangai, MCT, MCDBA

Author of Learning Tree's 4-day course: "SQL Server 2005 Integration
Services"
Author of Learning Tree's 4-day course: "SQL Server Reporting Services"






"Dave" wrote:

Quote:
I need an example or some ideas - how to do this -

I need to run a job that searches a directory every hour to see if any files
are available for upload - and then upload it into a SQL data base - and
then move the file to another directory.

I know how to manaully upload the file using DTS - and move the file to
another location - i don't know how to search a directory and if there is a
file there - to upload it.

Thanks

Dave




Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.