Solved

FileSystemObject - DeleteFile problem

Posted on 2003-12-01
6
826 Views
Last Modified: 2009-12-16
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.FileSystemObject")

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\Ew9681m130EO21969c93_AuthoritytoAccessClientInfo.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_AuthoritytoAccessClientInfo.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
0
Comment
Question by:Sebastion
6 Comments
 
LVL 4

Expert Comment

by:c_swanky
ID: 9855008
Try adding the full path

D:\html\doc\EngagementDocs\
0
 
LVL 4

Accepted Solution

by:
c_swanky earned 100 total points
ID: 9855085
'-- find the absolute path to your folder
sPath = Server.MapPath("..\EngagementDocs\")

Do While Not rsClean.EOF
     IF DateDiff("d", rsClean("RecordDate"), Now() > 1) THEN
          'objFile.DeleteFile(sPath & "\" & rsClean("RandomNumber") & "*.doc"), True
     END IF

     rsClean.MoveNext

loop
0
 
LVL 4

Expert Comment

by:c_swanky
ID: 9855184
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.FileSystemObject")

Set rsClean = Server.CreateObject("ADODB.Recordset")
SQLClean = "SELECT RandomNumber FROM CleanupData WHERE NOT CleanupID = 1 and (datediff(dd,RecordDate,getdate()) > 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("..\EngagementDocs\")

Do While Not rsClean.EOF

      '-- concat path and .doc name
      sFullName = (sPath & "\" & rsClean("RandomNumber") & "*.doc")

      '-- check if file exists
      If objFile.FileExists(sFullName) Then
      
            '-- delete file
                                objFile.DeleteFile(sFullName), 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
0
Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

 
LVL 1

Expert Comment

by:lowRider
ID: 9855478
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
0
 
LVL 2

Expert Comment

by:Sairam_S
ID: 9855759
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\Ew9681m130EO21969c93_AuthoritytoAccessClientInfo.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("../EngagementDocs/Ew9681m130EO21969c93_AuthoritytoAccessClientInfo.doc"), True
     END IF

     rsClean.MoveNext

loop
0
 

Author Comment

by:Sebastion
ID: 9862613
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.
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Hello, all! I just recently started using Microsoft's IIS 7.5 within Windows 7, as I just downloaded and installed the 90 day trial of Windows 7. (Got to love Microsoft for allowing 90 days) The main reason for downloading and testing Windows 7 is t…
This demonstration started out as a follow up to some recently posted questions on the subject of logging in: http://www.experts-exchange.com/Programming/Languages/Scripting/JavaScript/Q_28634665.html and http://www.experts-exchange.com/Programming/…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, just open a new email message. In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now