• Detecting folder contents before FileCopy

    Home » Forums » AskWoody support » Productivity software by function » MS Access and database help » Detecting folder contents before FileCopy

    Author
    Topic
    #472465

    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

    Viewing 5 reply threads
    Author
    Replies
    • #1250987

      Steve,

      If dir(“drive:path*.*) = “” then ‘File is empty

      End if

      May the Forces of good computing be with you!

      RG

      PowerShell & VBA Rule!
      Computer Specs

    • #1251013

      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

      • #1251015

        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!
        Computer Specs

    • #1251042

      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.

    • #1251048

      Sorry for the confusion. When checking for a directory do as follows:

      Code:
      Sub CreateDir()
        
      If Dir("G:BEKDocsRUGCalculator", vbDirectory) = "" Then
         MkDir "G:BEKDocsRUGCalculator"
      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!
      Computer Specs

    • #1251051

      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.

    • #1251064

      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!
      Computer Specs

    Viewing 5 reply threads
    Reply To: Detecting folder contents before FileCopy

    You can use BBCodes to format your content.
    Your account can't use all available BBCodes, they will be stripped before saving.

    Your information: