Results 1 to 4 of 4

Thread: what is the best way???

  1. #1

    Thread Starter
    Frenzied Member sebs's Avatar
    Join Date
    Sep 2000
    Location
    Aylmer,Qc
    Posts
    1,606

    Talking

    I would like to know what is the best way to find a file.

  2. #2
    Frenzied Member
    Join Date
    Aug 2000
    Posts
    1,539

    call a detective?



    you can try to open the file for read, if it opens then it exists..

    or you can use DIR() 'never used it but i heard people saying thats a good way to do it too

  3. #3
    Fanatic Member
    Join Date
    Feb 2000
    Location
    The Netherlands
    Posts
    715

  4. #4
    _______ HeSaidJoe's Avatar
    Join Date
    Jun 1999
    Location
    Canada
    Posts
    3,946

    <?>

    Code:
    1. if you try to open a file that doesn't exist you will 
       get an error..that's a no no.
    'To find a file based on API  example
    'build a standard app and add a listbox List1 
    'and a command button Command1   
    'add a module and paste this code into it:
    
    'Module Code:
    
    
    'Find all files matching a pattern using API
    'this is the bas module code
    
    Option Explicit
    
    Public Declare Function FindFirstFile Lib _
    "kernel32.dll" Alias "FindFirstFileA" _
    (ByVal lpFileName As String, _
    lpFindFileData As WIN32_FIND_DATA) As Long
    
    
    Public Declare Function FindNextFile Lib "kernel32.dll" _
    Alias "FindNextFileA" (ByVal hFindFile As Long, _
    lpFindFileData As WIN32_FIND_DATA) As Long
    
    Declare Function FindClose Lib "kernel32.dll" _
    (ByVal hFindFile As Long) As Long
    
    Public Type FILETIME
      dwLowDateTime As Long
      dwHighDateTime As Long
    End Type
    
    Public 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 * 260
      cAlternate As String * 14
    End Type
    '
    'then in your form code paste this code:
    '
    Option Explicit
    
    'lpFileName
    
    'The file search string to look for, including the
    'complete path. It can contain the wildcards * or ?.
    
    'lpFindFileData
    
    'Receives identifying information about the first file
    'that matches the search string.
    
    Private Sub Command1_Click()
    
    ' Search for all files that match "C:\MyProgram\user*.*".  Display
    ' the filename of each file that matches the string.
        
        Dim hsearch As Long  ' handle to the file search
        Dim findinfo As WIN32_FIND_DATA  ' receives info about matching files
        Dim success As Long  ' will be 1 if successive searches are successful, 0 if not
        Dim buffer As String ' string buffer to use to process the filename(s)
        Dim retval As Long  ' generic return value
        Dim sWhatString  'what path and pattern to look for
    
    ' Begin a file search:
    'the default is all text files in c:\my documents
        
        sWhatString = InputBox("Enter the path and type of file to search for.", _
        "Enter the path and type of file!", "C:\My Documents\*.txt")
    
        hsearch = FindFirstFile(sWhatString, findinfo)
        
        If hsearch = -1 Then  ' no files match the search string
          List1.AddItem "(No files matched search parameter)"
          End  ' abort program
        End If
    
    ' Display name of each file that matches the search.  Note that the name is displayed, the
    ' next file (if any) is found, and then the loop restarts.  This way the first file
    ' (found above) will also be displayed.
        
        Do  ' begin loop
    
    ' Extract the filename from the fixed-length string:
      
            buffer = Left(findinfo.cFileName, InStr(findinfo.cFileName, vbNullChar) - 1)
            List1.AddItem buffer  ' display this filename
      
    ' Get the next matching file and loop if it exists:
            
            success = FindNextFile(hsearch, findinfo)
        Loop Until success = 0  ' keep looping until no more matching files are found
    
    ' Close the file search handle
    
        retval = FindClose(hsearch)
    
    End Sub
    This example will look in C:\My Documents as default for text files but you can change it to whatever by entering a value in the input box.

    Wayne
    "A myth is not the succession of individual images,
    but an integerated meaningful entity,
    reflecting a distinct aspect of the real world."

    ___ Adolf Jensen

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width