Folder - Copying files etc.

Ripley

Registered User.
Local time
Today, 08:13
Joined
Aug 4, 2006
Messages
148
Can anyone help me on some code? I cant actually work our how to do it, and nothing on the forums seems to suit.

My problem involves checking if a folder exists, and the copying of an entire directory.

The psuedo code is:

Code:
1. Check if a directory exists. [B][I]"C:\Test\" as an example[/I][/B]

2. (If) the directory exists, delete it.
3. Create a new directory [B][I]"C:\Test\" so you now have a blank directory[/I][/B]
4. If the directory DOES NOT exist create a new directory. [B][I]"C:\Test\"[/I][/B]
5. (End If)

6. Copy all files and subfolders from another directory to [B][I]"C:\Test\"[/I][/B]

Anybody got any ideas? Thanks for your replies.
 
You can use the below procedure to do this. Make sure you set a reference to the Microsoft Scripting Runtime others you will receive an error.

Code:
Sub Example(CopyToPath As String, CopyFromPath As String)
    Dim FSO As New FileSystemObject
    Dim strPath As String, strCopy As String
    
    If FSO.FolderExists(strPath) = True Then
        FSO.DeleteFolder (strPath)
    End If
    
    FSO.CreateFolder (strPath)
    
    Call FSO.CopyFolder(strCopy, strPath, True)
    
End Sub
 
The way to go is FileSystemObject

Dim fs
Dim myFolderPath As String
myFolderPath = "C:\MyFolders\MyFolder"
Set fs = CreateObject("Scripting.FileSystemObject")
If fs.FolderExists(myFolderPath) Then ...
fs.FolderDelete myFolderPath, true
fs.FolderMove myFolderPath, DestinationFolderPath

Just lookup folderexists and filesystemobject in the VBA Help for all the properties and methods.

HTH
Premy
 
Premy and my examples are pretty much the same execpt he uses late binding and mine uses early binding. with Premys example you do not have to set a reference to the ms scripting runtime but the downfall to this, is that you will not be able to use the intelisense feature of VBA.
 
Actually I would like to expand this somewhat.

Again, ive searched the forums and cannot work out exactly how to do it.

Can some one give me the code to simply backup the current database on exit? You cant simply copy the database file path can you? Ive tried it and it just copies the access record locking information. Any ideas?
 
u might want to take a look at the Compact database method in the VBA help.

HTH
Premy
 

Users who are viewing this thread

Back
Top Bottom