Copying files from one folder to another using vba
This is because fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file"
is not a file... It's just a string to a dummy file from the looks of it.
If instead the line was
fso.CopyFile fil.Path, "C:\Users\Desktop\Files\Old\" & fil.name
... that might work.
Updated to add:
I just attempted the following using (subbing computer username for below) and had success moving everything into a new folder:
Sub test()
Dim fso As FileSystemObject
Dim fsoFiles As Files
Dim fil As File
Set fso = New FileSystemObject
Set fils = fso.GetFolder("C:\Users\<MY USERNAME>\Desktop\").Files
For Each fil In fils
n = fil.Name
d = fil.DateLastModified
fso.CopyFile fil.Path, fil.ParentFolder & "\test\" & fil.Name
Next fil
End Sub
The only difference here is that I used fil.ParentFolder to get my Desktop and then tossed it into a new folder I created on my desktop (prior to running the script) named "test".
dekio
Updated on December 01, 2020Comments
-
dekio over 3 years
There are some similar posts about this topic, I know. However, I have a code which is different than all codes I have seen here (when talking about this subject).
The error I am receiving is saying that the file couldn't be found. But that's kind of impossible, since I am searching for the file in the same folder I am using as SOURCE in fso.CopyFile.
So I have to fix this error and, if possible, I would like to copy the file to another folder and change the name. For example, if I have the file "Excel.xls", I would like to copy with the name "Excel_old.xls", is that possible using the code below or is it too hard that it's not worth?
This is the code:
Sub CopyFiles() 'Macro to copy all files modified yesterday Dim n As String, msg As String, d As Date Dim fso As Object Set fso = CreateObject("Scripting.FileSystemObject") Set fils = fso.GetFolder("C:\Users\Desktop\Files\").Files 'Verify all files in the folder, check the modification date and then copy 'to another folder (named Old) For Each fil In fils n = fil.Name d = fil.DateLastModified If d >= Date - 1 Then file = n 'The following line is where the error occurs fso.CopyFile "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file" End If Next fil End Sub
-
ZAT over 9 yearsthat's right. But there is a typo, after
fso.CopyFile
it should be eitherfile
orfil.name
according to OP's code. -
JNevill over 9 yearsYep.. should have been
fil.path
notfil.name
. My apologies. I guess I'm a bit rusty with fso. -
dekio over 9 yearsUsed
fso.CopyFile fil.Path, "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\file"
and got the same error, Type Mismatch. I also triedfso.CopyFile fil.Path, "C:\Users\Desktop\Files\file", "C:\Users\Desktop\Files\Old\"
and still getting error 13 -
EStraka over 9 yearsShouldn't it be: "C:\Users\Desktop\Files\" & file, "C:\Users\Desktop\Files\Old\" & file
-
dekio over 9 yearsTried, same error again, I'm searching but I'm really struggling
-
JNevill over 9 yearsEstraka, that would make more sense since this is in a loop. Otherwise the Destination would be overwritten every time a new file is found that passes the If statement. fso.CopyFile only takes two arguments, dekio, both are a string. The Source and the Destination both of which should be the path and full file name.
-
JNevill over 9 yearsI just attempted this myself making a Tools>>Reference to the Microsoft Scripting Runtime so I could get tooltips and autocompletion from my VBE. Everything worked as I anticipated it would. (I didn't use the variables
n
andd
but figured I would include them for fun. The code I used was added to my answer. -
dekio over 9 yearsLet me see if I got it, because I'm not so clever with these things I write very simple codes and I don't actually understand how to work with fso for example. If I check the box Microsoft Scripting Runtime, I get tips in writing codes? Do you have any tip so I can improve my understand in vba and its objects for example? Thank you so much for all the help here!
-
Admin about 3 yearsPotentially how would you set it up so you could check to see if the file exists (latest rev?) in the destination folder before copying it?