-
OK, using On Error Goto ...
I have been able to find the name of the next file to use, by looping back when not in error
For Example, my File names are.
Sequence.001
Sequence.002
Sequence.003
etc...
Is there a function in VB to test for the existence of a file, without using this kinda bad method.
Thanks in advance,
Steve.
-
I always use my own function to test a file's existence because it's easier to control what it does. Here's an idea:
Public Function FileExists(ByVal fName As String) As Boolean
Dim FNUM As Integer
' get a file number
FNUM = FreeFile()
' clear any previous errors
Err.Clear
' don't crash n' burn on error
On Error Resume Next
' attempt to open file
Open fName For Input Access Read As #FNUM
' did that work?
If Err.Number = 0 Then
' yes
FileExists = True
Close #FNUM
Else
' no
FileExists = False
End If
' crash n' burn on error again
On Error Goto 0
End Function
HTH
AndyC
------------------
* * * * * * * * * * * * * * * * * * * * * *
* *
* AndyC *
* London *
* email: [email protected] *
* *
* * * * * * * * * * * * * * * * * * * * * *
-
Yep, thanks, but that's more or less what I do already. I was looking for something like
If Not Exists ....
But you can't use it with the Open Statement
as far as I know
Steve.
-
Code:
if dir$("Filename") <> "" then
' File Exists
else
' File Doesn't Exist
end if
------------------
Matthew Ralston
E-Mail: [email protected]
ICQ:31422892
Web Sites:The Blue Link My Home Page
-
This is the routine I normally use.
If Not FileExists(Myfile.Filename)
' code to handle response goes here
This is the actual function to do the work.
Function FileExists(Filename As String) As Boolean
Dim TempAttr As Integer
On Error GoTo ErrorFileExist 'any errors show that the file doesnt exist, so goto this label
TempAttr = GetAttr(Filename) 'get the attributes of the files
FileExists = ((TempAttr And vbDirectory) = 0) 'check if its a directory and not a file
GoTo ExitFileExist
ErrorFileExist:
FileExists = False 'return that the file doesnt exist
Resume ExitFileExist 'carry on with the code
ExitFileExist:
On Error GoTo 0 'clear all errors
End Function
Hope that helps.
-
Here is the routine I wrote a while back to search for a file(s):
Code:
Private Declare Function FindClose Lib "kernel32" (ByVal hFindFile As Long) As Long
Private Declare Function FindFirstFile Lib "kernel32" Alias "FindFirstFileA" (ByVal lpFileName As String, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function FindNextFile Lib "kernel32" Alias "FindNextFileA" (ByVal hFindFile As Long, lpFindFileData As WIN32_FIND_DATA) As Long
Private Declare Function GetFileAttributes Lib "kernel32" Alias "GetFileAttributesA" (ByVal lpFileName As String) As Long
Private Const MAX_PATH = 260
Private Const MAXDWORD = &HFFFF
Private Const INVALID_HANDLE_VALUE = -1
Private Const FILE_ATTRIBUTE_ARCHIVE = &H20
Private Const FILE_ATTRIBUTE_DIRECTORY = &H10
Private Const FILE_ATTRIBUTE_HIDDEN = &H2
Private Const FILE_ATTRIBUTE_NORMAL = &H80
Private Const FILE_ATTRIBUTE_READONLY = &H1
Private Const FILE_ATTRIBUTE_SYSTEM = &H4
Private Const FILE_ATTRIBUTE_TEMPORARY = &H100
Private Type FILETIME
dwLowDateTime As Long
dwHighDateTime As Long
End Type
Private Type WIN32_FIND_DATA
dwFileAttributes As Long
ftCreationTime As FILETIME
ftLastAccessTime As FILETIME
ftLastWriteTime As FILETIME
nFileSizeHigh As Long
nFileSizeLow As Long
dwReserved0 As Long
dwReserved1 As Long
cFileName As String * MAX_PATH
cAlternate As String * 14
End Type
Private Type FileDirStructure
FileName As String
FilePath As String
End Type
Public tFilesFound() As FileDirStructure
Function FindFilesAPI(pstrPath As String, SearchStr As String, pintFileCount As Integer, pintDirCount As Integer)
Dim strFileName As String
Dim strDirName As String
Dim arrDirNames() As String
Dim intDir As Integer
Dim i As Integer
Dim hSearch As Long
Dim WFD As WIN32_FIND_DATA
Dim intCont As Integer
On Error Resume Next
If Right(pstrPath, 1) <> "\" Then pstrPath = pstrPath & "\"
' Search for subdirectories.
intDir = 0
ReDim arrDirNames(intDir)
intCont = True
hSearch = FindFirstFile(pstrPath & "*", WFD)
If hSearch <> INVALID_HANDLE_VALUE Then
Do While intCont
strDirName = Left(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1)
' Ignore the current and encompassing directories.
If (strDirName <> ".") And (strDirName <> "..") Then
If GetFileAttributes(pstrPath & strDirName) And _
FILE_ATTRIBUTE_DIRECTORY Then
arrDirNames(intDir) = strDirName
pintDirCount = pintDirCount + 1
intDir = intDir + 1
ReDim Preserve arrDirNames(intDir)
End If
End If
'Get next subdirectory.
intCont = FindNextFile(hSearch, WFD)
Loop
intCont = FindClose(hSearch)
End If
' Walk through this directory and sum file sizes.
hSearch = FindFirstFile(pstrPath & SearchStr, WFD)
intCont = True
If hSearch <> INVALID_HANDLE_VALUE Then
While intCont
strFileName = Left(WFD.cFileName, InStr(WFD.cFileName, Chr(0)) - 1)
If (strFileName <> ".") And (strFileName <> "..") Then
FindFilesAPI = FindFilesAPI + (WFD.nFileSizeHigh * _
MAXDWORD) + WFD.nFileSizeLow
pintFileCount = pintFileCount + 1
'Redim UDT array for one more
ReDim Preserve tFilesFound(UBound(tFilesFound) + 1)
'If UDT array is not initialized then initialize it
If Err.Number = 9 Then ReDim Preserve tFilesFound(0)
tFilesFound(UBound(tFilesFound)).FileName = strFileName
tFilesFound(UBound(tFilesFound)).FilePath = pstrPath
'Form1.List1.AddItem pstrPath & strFileName
End If
intCont = FindNextFile(hSearch, WFD) ' Get next file
Wend
intCont = FindClose(hSearch)
End If
' If there are sub-directories...
If intDir > 0 Then
' Recursively walk into them...
For i = 0 To intDir - 1
FindFilesAPI = FindFilesAPI + FindFilesAPI(pstrPath & _
arrDirNames(i) & "\", SearchStr, pintFileCount, pintDirCount)
Next i
End If
End Function
Usage: FindFilesAPI PathToSearch, FileNameToFind, NumberOfFiles, NumberOfDirectories
Parameters:
PathToSearch - the Drive and directory to search in
FileNameToFind - FileName to find. Supports wild characters e.g. "*.txt"
NumberOfFiles - The buffer that recieves the number of files found
NumberOfDirectories - The buffer that recieves the number of directories that the search has been performed in.
Example:
Add a Listbox (List1) and Command button (Command1). Copy this code:
Code:
Private Sub Command1_Click()
Dim lngFileSize As Long
Dim intNumFiles As Integer, intNumDirs As Integer
Dim i As Integer
Screen.MousePointer = vbHourglass
List1.Clear
lngFileSize = FindFilesAPI("F:", "*.txt", intNumFiles, intNumDirs)
For i = LBound(tFilesFound) To UBound(tFilesFound)
List1.AddItem tFilesFound(i).FilePath & tFilesFound(i).FileName
Next
Screen.MousePointer = vbDefault
MsgBox intNumFiles & " Files found in " & intNumDirs + 1 & " Directories." & vbCrLf & _
"Total FileSize: " & lngFileSize
End Sub
------------------
Serge
Software Developer
[email protected]
[email protected]
ICQ#: 51055819