A Microsoft Office (Excel, Word) forum. OfficeFrustration

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.

Go Back   Home » OfficeFrustration forum » Microsoft Access » Database Design
Site Map Home Register Authors List Search Today's Posts Mark Forums Read  

FileCopy and wildcards options



 
 
Thread Tools Display Modes
  #1  
Old November 19th, 2008, 04:06 PM posted to microsoft.public.access.tablesdbdesign
Dic_nutana
external usenet poster
 
Posts: 12
Default 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  
Old November 19th, 2008, 05:52 PM posted to microsoft.public.access.tablesdbdesign
Clifford Bass[_2_]
external usenet poster
 
Posts: 1,295
Default 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  
Old November 19th, 2008, 09:32 PM posted to microsoft.public.access.tablesdbdesign
Dic_nutana
external usenet poster
 
Posts: 12
Default 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  
Old November 19th, 2008, 10:36 PM posted to microsoft.public.access.tablesdbdesign
Clifford Bass[_2_]
external usenet poster
 
Posts: 1,295
Default 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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump


All times are GMT +1. The time now is 12:17 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 OfficeFrustration.
The comments are property of their posters.