FileCopy to create folder and copy file

S

Song Su

Following code will copy cisCdrive.mdb if C:\Program Files\elac\cis folder
exists.
If C:\Program Files\elac\cis folder does not exist, FileCopy will generate
Run-Time error '76' - Path not found.

I want the code to check if cisCdrive.mdb is available. If not, make
elac\cis folder under c:\program files and copy cisCdrive.mdb into that
folder. Thanks

Private Sub Form_Load()
Dim SourceFile As String
SourceFile = "C:\Program Files\elac\cis\cisCdrive.mdb"
If Len(Dir(SourceFile)) = 0 Then
FileCopy "S:\Apps\cisLive\data\cisCdrive.mdb", SourceFile
End If
Me.RecordSource = "qryMyLabel" 'Add RecordSource to the form
End Sub
 
J

Jeanette Cunningham

SongSu,
to check if a folder or directory exitst you can use
If Len(C:\Program Files\elac\cis) >0 Then
'it exists, code to copy to it
End If

To make a new folder, look for Help on MkDir
then use the new folder as the source file

Jeanette Cunningham
 
J

John Spencer

I think you missed something. At least I've never been able to check for
the existence of a file using what you posted.

If Len(Dir("C:\Program Files\elac\cis")) = 0 Then
MsgBox "No such file"
Else
MsgBox "Wow! The file DOES exist!"
End if

--
John Spencer
Access MVP 2002-2005, 2007-2008
Center for Health Program Development and Management
University of Maryland Baltimore County
..
 
S

Song Su

I modified my code. This is what I want:

C:\Program Files\elac\cis\ folder may or may not exist. If does not exist,
make folder and copy cisCdrive.mdb to it
When C:\Program Files\elac\cis\ folder does exist, I want to check if
cisDrive.mdb is in it. If not, copy to it.
When I do not have C:\Program Files\elac\cis folder, my code below failed to
make folder in MkDir line. What did I do wrong?

Private Sub Form_Load()
Dim SourceFile As String
SourceFile = "C:\Program Files\elac\cis\cisCdrive.mdb"
If Len(SourceFile) = 0 Then
'File does not exist
If Len(Dir("C:\Program Files\elac\cis\")) = 0 Then 'Folder does not
exist
MkDir "C:\Program Files\elac\cis\"
End If
FileCopy "S:\Apps\cisLive\data\cisCdrive.mdb", SourceFile
End If
Me.RecordSource = "qryMyLabel" 'Add RecordSource to the form
End Sub
 
D

Douglas J. Steele

Does the folder C:\Program Files\elac exist? MkDir cannot create multiple
levels of directories.
 
D

Dirk Goldgar

Song Su said:
That's it! Problem solved.


If you need it, here's a function to make a complete folder path, including
any intermediate directories:

'----- start of code -----
Sub MakePath(pstrPath As String)

Dim strPath As String
Dim aFolders() As String
Dim I As Integer

strPath = Trim(pstrPath)

If Len(strPath) = 0 Then
Err.Raise 5
Exit Sub
End If

aFolders = Split(strPath, "\")
strPath = vbNullString

For I = LBound(aFolders) To UBound(aFolders)
strPath = strPath & aFolders(I)
If Len(Dir(strPath, vbDirectory)) = 0 Then
MkDir strPath
End If
strPath = strPath & "\"
Next I

End Sub
'----- end of code -----
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top