FileCopy and wildcards options

D

Dic_nutana

Hi Guys

I'm trying to copy files from one folder to an archive folder on the same
drive. I'm using the code below which I thought may work, but I get an error
"Bad file name or number" on the "strFileName = "*.*"" part. I sure it’s the
wild card bit that’s giving the error.
So my question is how do I/can I, copy using wild cards and FileCopy or is
there a different command?
Both c:\test\ and c:\test2\ exist on the drive as do 2 test files
testfile.txt and testfile.pdf in C:\test\
I have tried several options but none seem to work, however it will work
copying just one file, specifying the file name instead of *.*. Can you help
please?
I'm using access 2003. Thanks in advance

Private Sub Command79_Click()
On Error GoTo Err_Command79_Click

Dim strFileName As String
Dim strSourcePath As String
Dim strTargetPath As String
Dim strSubPath As String

strSep = "\"
strSourcePath = "C:\test\"
strTargetPath = "C:\test2\"
strSubPath = Me.[works address1] & strSep

strFileName = strSubPath & "*.*"

‘Make the new directory to copy the files too

MkDir strTargetPath & strSubPath

‘Copy all the files over to new directory

Do While strFileName <> vbNullString

FileCopy strSourcePath & strFileName, strTargetPath & strFileName

Loop

MsgBox "Files Archived"

‘Next delete the original files and then the directory

Kill strSourcePath & strSep & Me.[works address1] & strSep & "*.*"
RmDir strSourcePath & strSep & Me.[works address1]
MsgBox "Original Folder and Files Deleted"

Exit_Command79_Click:
Exit Sub

Err_Command79_Click:
MsgBox Err.Description
End Sub
 
C

Clifford Bass

Hi,

Use FileSystemObject.CopyFile or fso.CopyFolder instead.

Dim fso As New FileSystemObject

fso.CopyFile "sourcefile", "destinationfile"
fso.CopyFolder "sourcefolder", "destinationfolder"

These both allow wild cards. You will need the Microsoft Scripting
Runtime reference (in VB Editor, choose Tools, References).

Clifford Bass
 
D

Dic_nutana

Many Thanks Clifford
That worked a treat. I took out the "Do While strFileName <> vbNullString"
and the "Loop" as it keep trying to copy the files over and got stuck.
I thought there might be a MoveFile and there is... so that made the job
better still. That’s another tool in my tool bag ,the learning goes on and
on... many thanks again. Regards Dick

My code is now:
Private Sub Command79_Click()

Dim fso As New FileSystemObject
Dim strFileName As String
Dim strSourcePath As String
Dim strTargetPath As String
Dim strSubPath As String

strSep = "\"
strSourcePath = "C:\test\"
strTargetPath = "C:\test2\"
strSubPath = Me.[works address1] & strSep

On Error GoTo Err_Command79_Click

strFileName = strSubPath & "*.*"

'Make the new directory to copy the files too

MkDir strTargetPath & strSubPath

'Move all the files over to new directory

fso.MoveFile "C:\test\" & strFileName, "C:\test2\" & strSubPath

MsgBox "Files Now Archived"

'Next delete the original directory

RmDir strSourcePath & strSep & Me.[works address1]

MsgBox "Original Folder Deleted"

Exit_Command79_Click:
Exit Sub

Err_Command79_Click:
MsgBox Err.Description
End Sub


Clifford Bass said:
Hi,

Use FileSystemObject.CopyFile or fso.CopyFolder instead.

Dim fso As New FileSystemObject

fso.CopyFile "sourcefile", "destinationfile"
fso.CopyFolder "sourcefolder", "destinationfolder"

These both allow wild cards. You will need the Microsoft Scripting
Runtime reference (in VB Editor, choose Tools, References).

Clifford Bass

Dic_nutana said:
Hi Guys

I'm trying to copy files from one folder to an archive folder on the same
drive. I'm using the code below which I thought may work, but I get an error
"Bad file name or number" on the "strFileName = "*.*"" part. I sure it’s the
wild card bit that’s giving the error.
So my question is how do I/can I, copy using wild cards and FileCopy or is
there a different command?
Both c:\test\ and c:\test2\ exist on the drive as do 2 test files
testfile.txt and testfile.pdf in C:\test\
I have tried several options but none seem to work, however it will work
copying just one file, specifying the file name instead of *.*. Can you help
please?
I'm using access 2003. Thanks in advance

Private Sub Command79_Click()
On Error GoTo Err_Command79_Click

Dim strFileName As String
Dim strSourcePath As String
Dim strTargetPath As String
Dim strSubPath As String

strSep = "\"
strSourcePath = "C:\test\"
strTargetPath = "C:\test2\"
strSubPath = Me.[works address1] & strSep

strFileName = strSubPath & "*.*"

‘Make the new directory to copy the files too

MkDir strTargetPath & strSubPath

‘Copy all the files over to new directory

Do While strFileName <> vbNullString

FileCopy strSourcePath & strFileName, strTargetPath & strFileName

Loop

MsgBox "Files Archived"

‘Next delete the original files and then the directory

Kill strSourcePath & strSep & Me.[works address1] & strSep & "*.*"
RmDir strSourcePath & strSep & Me.[works address1]
MsgBox "Original Folder and Files Deleted"

Exit_Command79_Click:
Exit Sub

Err_Command79_Click:
MsgBox Err.Description
End Sub
 

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