|
Gregory Asks:
I need to write a one time program that does the following:
Recurse through all subdirectories of D:\ and place the name of each file
in FIELD1 and the contents of each file in FIELD2. I am working with a
Access97 database. Can anyone point me to any resources/examples? Thanks
so much!
In Jest, Rob Replies:
got a copy of the "iloveyou" virus that went around a while back? It
contained the necessary code to recurse a directory structure.
http://www.aspfree.com/devlinks/search.asp?file404=dir;list#FileSystemObject
and also the ADO Category for throwing the info into the database.
Phil Wants To Know:
what sort of files? text, ms word.....?
Gregory Responds:
Sorry...these are simple (small) ascii files.
Paul Lays It All Out With:
<!-- METADATA TYPE="TypeLib" FILE="c:\Program Files\Common
Files\System\ado\msado15.dll" -->
<%
Dim objFs
Dim strSql
Set objFs = Server.CreateObject("Scripting.FileSystemObject")
strPath = "d:\"
RecurseFolder strPath
Set objFile = Nothing
Set objFolder = Nothing
Set objFs = Nothing
WriteData strSql
Sub EnumerateFiles(FolderObject)
For Each File in FolderObject.Files
strFileName = File.Name
Set objFile = objFs.OpenTextFile(strPath & strFileName)
strFileContent = SqlFixer(objFile.ReadAll)
strSql = strSql & "INSERT INTO your_table (field1,field2) "
& _
"VALUES ('" & strFileName & "','" & strFileContent &
"');" & vbCrLf
Next
End Sub
Function RecurseFolder(CurrentPath)
Set objFolder = objFs.GetFolder(CurrentPath)
If objFolder.SubFolders.Count > 0 Then
For Each Folder In objFolder.SubFolders
EnumerateFiles objFolder
RecurseFolder Folder.Path
Next
End If
End Function
Sub WriteData(SqlString)
Set objConn = Server.CreateObject("ADODB.Connection")
With objConn
.Open your_dsn
.Execute strSql,,adCmdText+adExecuteNoRecords
.Close
End With
Set objConn = Nothing
End Sub
Function SqlFixer(TheText)
On Error Resume Next
SqlFixer = Replace(TheText,"'","''")
End Function
%>
Gregory, Indebted to Paul, Replies:
Much thanks goes to Paul for this one. Works like a charm and I learned a
lot from the code!!!
Like a True Hero, Paul Ends With:
Glad to be of help, and I hope it's useful to other people too.
This conversation string was taken from the 15Seconds ASP Listserv on 2/7/01. If you have an ASP-related question or would like to share some of your knowledge with others, you may join the list by clicking here.
|