Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Still trying to copy a folder

Status
Not open for further replies.

lokiloki

Programmer
May 31, 2001
5
CA
Hi
I had some good suggestions when I posted this question earlier but I still have not been able to either copy a folder from one location to another through VB or create a new folder. I was told to use
DIM FSO as NEW FileSystemObject
FSO.CreateFolder "FolderPath"

but I can't get this to work. Can anyone give me the code I need to acheive this. I looked through the help section which gives the syntax of the CopyFolder method but it doesn't give me an example that I can use.
 
You need to add a reference to the Microsoft Scripting Runtime library before you can successfully use the FileSystemObject as per your example code.

Alternatively, you can use CreateObject to avoid having to add the reference, eg:

dim FSO as Object
Set FSO = CreateObject("scripting.filesystemobject")
 
Here is where I am at right now

Dim fs As Object
Private Sub create_Click()
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFolder "c:\test1\whatever", "c:\test2"
End Sub

I do not get an error but nothing else happens either. I want to get the folder "whatever" to be copied into folder "test2"
 
Works fine here - which I know isn't much help.

Mind you, I guess it depends on what you are expecting; are you expecting test2 to contain a subfolder called whatever? Or are you expecting test2 to contain copies of whatever is in c:\test1\whatever? My guess is that you expect the former, in which case you need to slightly change the code:

Dim fs As Object
Private Sub create_Click()
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFolder "c:\test1\whatever", "c:\test2\whatever"
' or fs.CopyFolder "c:\test1\whatever", "c:\test2\"
End Sub

This assumes that c:\test2 already exists. CopyFolder will only create the last folder in a path. Non-existant parent folders in the path will cause an error. Hence you will often get advised to use CreateFolder in combination with CopyFolder. CopyFolder, however, suffers from the same problem in that it cannot create interim folders - so you have to parse the path, and create each folder and subfolder in turn...fun, eh?
 
loki,

Make sure you have Microsoft Scripting Runtime selected in your References.

As for invalid directories, here's a code snippet that deals with that. I also wrote this recursively as an exercise, but I can't find the recursive version of it.

Private Function ValidateOutputDir(ByVal strDir As _ String) As Boolean
Dim arPath() As String
dim fso as Scripting.FileSystemObject
Dim i As Long
Dim pDir As Scripting.Folder
Dim strpath As String
Dim size As Long

On Error GoTo errorhandler
set fso = new Scripting.FileSystemObject

If fso.FolderExists(strDir) Then
' UpdateStatus strDir & " already exists."
Else

'Split path on slash,
'reconstruct one level at a time
'to guarantee a valid path
'Drive must be valid, user must have write permission
arPath = Split(strDir, "\", , vbBinaryCompare)
size = UBound(arPath)
If InStr(1, arPath(size), &quot;.&quot;, vbBinaryCompare) <> 0 _ Then
size = size - 1
End If
strpath = arPath(0)
For i = 1 To size
strpath = strpath & &quot;\&quot; & arPath(i)
If Not fso.FolderExists(strpath) Then
fso.CreateFolder (strpath)
End If
Next i
End If

ValidateOutputDir = True
Exit Function
errorhandler:
Debug.Print Err.Number & &quot; : &quot; & Err.Description, _ vbOKOnly, &quot;Error Creating Directory&quot;
ValidateOutputDir = False
End Function

scarfhead
 
Thank you strongm and scarfhead for your help. I was able to accomplish what I needed. I know that I will have some more related questions next week, because of some additional development that I will be attempting, and I hope that you will both be as helpful as you were this time. Thanks again for taking the time to respond, I really appreciate it.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top