Home > Back-end >  FileCopy in Access with wildcards
FileCopy in Access with wildcards

Time:09-23

I am having a heck of a time with something that I think should be so very simple. I think age is getting to me.

All I want to do is in access, run a command that would mimic the following DOS command:

copy c:\1\ABC123-*.txt c:\1\ABC456-*.txt

It seems that filecopy does not support wildcards and for the life of me fso.copyfile just wont work (probably because I am dense). I have also tried to use the copy command using shell and cant get that to work.

To be a little clearer:

The database keeps track of parcels of land. These are "owned" by a land company. On occasion, these parcels are sold from one land company to another. When that happens we have to keep the old records and the new, so the parcel is "copied" to the new land company complete with all its payment history, etc. There are physical documents attached to that record too, and these must appear in BOTH places. I need to copy those PDF files from one parcel number to another (I already have code that duplicates the document entries in the database, I just need to fix the physical files).

So, as an example, parcel 1234 may have a deed, a latenotice, and a survey. Those files would exist as something like 1234-deed.pdf, 1234-latenotice.pdf, and 1234-survey.pdf. I need those files copied to 5678-deed.pdf, 5678-latenotice.pdf, and 5678-survey.pdf after the rest of the database copy code runs.

I need something simple, it only needs to run in that one part of the database so I do not need it to be extensible, or to create a new function, or whatever.

Thanks!

CodePudding user response:

Add reference to "Microsoft Scripting Runtime".

Give this a try:

    Dim F As File
    Dim FSO As New FileSystemObject
    
    Dim FromPath As String
    Dim ToPath As String
    
    FromPath = "C:\1"
    ToPath = "C:\1"

    Debug.Print FSO.GetFolder(FromPath).Files.Count

    For Each F In FSO.GetFolder(FromPath).Files
        If Instr(F.Name,"ABC123-") Then
            FileCopy FromPath & "\" & F.Name, ToPath & "\" & Replace(F.Name, "123", "456")
        End If
    Next
  • Related