Results 1 to 3 of 3
  1. #1
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615

    "Kill" statement works in immediate pane, but not in module loop

    This is one for the experts!

    I have been tasked to clean up part of one of our network drives, which involves merging 5 directories into one, and deleting duplicate and unnecessary files.

    I have put the whole drive directory into MS Access, and using queries and VBA I have identified and marked the files to be deleted.

    All this works perfectly, and marks 16,800 / 54,000 files for deletion. Here is the procedure I am trying to use to do the deletions, using the VBA Kill statement:

    Code:
    Sub KillFiles()
      Dim Filename  As String
      Dim db As Database, rst As Recordset
      Dim SQL As String
      On Error GoTo ErrProc
      Set db = CurrentDb
      SQL = "Select * from [All MOSART Files with Directories] where MarkForDeletion and not filedeleted and " & _
          " left([directory Name],15) = 'R:\MOSART FINAL'"
      Set rst = db.OpenRecordset(SQL)
      While Not rst.EOF
        Filename = Chr(34) & rst![directory name] & "\" & rst![file name] & Chr(34)
        Debug.Print "Kill " & Filename
        Kill Filename
        rst.Edit
          rst!filedeleted = True
        rst.Update
        rst.MoveNext
      Wend
      rst.Close
      Set db = Nothing
      Exit Sub
    ErrProc:
      Debug.Print "Error " & Err.Description & " when deleting " & vbCrLf & Filename
      rst.Close
      Set db = Nothing
    End Sub
    However, when I run it, the code fails on the first file, with a "File not found" error. I know the file name is correct, because the debug.print shows it correctly.

    Here is where it gets odd: if in the immediate window I click on the line that debug.print has produced, (see bold below) and execute it, it works - the file is deleted.

    kill "R:\MOSART FINAL JG\5555-106 Briefings & Conf\fsa25A.tmp" (this works in the immediate window)

    A little test shows that the simplest of the simple:

    Sub x()


    Dim txtFilename As String
    txtFilename = "R:\MOSART FINAL JG\5555-101 Budget\dir budget Qtr - 20 Jun 01.xls"
    Kill txtFilename
    End Sub

    does work. I'm thinking it may be a timing issue, where I try the second Kill before the first one has completed. I put a Doevents after the Kill, but no luck.

    Any ideas?

    Thanks!

    John

  2. #2
    rpeare is offline VIP
    Windows XP Access 2003
    Join Date
    Jul 2011
    Posts
    5,442
    Have you tried using filesystemobject commands instead? I'm not a huge fan of the kill command because filesystemobject is much more versatile

    You'd have to set it up with

    dim fs
    set fs = createobject("scripting.filesystemojbect")

    then to delete you'd use

    fs.deletefile txtFileName

    also you do not need to have the chr(34) around your filename definition

    use the snippet below, you've already defined filename as a string so you do not need to put " marks around it, in essence you're issing the command

    kill "filename"

    instead of

    kill filename


    Code:
    Filename = rst![directory name] & "\" & rst![file name]

  3. #3
    John_G is offline VIP
    Windows XP Access 2003
    Join Date
    Oct 2011
    Location
    Ottawa, ON (area)
    Posts
    2,615
    Many thanks!!!

    The quotes around the file name were not only not needed, they were wrong!

    Once I took them off, all was well.


    Thanks again

    John

Please reply to this thread with any new information or opinions.

Similar Threads

  1. Export "Query or Report" to a "Delimited Text File"
    By hawzmolly in forum Import/Export Data
    Replies: 3
    Last Post: 08-31-2012, 08:00 AM
  2. Replies: 4
    Last Post: 08-17-2012, 05:27 PM
  3. Replies: 11
    Last Post: 03-29-2012, 02:32 PM
  4. "Not" criteria works for one line, but not two
    By avarusbrightfyre in forum Queries
    Replies: 1
    Last Post: 02-02-2012, 10:56 AM
  5. Replies: 2
    Last Post: 11-10-2011, 07:45 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Other Forums: Microsoft Office Forums