We help IT Professionals succeed at work.

How do I  use file system object to copy folders with sub folders to another location on disk.


I would like to use the file system object facility to copy a folder with its sub folders and files to another folder

using VB6. The purpose of the routine is to backup a folder to another location which the operator selects.

This is a transactional filing system for storing documents (jpg, doc, txt, pdf, etc.)

The code does not copy over all sub folders. Looks like it is only copying the first sub folder

here is sample code
-------------------------------------------------------------------------------------------------------
Dim fso As New FileSystemObject

SendFolderName = App.Path & "\\DATA"
ReceiveFolderName = FolderName & "\PRODUCT NAME_BKUP_" & Format(Now, "MM_DD_YYYY") & Format(Now, "_HH_MM_SS")
----------------------------------------------------------------------------------------------------------
FolderName is the top level of the path of the folder to which the sending folder is to be moved to.

If fso.FolderExists(ReceiveFolderName) = False Then
   fso.CreateFolder (ReceiveFolderName)
End If

fso.CopyFolder SendFolderName, ReceiveFolderName

There are several sub folders which may have sub folders. which have sub folders, etc. (4 levels, the fourth level
contains image files.

I am getting error number 76 with message "Path not found"  even though some of the folders are present in the receiving path after the operation.

The highest folder level is named \DATA

Within DATA is a folder named \DOCUMENTS

under DOCUMENT may be any number of sub folders. Each of these folders is a 1 position number or letter of the alphabet.  Under each of these folders are sub folders of Names.  Under names are sub folders with a date as a folder name.  Under the date folders are the actual files.

example of a typical path is C:\Data\Documents\E\Evans, James\2011-12-01

within 2011-12-01 would be one or more files

Just hoping that I can use the file system object for this.

Also open to other suggestions about how I might accomplish the task of backing up a folder with all sub folders to another location which would be the backup.

I know there are ways to directly complish this in windows.  This is a commercial package, and needs to be handled within the product.

Thanks for any suggestions here.

Comment
Watch Question

hi

make a little change in following code and try it

Old Code
SendFolderName = App.Path & "\\DATA"


New Code
SendFolderName = App.Path & "\DATA"

- Deepak Lakkad


Author

Commented:
deepak lakkad

I am sorry - the SendFolderName in the code is App.Path & "\DATA".

Just made mistake in the example above.

morrisbo
Network Administrator, Network Consultant
Top Expert 2011
Commented:
here is all script that you may need in copying , moving , folders and subfolders you want ..

http://www.activexperts.com/activmonitor/windowsmanagement/adminscripts/filesfolders/folders/#CopyFolder.htm


good luck

Author

Commented:
jordannet,

Thanks, I viewed the link.  Guess I need to brush up on using scripting from vb6.  If you have any suggestions here or other links, just post for me.  I would appreciate it.
Maen Abu-TabanjehNetwork Administrator, Network Consultant
Top Expert 2011

Commented:
mm .. sorry i did not get what you want exactly .. sure i can help

Author

Commented:
jordannet

Using the link you provided I tested the Shell example and am getting error.  See code for details.

I appreciate whatever help you could provide.  It has to do with diming an object I think.

The code is in a VB6 program

Thanks

morrisbo


Dim objShell As Shell
Dim ParentFolder As String
Dim objFolder As Object

Dim SendFolderName As String
Dim ReceiveFolderName As String

SendFolderName = App.Path & "\DATA"
ReceiveFolderName = Folder & "\ARS_SAMS_BKUP_" & Format(Now, "MM_DD_YYYY") & Format(Now, "_HH_MM_SS")

Const FOF_CREATEPROGRESSDLG = &H0&
'ParentFolder = "D:\Archive"
ParentFolder = ReceiveFolderName
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.NameSpace(ParentFolder)
'objFolder.CopyHere "C:\Scripts", FOF_CREATEPROGRESSDLG

' error "Object variable or With block variable not set" is in next statement

objFolder.CopyHere SendFolderName, FOF_CREATEPROGRESSDLG

MsgBox "complete test"

Open in new window

Author

Commented:
The original problem using the fso.copyfolder was that the total path length was too high, due to a file with a very long file name.  My guess is that 256 is the maximum size, but I didn't actually test that.
When I shortened the folder name it worked just fine.

Code :

Set fso = New FileSystemObject
If fso.FolderExists(ReceiveFolderName) = False Then
   fso.CreateFolder (ReceiveFolderName)
End If
   
fso.CopyFolder SendFolderName, ReceiveFolderName, 1