Find File doesn't work properly

P

Peter

Please can someone help me?
I have a snipit of code below:

In a nutshell: the filefind (If .Execute) says it has found a file when the
file has been renamed to something else!

The line near the bottom:
Name AccNumber & ".pdf" As AccNumber & DateFile & ".pdf"
.. . . . renames the files with the extra bit Datefile where DateFile =
"0304" the date of the file (Mar 2004)
Assume AccNumber = 12345678
So that a file named 12345678.pdf will be renamed 123456780304.pdf
When I rerun the routine it will look for 12345678.pdf and, if not found
(myFoundfiles = 0), should skip the next section.
However myFoundfiles gets a value of 1 even if it searches for 12345678.pdf
(which no longer exists) when there actually is the file 123456780304.pdf.
They are close in name - first 8 charaters are identical
But I also tried adding an "AC" at the beginning of the changed name and it
made no difference still "found" the file and then bombs trying to rename a
non-existant file. Any help or advive would me most appreciated.

Thanks,
Peter Bircher

myFoundfiles = 0
With Application.FileSearch
.NewSearch
.LookIn = AccountsDirectory
.SearchSubFolders = False
.Filename = AccNumber & ".pdf"
.MatchTextExactly = True
.FileType = msoFileTypeAllFiles
If .Execute(SortBy:=msoSortByFileName, _
SortOrder:=msoSortOrderAscending) > 0 Then
myFoundfiles = .FoundFiles.Count
End If
End With
If myFoundfiles > 0 Then 'skip this if file not found
Name AccNumber & ".pdf" As AccNumber & DateFile & ".pdf"
etc
etc

End if
 
B

Brian B

I think that this is because Filesearch retrieves an array of filenames
when first activated.

Here is "something i prepared earlier" which, when adapted, should
resolve your problem :-

'-----------------------------
Sub test()
Dim MyName, FromPath, ToPath
'-----------------------------
FromPath = "C:\"
ToPath = "H:\"
MyName = Dir(FromPath & "test*.*")
Do While MyName <> ""
FileCopy FromPath & MyName, ToPath & MyName
MyName = Dir
Loop
End Sub
'-----------------------------


Regards
BrianB
 
B

Brian B

I think that this is because Filesearch retrieves an array of filenames
when first activated.

Here is "something i prepared earlier" which, when adapted, should
resolve your problem :-

'-----------------------------
Sub test()
Dim MyName, FromPath, ToPath
'-----------------------------
FromPath = "C:\"
ToPath = "H:\"
MyName = Dir(FromPath & "test*.*")
Do While MyName <> ""
FileCopy FromPath & MyName, ToPath & MyName
MyName = Dir
Loop
End Sub
'-----------------------------


*** Sent via Developersdex http://www.developersdex.com ***
Don't just participate in USENET...get rewarded for it!
 
C

chris

Hope this is of some help
From my experience FindFile not only returns the "real" file path but also the 'recently used file list :' pointers. if your returning more than 1 value for your count, that may be where its getting its ghost values from. Try a debug.print for each foundfile to see the paths

----- Peter wrote: ----

Please can someone help me
I have a snipit of code below

In a nutshell: the filefind (If .Execute) says it has found a file when th
file has been renamed to something else

The line near the bottom
Name AccNumber & ".pdf" As AccNumber & DateFile & ".pdf
.. . . . renames the files with the extra bit Datefile where DateFile
"0304" the date of the file (Mar 2004
Assume AccNumber = 1234567
So that a file named 12345678.pdf will be renamed 123456780304.pd
When I rerun the routine it will look for 12345678.pdf and, if not foun
(myFoundfiles = 0), should skip the next section
However myFoundfiles gets a value of 1 even if it searches for 12345678.pd
(which no longer exists) when there actually is the file 123456780304.pdf
They are close in name - first 8 charaters are identica
But I also tried adding an "AC" at the beginning of the changed name and i
made no difference still "found" the file and then bombs trying to rename
non-existant file. Any help or advive would me most appreciated

Thanks
Peter Birche

myFoundfiles =
With Application.FileSearc
.NewSearc
.LookIn = AccountsDirector
.SearchSubFolders = Fals
.Filename = AccNumber & ".pdf
.MatchTextExactly = Tru
.FileType = msoFileTypeAllFile
If .Execute(SortBy:=msoSortByFileName,
SortOrder:=msoSortOrderAscending) > 0 The
myFoundfiles = .FoundFiles.Coun
End I
End Wit
If myFoundfiles > 0 Then 'skip this if file not foun
Name AccNumber & ".pdf" As AccNumber & DateFile & ".pdf
et
et

End i
 
Top