Copying all folders and files from one location to another (2 Viewers)

jazsriel

Member
Local time
Today, 04:19
Joined
Dec 21, 2020
Messages
62
I have been scouring the web and forums and am unable to find anything to my issue. I am in need of a way to push a button on a form and have it copy all files and folders in one location and then essentially paste (generic MS term I know) them to another location. The only way I know how to do this is essentially using:
If Dir("C:\Folderlocationtocheck") = "" Then
MkDir ("C:\Createmissingfolder\")
End If
to create each and every single folder in the new location then using:
FileSystemObject.CopyFile "c:\oldfilelocation\*.*", "c:\newfilelocation\"
However, the problem is this takes way to much time to setup as I have to map out each folder and file location to get them to copy over. There is also the problem that new files and folders are created and without including them in the cumbersome way I currently know how to do this then they would never be copied when they are needed to be. Any ideas on this problem? The only things I have found so far are partial ideas that some folks have had, but it seems as though they gave up without a solution or guidance to their questions.
 

ebs17

Well-known member
Local time
Today, 11:19
Joined
Feb 7, 2020
Messages
1,946
For mass copying there is the old xcopy from DOS times or the newer robocopy.

 
Last edited:

theDBguy

I’m here to help
Staff member
Local time
Today, 02:19
Joined
Oct 29, 2018
Messages
21,473
However, the problem is this takes way to much time to setup as I have to map out each folder and file location to get them to copy over.
If by "mapping" you're talking about writing each file name and location to a table, I am not sure that is necessary. You should be able to just loop through the files in a folder. Take a look at this demo for an idea on how to do that.
 

The_Doc_Man

Immoderate Moderator
Staff member
Local time
Today, 04:19
Joined
Feb 28, 2001
Messages
27,186
Look into the FileSystemObject if you don't want to use a SHELL to do the XCOPY or ROBOCOPY. You can use the properties of an FSO to get a folder, look at the files in that folder as a collection, and issue copy commands.


Among other things, FSO has a "move folder" function if you wanted to subsequently delete the files from the source. From the page I referenced, you can browse the offerings using the topic-list on the left of that web page. It should make your life easier.
 

jazsriel

Member
Local time
Today, 04:19
Joined
Dec 21, 2020
Messages
62
Look into the FileSystemObject if you don't want to use a SHELL to do the XCOPY or ROBOCOPY. You can use the properties of an FSO to get a folder, look at the files in that folder as a collection, and issue copy commands.


Among other things, FSO has a "move folder" function if you wanted to subsequently delete the files from the source. From the page I referenced, you can browse the offerings using the topic-list on the left of that web page. It should make your life easier.
Okay, after looking into this method of copyfolder and using that, it copies all the folders I need over, however it is renaming them to something I am not wanting.

I am using the following:
Private Sub Command35_Click()
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFolder "C:\oldfilelocation\*", "C:\newfilelocation\"
Set fso = Nothing
Set fso = CreateObject("Scripting.FileSystemObject")
fso.CopyFile "C:\oldfilelocation\*.*", "C:\newfilelocation\"
Set fso = Nothing
End Sub

In the oldfilelocation: the 2 folders I have in there are called Public Documents and the other one is Public Downloads, after the files are copied to the newfilelocation, they are being renamed Documents and Downloads. I still need them to be labelled as Public Downloads and Public Documents. Any ideas on how to fix this? Is there a way to rename them to what I want after the copy is done, because these particular folders will always be named Public Downloads and Public Documents.

I did think it was because I was testing the copy to my own user desktop and maybe that was causing it to change the name, but then I just went and copied it to the public locations that they would be located at, and it changes the names there to.
 
Last edited:

moke123

AWF VIP
Local time
Today, 05:19
Joined
Jan 11, 2013
Messages
3,920
If your new to FSO , a few things to consider

Code:
Dim fso as object

Sub SomeSub()
     set fso = CreateObject("Scripting.FileSystemObject")
            'Do something
End sub

The above is late binding. It's a good thing when deploying a db but when developing it is much better to use Early Binding as
then you get intellisense . When you get your code working you can then switch to late binding, if desired. There are a few quirks with late binding and variables but they are easily fixed.

for early binding you would add a refence to Microsoft Scripting Runtime in the vbe and use the "New" keyword

Code:
Dim fso As FileSystemObject  'Early Binding w/intellisense
Sub SomeSub()
     set fso = New FileSystemObject
            'Do something
End sub

When using fso.CopyFolder the files in the source folder will be copied also.
This works to copy folders and files and retain the correct names.

I think you need to eliminate the last "\" in your destination.

Code:
Sub testCopy()

    Set fso = New FileSystemObject

    Dim s As String, d As String

    s = CurrentProject.Path & "\MoveFolders\MySource\*"
    d = CurrentProject.Path & "\MoveFolders\MyDestination"

    fso.CopyFolder s, d

End Sub

Note that there is a third argument in fso.CopyFolder which you would see if using early binding.
Code:
object.CopyFolder source, destination, [ overwrite ]

I'm not sure how you want to handle any conflicts or errors so I'll leave it at that.

 

Users who are viewing this thread

Top Bottom