I need a VBA code that can do this
I want this vba code to copy all files from one specific folder to another specific folder
Thanks in advance
I need a VBA code that can do this
I want this vba code to copy all files from one specific folder to another specific folder
Thanks in advance
This is one method. The following will copy the folders and the files between folders.
Sub PerformCopy() '<< Change path as required. First on left is the COPY FROM folder; the second on RIGHT <---//////\\\\\\ '<< Is the COPY TO folder CopyFiles "C:\Users\gagli\Desktop\Test1" & "\", "C:\Users\gagli\Desktop\Test2" & "\" '<<-- change the FROM and TO hard coded paths here End Sub Public Sub CopyFiles(ByVal strPath As String, ByVal strTarget As String) Dim FSO As Object Dim FileInFromFolder As Object Dim FolderInFromFolder As Object Dim Fdate As Long Dim intSubFolderStartPos As Long Dim strFolderName As String Dim dirStr As String Dim ToFolder As String Dim DeleteFile On Error Resume Next Set FSO = CreateObject("scripting.filesystemobject") ToFolder = "C:\Users\gagli\Desktop\Test2" '<< Change path as required.This is the COPY TO folder 'First loop through files For Each FileInFromFolder In FSO.GetFolder(strPath).Files Fdate = Int(FileInFromFolder.DateLastModified) If Fdate < Date - 1 Then FileInFromFolder.Copy strTarget 'Kill FileInFromFolder End If Next 'Next loop throug folders For Each FolderInFromFolder In FSO.GetFolder(strPath).subfolders CopyFiles FolderInFromFolder.Path & "\", strTarget & "\" & strFolderName & "\" For Each FileInFromFolder In FSO.GetFolder(strPath).Files Fdate = Int(FileInFromFolder.DateLastModified) If Fdate < Date - 1 Then FileInFromFolder.Copy strTarget End If Next Next 'Now delete all empty folder in the FROM Folder For Each FolderInFromFolder In FSO.GetFolder(strPath).subfolders RmDir FolderInFromFolder Next End Sub
No spaces in the foldernames.
Sub hsv() Shell "cmd /c copy c:\users\****\documents\folder\*.xls* c:\users\****\documents\folder2\", 0 End Sub
Kind regards, Harry.
I dont want to delete the files from the source folder
Also can we delete the part about date, I have no problem with the dates.
Can we adjust the coding as such please?
Thanks
Last edited by misterno; 10-30-2022 at 01:17 PM.
At the very bottom of the FUNCTION code, eliminate these lines of code :
'Now delete all empty folder in the FROM Folder For Each FolderInFromFolder In FSO.GetFolder(strPath).subfolders RmDir FolderInFromFolder Next
You are welcome.
never mind
Last edited by misterno; 10-30-2022 at 02:28 PM.
Well ... according to your last posted example of code, you've implement the SUBS and the FUNCTION twice. Its like you pasted the code twice.
Did you mean to do that ?
Last edited by misterno; 10-30-2022 at 02:17 PM.
I tested the code here and cannot duplicate the error (.txt files) that you are seeing there.
The code is designed to copy all files without regard of the type.
???
I just tried again with a different CSV file size 126kb and it worked
But the CSV file I am trying to copy is 9K kb, is that the reason why it is not copying?
This big file has 43K lines-rows of data
Last edited by misterno; 10-30-2022 at 02:36 PM.
Logit
Would you please try your code with a large CSV file please?
And let me know
Thanks
Post the .CSV file you have that refuses to be copied. I'll use it and try to understand what is occurring.
Try again but this time delete a good portion of the data in the file. Just need maybe 200 rows of sample data.
I forget what the max limit size is for files here.
Why not just copy the folder and rename?
Good Luck
I don't presume to know what I am doing, however, just like you, I too started somewhere...
One-day, One-problem at a time!!!
If you feel I have helped, please click on the star to left of post [Add Reputation]
Also....add a comment if you like!!!!
And remember...Mark Thread as Solved.
Excel Forum Rocks!!!
Nmbbnnnnn
Jhgbbbb
Last edited by misterno; 10-31-2022 at 12:20 AM.
He is having issues with only the single file. I suspect there is some type corruption affecting the copy process.
???
By chance, is the file format CSV for MacIntosh and not Windows ?
Ideally...Copy Folder contents at once to other Folder...
Change red snippets to your paths
Or if you want to copy one at a time then...Sub J3v16() Dim File As Object Const Path1 = "E:\Sintek\Desktop\Folder1" '! Take note-No Path separator here Const Path2 = "E:\Sintek\Desktop\Folder2" With CreateObject("Scripting.FileSystemObject") .CopyFolder Path1, Path2 '! This will copy Files & SubFolders & Files within... End With End Sub
Sub J3v16() Dim File As Object Const Path1 = "E:\Sintek\Desktop\Folder1\" '! Take note-Path separator here Const Path2 = "E:\Sintek\Desktop\Folder2\" For Each File In CreateObject("Scripting.FileSystemObject").GetFolder(Path1).Files File.Copy Path2 Next File End Sub
Last edited by sintek; 10-31-2022 at 01:51 AM.
Sintek ... thanks for providing misterno a solution.
misterno...No need to quote entire post...just clutters the thread...
Glad to have helped...Tx for rep +
Please remember to mark thread as SOLVED...
Logit...Just giving back...What I was taught...Tx
Last edited by sintek; 10-31-2022 at 02:09 PM.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks