Sebastion
asked on
FileSystemObject - DeleteFile problem
Hello,
I have some code in a script which is executed every 15 or so minutes. This code erases documents off the server which are older than a day. Unfortunatly, I cannot get the DeleteFile statement to work, as it always comes up with "File cannot be found" even if I point directly to the file. (Ultimately, though, I would want to use wildcards).
Heres the code I have:
--------------------------
Dim objFile
Set objFile = CreateObject("Scripting.Fi leSystemOb ject")
Set rsClean = Server.CreateObject("ADODB .Recordset ")
SQLClean = "SELECT * FROM CleanupData WHERE NOT CleanupID = 1"
'Set the lock type so that the record is locked by ADO when it is updated
rsClean.LockType = 3
rsClean.Open SQLClean, objConn
Do While Not rsClean.EOF
IF DateDiff("d", rsClean("RecordDate"), Now() > 1) THEN
'objFile.DeleteFile "..\EngagementDocs\" & rsClean("RandomNumber") & "*.doc", True
objFile.DeleteFile "..\EngagementDocs\Ew9681m 130EO21969 c93_Author itytoAcces sClientInf o.doc", True
END IF
rsClean.MoveNext
loop
rsClean.Close
Set rsClean = nothing
Set objConn = nothing
-------------------------- ---
An example document which would be on the server would be something like my hardcoded document (Ew9681m130EO21969c93_Auth oritytoAcc essClientI nfo.doc). Each person who uses the system has a random number and they are able to create a variety of documents. Their random number, however, changes on a daily basis, so the existing documents would be worthless after a day. The "RecordDate" field is saved after the user has executed a script which creates the document.
Any help would be appreciated
I have some code in a script which is executed every 15 or so minutes. This code erases documents off the server which are older than a day. Unfortunatly, I cannot get the DeleteFile statement to work, as it always comes up with "File cannot be found" even if I point directly to the file. (Ultimately, though, I would want to use wildcards).
Heres the code I have:
--------------------------
Dim objFile
Set objFile = CreateObject("Scripting.Fi
Set rsClean = Server.CreateObject("ADODB
SQLClean = "SELECT * FROM CleanupData WHERE NOT CleanupID = 1"
'Set the lock type so that the record is locked by ADO when it is updated
rsClean.LockType = 3
rsClean.Open SQLClean, objConn
Do While Not rsClean.EOF
IF DateDiff("d", rsClean("RecordDate"), Now() > 1) THEN
'objFile.DeleteFile "..\EngagementDocs\" & rsClean("RandomNumber") & "*.doc", True
objFile.DeleteFile "..\EngagementDocs\Ew9681m
END IF
rsClean.MoveNext
loop
rsClean.Close
Set rsClean = nothing
Set objConn = nothing
--------------------------
An example document which would be on the server would be something like my hardcoded document (Ew9681m130EO21969c93_Auth
Any help would be appreciated
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
1) It's bound to happen that for whatever reason a doc that's in your db may NOT be in the folder, so I added logic so your page does not error out.
2) I also moved the datediff comparison to the sql statement and limited the fields returned from * to only the RandomNumber (no point in returning all the fields if you're not going to use them)
=== code sample below ====
Dim objFile
Set objFile = CreateObject("Scripting.Fi leSystemOb ject")
Set rsClean = Server.CreateObject("ADODB .Recordset ")
SQLClean = "SELECT RandomNumber FROM CleanupData WHERE NOT CleanupID = 1 and (datediff(dd,RecordDate,ge tdate()) > 1)"
'Set the lock type so that the record is locked by ADO when it is updated
rsClean.LockType = 3
rsClean.Open SQLClean, objConn
'-- find the absolute path to your folder
sPath = Server.MapPath("..\Engagem entDocs\")
Do While Not rsClean.EOF
'-- concat path and .doc name
sFullName = (sPath & "\" & rsClean("RandomNumber") & "*.doc")
'-- check if file exists
If objFile.FileExists(sFullNa me) Then
'-- delete file
objFile.DeleteFile(sFullNa me), True
Else
'-- notify that file was not found (you can also write this out to a log file)
Response.Write("File" & sFullName & " could NOT be found<br>")
End If
rsClean.MoveNext
loop
rsClean.Close
Set rsClean = nothing
Set objConn = nothing
2) I also moved the datediff comparison to the sql statement and limited the fields returned from * to only the RandomNumber (no point in returning all the fields if you're not going to use them)
=== code sample below ====
Dim objFile
Set objFile = CreateObject("Scripting.Fi
Set rsClean = Server.CreateObject("ADODB
SQLClean = "SELECT RandomNumber FROM CleanupData WHERE NOT CleanupID = 1 and (datediff(dd,RecordDate,ge
'Set the lock type so that the record is locked by ADO when it is updated
rsClean.LockType = 3
rsClean.Open SQLClean, objConn
'-- find the absolute path to your folder
sPath = Server.MapPath("..\Engagem
Do While Not rsClean.EOF
'-- concat path and .doc name
sFullName = (sPath & "\" & rsClean("RandomNumber") & "*.doc")
'-- check if file exists
If objFile.FileExists(sFullNa
'-- delete file
objFile.DeleteFile(sFullNa
Else
'-- notify that file was not found (you can also write this out to a log file)
Response.Write("File" & sFullName & " could NOT be found<br>")
End If
rsClean.MoveNext
loop
rsClean.Close
Set rsClean = nothing
Set objConn = nothing
That error actually means "permission denied". You probably need to impersonate a user, as the script is probably running within the context of the IWAM_<computername> or IUSR_<computername> account. I had this same problem and after creating a simple little DLL, it worked perfectly. See this article:
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q248/1/87.ASP&NoWebContent=1
Regards,
lowRider
http://support.microsoft.com/default.aspx?scid=http://support.microsoft.com:80/support/kb/articles/q248/1/87.ASP&NoWebContent=1
Regards,
lowRider
INSTEAD OF THE CODE BELOW, CHANGE IT AS ..
Do While Not rsClean.EOF
IF DateDiff("d", rsClean("RecordDate"), Now() > 1) THEN
'objFile.DeleteFile "..\EngagementDocs\" & rsClean("RandomNumber") & "*.doc", True
objFile.DeleteFile "..\EngagementDocs\Ew9681m 130EO21969 c93_Author itytoAcces sClientInf o.doc", True
END IF
rsClean.MoveNext
loop
NEW CODE::
Do While Not rsClean.EOF
IF DateDiff("d", rsClean("RecordDate"), Now() > 1) THEN
objFile.DeleteFile Server.Mappath("../Engagem entDocs/Ew 9681m130EO 21969c93_A uthorityto AccessClie ntInfo.doc "), True
END IF
rsClean.MoveNext
loop
Do While Not rsClean.EOF
IF DateDiff("d", rsClean("RecordDate"), Now() > 1) THEN
'objFile.DeleteFile "..\EngagementDocs\" & rsClean("RandomNumber") & "*.doc", True
objFile.DeleteFile "..\EngagementDocs\Ew9681m
END IF
rsClean.MoveNext
loop
NEW CODE::
Do While Not rsClean.EOF
IF DateDiff("d", rsClean("RecordDate"), Now() > 1) THEN
objFile.DeleteFile Server.Mappath("../Engagem
END IF
rsClean.MoveNext
loop
ASKER
Hey,
Sorry it took awhile for me to answer, when I posted this question I didn't get back to the computer in order to test some of the replies. I accepted the first answer which worked for me, so thanks c_swanky. I never could get my head around the amount of backslashes in those statements (ie, your solution would, at first glance, make the path "..\EngagementDocs\\Number .doc", with the double back slashes). I had a similar instance with that before in some other code.
Sorry it took awhile for me to answer, when I posted this question I didn't get back to the computer in order to test some of the replies. I accepted the first answer which worked for me, so thanks c_swanky. I never could get my head around the amount of backslashes in those statements (ie, your solution would, at first glance, make the path "..\EngagementDocs\\Number
D:\html\doc\EngagementDocs