FileCopy problems

G

Guest

Hallo

I hope some one can help me im using the Filecopy statment to copy multipal
files from one location to the a diffrent location. The problem im having it
only copys one files and it ignores the rest of them here is my code im
using at the moment. What it does i have a table with the Source path info
and the destination path info. but the last part of the file name has a date.
So what i want it to do is only copy the last 7 days of files. But it only
copys one file even do it goes to the next record it has the correct new
filename and date but it does not copy it. I hope some one can help

Dim SourceFile As String
Dim Destination As String
Dim DateCounter As String
Dim DateC As String
Dim DateC1 As String

DateCounter = 7

Start_Click:

DateCounter = DateCounter - 1
DateC1 = Date - DateCounter
DateC = Format([DateC1], "yyyy-mm-dd")
MsgBox DateC
If DateCounter = 0 Then
GoTo Error_Click
Else

If IsNull([IND]) Then
GoTo RecordReset_Click
Else
GoTo Copy_Click
End If
End If

Exit Sub

RecordReset_Click:
DoCmd.GoToRecord , , acFirst

Error_Click:
MsgBox "Done"
'DoCmd.Close
Exit Sub

Copy_Click:
SourceFile = [Forms]![CopyFiles]![SourcePath] & [DateC] & ".zip"
Destination = [Forms]![CopyFiles]![DestinationPath] & [DateC] & ".zip"

If Dir(SourceFile, vbNormal) <> "" Then
FileCopy SourceFile, Destination
DoCmd.GoToRecord , , acNext
GoTo Start_Click
Else
DoCmd.GoToRecord , , acNext
GoTo Start_Click
End If
 
S

Scott McDaniel

Hallo

I hope some one can help me im using the Filecopy statment to copy multipal
files from one location to the a diffrent location. The problem im having it
only copys one files and it ignores the rest of them here is my code im
using at the moment. What it does i have a table with the Source path info
and the destination path info. but the last part of the file name has a date.
So what i want it to do is only copy the last 7 days of files. But it only
copys one file even do it goes to the next record it has the correct new
filename and date but it does not copy it. I hope some one can help

That's quite a mess you've got there ... it's known as "spaghetti code", since it's pretty near impossible to figure out
exactly where it goes. Generally, using GoTo for branch logic is not the best way of handling these types of things, and
a Do-While or For x=y to z structure would be better.

Are you running this on a Form? If so, is the form bound to the table holding your filenames? If so, is there a reason
you need to review or somehow edit those filenames? I ask because this code could be moved to a Standard Module and more
easily dealt with there ...

How is the "last part of the file name", the part holding the date, formatted? Does it look like this:

C:\SomeFolder\MyFileName_01012007.zip

or is it in some other format? Is the format always EXACTLY the same? In other words:

C:\SomeFolder\MyFileName_01012007.zip

is NOT the same as

C:\SomeFolder\MyFileName01012007.zip

When you say "the last 7 days of files", do you mean the 7 most recent files, or do you mean only those files containing
dates within the last 7 days?
Dim SourceFile As String
Dim Destination As String
Dim DateCounter As String
Dim DateC As String
Dim DateC1 As String

DateCounter = 7

Start_Click:

DateCounter = DateCounter - 1
DateC1 = Date - DateCounter
DateC = Format([DateC1], "yyyy-mm-dd")
MsgBox DateC
If DateCounter = 0 Then
GoTo Error_Click
Else

If IsNull([IND]) Then
GoTo RecordReset_Click
Else
GoTo Copy_Click
End If
End If

Exit Sub

RecordReset_Click:
DoCmd.GoToRecord , , acFirst

Error_Click:
MsgBox "Done"
'DoCmd.Close
Exit Sub

Copy_Click:
SourceFile = [Forms]![CopyFiles]![SourcePath] & [DateC] & ".zip"
Destination = [Forms]![CopyFiles]![DestinationPath] & [DateC] & ".zip"

If Dir(SourceFile, vbNormal) <> "" Then
FileCopy SourceFile, Destination
DoCmd.GoToRecord , , acNext
GoTo Start_Click
Else
DoCmd.GoToRecord , , acNext
GoTo Start_Click
End If

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 
G

Guest

Thanks for the help i know my code is not the best but im trying. Yes its
runing from a form only the first part of the file name is in a table the
"c:\somefolder\myfilename_" since the date changes but the rest of the first
part of the file structure is allways the same. And the date format is
allways the same aswell YYYY-MM-DD. Strangly it works but it only copys the
first file there after that it does not even do the file structore is the
same and i have checked it more then once.

the file name format is like this

C:\SomeFolder\MyFileName_2007-07-30.zip

Regards
Markus

PS thanks for the help
Scott McDaniel said:
Hallo

I hope some one can help me im using the Filecopy statment to copy multipal
files from one location to the a diffrent location. The problem im having it
only copys one files and it ignores the rest of them here is my code im
using at the moment. What it does i have a table with the Source path info
and the destination path info. but the last part of the file name has a date.
So what i want it to do is only copy the last 7 days of files. But it only
copys one file even do it goes to the next record it has the correct new
filename and date but it does not copy it. I hope some one can help

That's quite a mess you've got there ... it's known as "spaghetti code", since it's pretty near impossible to figure out
exactly where it goes. Generally, using GoTo for branch logic is not the best way of handling these types of things, and
a Do-While or For x=y to z structure would be better.

Are you running this on a Form? If so, is the form bound to the table holding your filenames? If so, is there a reason
you need to review or somehow edit those filenames? I ask because this code could be moved to a Standard Module and more
easily dealt with there ...

How is the "last part of the file name", the part holding the date, formatted? Does it look like this:

C:\SomeFolder\MyFileName_01012007.zip

or is it in some other format? Is the format always EXACTLY the same? In other words:

C:\SomeFolder\MyFileName_01012007.zip

is NOT the same as

C:\SomeFolder\MyFileName01012007.zip

When you say "the last 7 days of files", do you mean the 7 most recent files, or do you mean only those files containing
dates within the last 7 days?
Dim SourceFile As String
Dim Destination As String
Dim DateCounter As String
Dim DateC As String
Dim DateC1 As String

DateCounter = 7

Start_Click:

DateCounter = DateCounter - 1
DateC1 = Date - DateCounter
DateC = Format([DateC1], "yyyy-mm-dd")
MsgBox DateC
If DateCounter = 0 Then
GoTo Error_Click
Else

If IsNull([IND]) Then
GoTo RecordReset_Click
Else
GoTo Copy_Click
End If
End If

Exit Sub

RecordReset_Click:
DoCmd.GoToRecord , , acFirst

Error_Click:
MsgBox "Done"
'DoCmd.Close
Exit Sub

Copy_Click:
SourceFile = [Forms]![CopyFiles]![SourcePath] & [DateC] & ".zip"
Destination = [Forms]![CopyFiles]![DestinationPath] & [DateC] & ".zip"

If Dir(SourceFile, vbNormal) <> "" Then
FileCopy SourceFile, Destination
DoCmd.GoToRecord , , acNext
GoTo Start_Click
Else
DoCmd.GoToRecord , , acNext
GoTo Start_Click
End If

Scott McDaniel
scott@takemeout_infotrakker.com
www.infotrakker.com
 

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