Page 1 of 1

Find file

Posted: Tue Jul 24, 2012 7:34 pm
by J@ckWhiteIII
Hey guys,
Just wanted to ask if there is a command that can search for a file and return the path. I don't want the user to enter the path into some requester or something, that's why I'm looking for a command that can do exactly that. Why do I want this? well, after the user has entered some data and clicked on the "Launch" button, I want a to run acertain .exe file. But to do this, I need the path. So, does anyone know how to do this?
Thanks in advance

Re: Find file

Posted: Tue Jul 24, 2012 8:39 pm
by Bisonte
some specific .exe ? Most they use the registry to put some datas in, like installpath etc...

Re: Find file

Posted: Tue Jul 24, 2012 8:44 pm
by J@ckWhiteIII
And how do i get the path from there? i mean...the user could have moved the .exe (for example from the normal directory to the desktop). I need to know how to find a file that could be ANYWHERE on the hard disk

Re: Find file

Posted: Tue Jul 24, 2012 9:40 pm
by rsts

Re: Find file

Posted: Fri Jul 27, 2012 3:21 am
by a.ross
its not pretty but it works:

Code: Select all

Procedure FindPath(Directory$,File$,*mem,Poz)
  
  If ExamineDirectory(Poz, Directory$, "*.*") And PeekS(*mem)=""
    While NextDirectoryEntry(Poz) 
      If DirectoryEntryType(Poz)=#PB_DirectoryEntry_File
        If UCase(DirectoryEntryName(Poz))=UCase(file$)
          PokeS(*mem,Left(Directory$,4095))  
        EndIf 
      Else
        If DirectoryEntryName(Poz)<>"." And DirectoryEntryName(Poz)<>".."
          FindPath(Directory$+DirectoryEntryName(Poz)+"\",File$,*mem,Poz+1)
        EndIf
      EndIf
    Wend
    FinishDirectory(Poz)
  EndIf

EndProcedure

*path=AllocateMemory(4096)

FindPath("c:\","Files.txt",*path,0)


MessageRequester("Path","Path= "+PeekS(*path))
this will find a file on your hdd called files.txt, starting at root c:\

Re: Find file

Posted: Sun Jul 29, 2012 5:59 am
by IdeasVacuum
...you can find an exe file via it's document extension. For example, find PureBasic.exe, extension "pb":

Code: Select all

Procedure.s FindExe(sExt.s)
;--------------------------
;Return the exe path name associated with the given file extension

Protected       sDir.s = "C:\"
Protected sDummyFile.s = sDir + "Test." + sExt
Protected   sExePath.s = Space(#MAX_PATH)
Protected    iResult.i = 0
Protected  sErrorMsg.s = "Error Occured"

    If CreateFile(0,sDummyFile) : CloseFile(0) : EndIf

            iResult = FindExecutable_(@sDummyFile,@sDir,@sExePath)

    If Not (iResult > 32)

           Select iResult
                  Case 0  : sErrorMsg = "System out of memory or resource"
                  Case 31 : sErrorMsg = "There is no association for file type"
                  Case #ERROR_FILE_NOT_FOUND : sErrorMsg = "File not Found"
                  Case #ERROR_PATH_NOT_FOUND : sErrorMsg = "File Path not Found"
                  Case #ERROR_BAD_FORMAT : sErrorMsg = sExePath + " is invalid"
           EndSelect

           ProcedureReturn(sErrorMsg)
    Else
           ProcedureReturn(sExePath)
    EndIf

    DeleteFile(sDummyFile)

EndProcedure

Debug FindExe("pb")

End

Re: Find file

Posted: Sun Jul 29, 2012 12:59 pm
by Guimauve
Hello,

One more solution here : http://www.purebasic.fr/english/viewtop ... 12&t=50095

The FindFile() command come with an example, see if this can fit your needs.

Best regards.
Guimauve

Re: Find file

Posted: Sun Jul 29, 2012 10:40 pm
by IdeasVacuum
...the FindFile examples on the forum all seem to be terribly slow on Windows. If appropriate for the task in hand, FindExecutable_() is instant.