Results 1 to 7 of 7
  1. #1
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104

    Error on using Kill to remove all files from directory if directory is empty

    I have (kinda) the following code:

    Code:
    Kill(Me.txtFullPath.Value & "\*.*")
    to clear out all the files out of a specified folder. I then use:

    Code:
    RmDir(Me.txtFullPath.Value)
    to remove the folder itself. I found out the hard way that (to my understanding) you had to clear out all files out of the directory before executing the RmDir command, or it would error out with a Permissions error.

    It works fine as-is, as long as there is actually at least one file in the folder in question. If there are no files, and merely the existance of the folder (the folder will always exist, by the way), it errors out with a "File not found" error. Any ideas how to fix it?



    I had thought about using Nz(), but I don't want to use it on that and blow something up.

  2. #2
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,921
    Use Dir() first.
    (RG for short) aka Allan Bunch Previous MS Access MVP - WinXP Pro, Win7 Pro Win10 Pro - acXP, ac07, ac10, ac13
    How to mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

  3. #3
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    It says "Error: Expected Sub, function, or property" when I use

    Code:
    Dir(Me.txtFullPath.Value & "\")
    or

    Code:
    Dir(Me.txtFullPath.Value)
    And if I try just Dir(), it errors out as well.

    I also tried checking to see if the string length of the Dir of it was 0, but it gave me a type mismatch (read in help about the zero-length string thing)

  4. #4
    boblarson is offline --------
    Windows 7 64bit Access 2010 32bit
    Join Date
    Jun 2011
    Posts
    1,272
    Umm, you would use the same syntax as your kill command. So, did you try that?
    Code:
    If Dir(Me.txtFullPath.Value & "\*.*") = vbNullString Then
       Kill(Me.txtFullPath.Value & "\*.*")
    End If

  5. #5
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    That's what I used to check the string length as well (exact same code, actually), but for some reason it's giving me a type mismatch error on the first line.

  6. #6
    justinwright is offline Competent Performer
    Windows XP Access 2007
    Join Date
    Jul 2010
    Posts
    104
    ...oh wow, I just realized I'm retarded. It's been a long day already

    I had used
    Code:
    dim folder, files, dir
    at the top for something else I was doing. I changed the reference to not take up dir as a variable.

    It worked like a charm, thanks guys!

    EDIT:

    By the way, I ended up using the code:

    Code:
    If dir(Me.txtFullPath.Value & "\*.*") = vbNullString Then
    RmDir (Me.txtFullPath.Value)
    Else
    Kill (Me.txtFullPath.Value & "\*.*")
    RmDir (Me.txtFullPath.Value)
    End If

  7. #7
    RuralGuy's Avatar
    RuralGuy is offline Administrator
    Windows 7 64bit Access 2010 32bit
    Join Date
    Mar 2007
    Location
    8300' in the Colorado Rocky Mountains
    Posts
    12,921
    Thanks for posting back with your success.
    (RG for short) aka Allan Bunch Previous MS Access MVP - WinXP Pro, Win7 Pro Win10 Pro - acXP, ac07, ac10, ac13
    How to mark the thread as Solved!
    Teaching is not filling a bucket but lighting a fire.
    Borrowed quote..."Docendo discimus"

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

Similar Threads

  1. IP Address Directory
    By neo651 in forum Queries
    Replies: 8
    Last Post: 04-21-2011, 12:28 PM
  2. Directory Database
    By bwaters in forum Access
    Replies: 1
    Last Post: 10-29-2010, 05:51 AM
  3. Change image directory
    By amer in forum Access
    Replies: 3
    Last Post: 06-09-2010, 07:01 AM
  4. Make directory or folder
    By thart21 in forum Programming
    Replies: 1
    Last Post: 04-07-2010, 05:37 PM
  5. New directory with the name from field
    By np1111 in forum Programming
    Replies: 14
    Last Post: 02-25-2010, 07:06 AM

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 - Senior Forums