Copy and rename a file with partial name start and end name

anichols

Board Regular
Joined
Mar 11, 2021
Messages
87
Office Version
  1. 365
Platform
  1. Windows
Hello all,

I typically move a number of files using VBA using very simple code (sometimes its FileCopy instead of NameAS):
VBA Code:
Name Sheets(2).Range("F26").Value As Sheets(2).Range("F27").Value

The cells values that the code references typically look like this: "C:\Users\drew\Desktop\REPORTING\test01.txt"
Though in some cases there are formulas that drive the particular file name.

Now, I have a new batch of files to move around, but I've encountered a problem, and various things online don't quite seem to be working for me. Now the folder that these file are in stays consistent, but there are unknown characters in each file name. The First part of the file name would be something like: "GL Print", and the end of the file name will be "07-29-2021.pdf" and the middle is unknown. My challenge is that there are dozens of files that have the same Prefix, and dozens also that have the same Suffix. But one file will be the correct file, and once found, I will need to NameAs the file to a new location and name.

Any help getting this right would be greatly appreciated.
 

Excel Facts

Workdays for a market open Mon, Wed, Friday?
Yes! Use "0101011" for the weekend argument in NETWORKDAYS.INTL or WORKDAY.INTL. The 7 digits start on Monday. 1 means it is a weekend.
Try something like this:
VBA Code:
    Dim sourceFolder As String, fileName As String
    sourceFolder = "C:\folder\path\"
    fileName = Dir(sourceFolder & "GL Print*07-29-2021.pdf")
    If fileName <> vbNullString Then
        Name sourceFolder & fileName As "C:\dest\folder\" & fileName
    Else
        MsgBox "File not found"
    End If
 
Upvote 0
I keep getting file not found when I try this. I was playing with something like this, but it's not working either.

VBA Code:
Dim Cell01, Cell02, Cell03, Cell04 As Long
Cell01 = Sheets(2).Range("F26").Value 'Folder Location
Cell02 = Sheets(2).Range("I26").Value 'Known Prefix
Cell03 = Sheets(2).Range("I27").Value 'Known suffix & file extension
Cell04 = Sheets(2).Range("F27").Value 'new filepath and name
FileCopy Cell01 & Cell02 & "*" & Cell03, Cell04
 
Upvote 0
You can't use wildcards with FileCopy. Use wildcards with Dir to first find the actual file name.

Try this (untested):
VBA Code:
Dim Cell01 As String, Cell02 As String, Cell03 As String, Cell04 As String
Cell01 = Sheets(2).Range("F26").Value 'Folder Location
Cell02 = Sheets(2).Range("I26").Value 'Known Prefix
Cell03 = Sheets(2).Range("I27").Value 'Known suffix & file extension
Cell04 = Sheets(2).Range("F27").Value 'new filepath and name
Dim fileName As String
If Right(Cell01,1) <> "\" Then Cell01 = Cell01 & "\"
fileName = Dir(Cell01 & Cell02 & "*" & Cell03)
If fileName <> vbNullString Then
    FileCopy Cell01 & fileName, Cell04
Else
    MsgBox "File not found"
End If
 
Upvote 0
Solution
You can't use wildcards with FileCopy. Use wildcards with Dir to first find the actual file name.

Try this (untested):
VBA Code:
Dim Cell01 As String, Cell02 As String, Cell03 As String, Cell04 As String
Cell01 = Sheets(2).Range("F26").Value 'Folder Location
Cell02 = Sheets(2).Range("I26").Value 'Known Prefix
Cell03 = Sheets(2).Range("I27").Value 'Known suffix & file extension
Cell04 = Sheets(2).Range("F27").Value 'new filepath and name
Dim fileName As String
If Right(Cell01,1) <> "\" Then Cell01 = Cell01 & "\"
fileName = Dir(Cell01 & Cell02 & "*" & Cell03)
If fileName <> vbNullString Then
    FileCopy Cell01 & fileName, Cell04
Else
    MsgBox "File not found"
End If
Awesome! That worked perfectly, thank you!
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,179
Members
453,021
Latest member
Justyna P

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top