Results 1 to 8 of 8
  1. #1
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts
    As you may know, if you try to do FileCopy on an existing, empty folder, the operation fails. In order to get around this, I'd like to detect if the folder is empty, if so, delete and then do MakDir & FileCopy. If it isn't empty, i don't want to delete or recreate the folder.

    TIA

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Steve,

    If dir("drive:\path\*.*) = "" then 'File is empty

    End if
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks for the tip. i ended up doing this:

    On Error Resume Next <-- this forces the routine to complete; otherwise I get file/path error and execution halts.

    If Dir("C:\RUGCalculator\") = "" Then
    MkDir ("C:\RUGCalculator\")
    Else
    If Dir("C:\RUGCalculator\*.*") = "" Then
    RmDir ("C:\RUGCalculator\")
    MkDir ("C:\RUGCalculator\")
    End If
    End If

  4. #4
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Quote Originally Posted by steve_skelton13 View Post
    Thanks for the tip. i ended up doing this:

    On Error Resume Next <-- this forces the routine to complete; otherwise I get file/path error and execution halts.

    If Dir("C:\RUGCalculator\") = "" Then
    MkDir ("C:\RUGCalculator\")
    Else
    If Dir("C:\RUGCalculator\*.*") = "" Then
    RmDir ("C:\RUGCalculator\")
    MkDir ("C:\RUGCalculator\")
    End If
    End If

    If you drop the trailing \ on the check for the Directory, e.g. If Dir("C:\RUGCalculator") = "" Then
    it should eliminate the error, actually you can remove them from the RmDir & MkDir also!
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  5. #5
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts
    well, you still need the On Error Resume Next:

    On Error Resume Next

    If Dir("C:\RUGCalculator") = "" Then
    MkDir ("C:\RUGCalculator")
    End If

    no need to check if empty - the system will place files in an empty existing folder, overwrite files in an existing folder or re-create the folder if it doesn't exist.

  6. #6
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Sorry for the confusion. When checking for a directory do as follows:

    Code:
    Sub CreateDir()
      
    If Dir("G:\BEKDocs\RUGCalculator", vbDirectory) = "" Then
       MkDir "G:\BEKDocs\RUGCalculator"
    Else
       MsgBox "Directory Exists", vbOKOnly
    End If
    
    End Sub
    This code executes w/o errors whether the directory exists or not.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  7. #7
    5 Star Lounger
    Join Date
    Jan 2001
    Location
    austin, Texas, USA
    Posts
    1,029
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Just to make sure we're talking about the same thing, the issue i have is, if a directory exists but has no files, i get a file/path error on trying to place files in the directory. if the directory does not exist, the system creates it and then places files - no error. if the directory exists and contains some file (any file), i can write new files to it. I don't understand why this happens, as a newly created directory is also empty. It's a little weird. I don't like fixing it by ignoring the error, but that does fix it, in the sense that it behaves as desired.

  8. #8
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Steve,

    This is driving me nuts! It seems that FileCopy won't take wildcards? As I remember using it with XP and Access 2002 that worked just fine.

    You might want to check this out.

    The only way I could get FileCopy to work was with full path/filenames on both the source and destination.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •