...

View Full Version : adding files to db



dvdljns
10-30-2006, 12: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
ClearDatabase()
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.


<%@LANGUAGE="VBSCRIPT" CODEPAGE="1252"%>
<%
set conn=Server.CreateObject("ADODB.Connection")
conn.Provider="Microsoft.Jet.OLEDB.4.0"
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
next
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">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>downloads</title>
</head>
<body>
<form name="form1" method="post" action="">
<input name="Add" type="submit" id="Add" value="Add Files">

</form>
<%
conn.close
%>
</body>
</html>

BarrMan
10-30-2006, 03: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.

degsy
11-01-2006, 03:19 PM
The full script is here
http://forums.aspfree.com/asp-development-5/setting-up-download-links-from-database-list-138471.html?&highlight=ClearDatabase%28%29



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



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum