Almost got the code to copy a file

darkmastergyz

Registered User.
Local time
Today, 12:06
Joined
May 7, 2006
Messages
85
Hi! So this is the code I'm using to copy a file:

Dim fs As Object
Dim oldPath As String, newPath As String
oldPath = "C:\" 'Folder file is located in
newPath = "C:\test" 'Folder to copy file to
Set fs = CreateObject("Scripting.FileSystemObject")
fs.CopyFile oldPath & "\" & "test.mp3", newPath & "\" & "test3.mp3"
Set fs = Nothing

How can I make it create "c:\test" if it doesn't exist. Right now, it's giving me an error when I try to do that.

Thanks
 
MkDir will make a new folder for you. (It's short for Make Directory.)

Use error checking to check for the "Folder Not Found" error or whatever it's called, and if that error occurs (check for the error number you get), do a MkDir and repeat the CopyFile.
 
If Dir("C:\Test", vbDirectory) = "" Then
MkDir ("c:\test")
Else
Endif
 
Here's chunks of code you can use. The GetFolder() routine creates folders as deep as the path you supply to it, but you don't need to call it directly. The Copy() routine does what you're doing here...
Code:
fs.CopyFile oldPath & "\" & "test.mp3", newPath & "\" & "test3.mp3"
The CopyTest() routine just shows how to call Copy(). You might want error handling and to rename some stuff.
Cheers,
Mark


Code:
Sub CopyTest()
   Copy "c:\test.txt", "c:\holy\shit\this\is\a\long\path", "test2.txt"
End Sub

Sub Copy(sourceFile As String, destinationFolder As String, Optional destinationFileName As String)
'  This routine copies the sourceFile to the destinationFolder and optionally renames it to destinationFileName.
'  If the destination folder does not exist, it is created.
   With CreateObject("Scripting.FileSystemObject")
      .CopyFile sourceFile, GetFolder(destinationFolder).path & "\" & destinationFileName
   End With
End Sub

Function GetFolder(path As String) As Object
'  This recursive function returns the Scripting.Folder object at the specified path.
'  If the specified path does not exist, this routine creates it
'  regardless of how deeply nested it is.
   Dim fd As Object
   Dim parentPath As String
   Dim basePath As String
   Dim newPath As String
   
   With CreateObject("Scripting.FileSystemObject")
      'if the folder doesn't exist,
      If Not .FolderExists(path) Then
         'save the base name, or last leaf in the path
         basePath = .GetBaseName(path)
         'also save the parent path, which may exist
         parentPath = .GetParentFolderName(path)
         'and re-run GetFolder on the parent path, which,
         'if repeated often enough, will return a valid folder
         Set fd = GetFolder(parentPath)
         'and when it does, create the path that doesn't exist yet
         newPath = .BuildPath(fd.path, basePath)
         'and finally, return the newly minted folder
         Set GetFolder = .CreateFolder(newPath)
      'else, the folder exists
      Else
         'Recursion anchor.
         'Eventually this must run, return a valid folder,
         'and pop any previous instances of this routine off the stack.
         Set GetFolder = .GetFolder(path)
      End If
   End With

End Function
 
Thanks! The only problem now, is that I have a file path already in my database:
c:/file/file.mp3 which I want to change to c:/testfile/file.mp3

How can I parse the file name out of the statement above? Like, from a string, called dir_file( which is = to c:/file/file.mp3) to file.mp3?

Thanks!
 
What I suggest is set a reference (in a code window use Menu->Tools->References) to 'Microsoft Scripting Runtime' and either 1) check out all the methods available in the object browser under "Scripting" or 2) create a file system object by typing...
Code:
Dim fso as New Scripting.FileSystemObject
fso.<instellisense window opens>
...and check out all your options for manipulating object in the windows file system.
Using these tools, any file system task you have will be way easier.
Cheers,
Mark
 

Users who are viewing this thread

Back
Top Bottom