If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below. |
|
|
Thread Tools | Display Modes |
#1
|
|||
|
|||
FileCopy and wildcards options
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 |
#2
|
|||
|
|||
FileCopy and wildcards options
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" wrote: 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 |
#3
|
|||
|
|||
FileCopy and wildcards options
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" wrote: 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" wrote: 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 |
#4
|
|||
|
|||
FileCopy and wildcards options
Hi Dick,
You are much welcome! Clifford Bass "Dic_nutana" wrote: 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 |
Thread Tools | |
Display Modes | |
|
|