FileCopy Error 52 Bad File name or number

mark hansen

Well-known Member
Joined
Mar 6, 2006
Messages
534
Office Version
  1. 2016
Platform
  1. Windows
I'm trying to do something that I thought was straight forward. I'm trying to move and rename a file from one folder to another. After doing the Google search I found:
Code:
Sub Move_Rename_One_File()
'You can change the path and file name
    Name "C:\Users\Ron\SourceFolder\Test.xls" As "C:\Users\Ron\DestFolder\TestNew.xls"
End Sub
On Ron de Bruin web site. This give me an Error 5 Invalid procedure error

Now I'm the trying the FileCopy and using Ron's example of
Code:
Sub Copy_One_File()
    FileCopy "C:\Users\Ron\SourceFolder\Test.xls", "C:\Users\Ron\DestFolder\Test.xls"
End Sub

I came up with the following for my code
Code:
   NewName = HoldingPath & DateXTime & ".txt"
   OldName = ImportPath & "Testing.txt"
   
MsgBox (OldName & vbCr & vbCr & NewName)

     FileCopy OldName, NewName
Which give me the error
The MsgBox shows - (under type in the Watch box is "Variant/string" for OldName and "String" for NewName. )

OldName = "\\{servername}\pad\Coding\IPR Deficiency\Chart Deficiency information\Data Files\Testing.txt"
NewName = "\\{servername}\pad\Coding\IPR Deficiency\Chart Deficiency information\Data Files\Old Import Files\20180913-11:14:11.txt"

I have the following Dim on top:

Dim OldName, NewName As String

What am I overlooking? Why is this kicking my kester?

Thanks for any help
Mark
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
Thanks Simon. Can do a manual copy and paste the file in the destination folder so I don't think network permissions. (even renamed it too.) I modified the code and took out the "oldName" and "NewName variables and put in the text and got the same message.

I wonder why NewName is a string, and OldName is a Variant/String in the Add watch window. Is that causing a problem?
 
Upvote 0
This
Code:
Dim OldName, NewName As String
is only declaring Newname as a string, OldName is variant. It should really be
Code:
Dim OldName As String, NewName As String
Although that wouldn't cause the error.
What error are you getting?
 
Upvote 0
OH, I'll make that change in the DIM statement

I think I figured it out.... I had colons in the time that's used as the file name. Then it hit me, you can have colons in a file name. Boy do I feel stupid. I changed the colons to a dash and it seems to work. More testing to come.

Thanks.
 
Upvote 0
OH, I'll make that change in the DIM statement

I think I figured it out.... I had colons in the time that's used as the file name. Then it hit me, you can have colons in a file name. Boy do I feel stupid. I changed the colons to a dash and it seems to work. More testing to come.

Thanks.

Just to close the loop, I guess most of you noticed the typo in my last response... I meant you CAN'T have colons in a file name.
 
Upvote 0

Forum statistics

Threads
1,223,904
Messages
6,175,295
Members
452,632
Latest member
jladair

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