View Full Version : adding files to db

10-30-2006, 01:46 PM
I a trying to get this to read a folder then write the files to a list in a access db.
Also one of the things I need it to do is clear the db before it adds the files something like.

If Len(Request.Form("Add")) Then
sql = "DELETE FROM filelist"
conn.Execute sql
Response.Write "Database emptied"

But not sure how t add it where it will clear the db then add the files maybe I will try adding it to beinning of the function. Where am I mesing up.

set conn=Server.CreateObject("ADODB.Connection")
conn.Open Server.MapPath("/database/downloads.mdb")
Function insertFilesToDatabase()
dim fs,fo,x
set fs=Server.CreateObject("Scripting.FileSystemObject")
set fo=fs.GetFolder(Server.MapPath("/downloads"))
for each x in fo.files
'Print the name of all files in the test folder
sql = "INSERT INTO filelist (filename, filesize, filepath, filedescription) VALUES ("
sql = sql & "'" & x.Name & "'," & x.Size & ",'" & x.Path & "','" & x.Type & "'"
sql = sql & ")"
Response.Write sql & "<br>"
conn.Execute sql
set fo=nothing
set fs=nothing
Response.Write "Files Added!<br><hr><br>"
insertFilesToDatabase = true
End Function
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<form name="form1" method="post" action="">
<input name="Add" type="submit" id="Add" value="Add Files">


10-30-2006, 04:10 PM
To delete the database table use:

sql = "DROP TABLE filelist"

And your adding files code looks fine.
Just call the function clearDatabase() inside the insertFilesToDatabase function.

11-01-2006, 04:19 PM
The full script is here

If Len(Request.Form("Add")) > 0 Then
Call ClearDatabase()
If insertFilesToDatabase = true Then
Call outputFileList
End If
End If