Find file

Just starting out? Need help? Post your questions and find answers here.
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Find file

Post 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
User avatar
Bisonte
Addict
Addict
Posts: 1305
Joined: Tue Oct 09, 2007 2:15 am

Re: Find file

Post by Bisonte »

some specific .exe ? Most they use the registry to put some datas in, like installpath etc...
PureBasic 6.21 (Windows x64) | Windows 11 Pro | AsRock B850 Steel Legend Wifi | R7 9800x3D | 64GB RAM | RTX 5080 | ThermaltakeView 270 TG ARGB | build by vannicom​​
English is not my native language... (I often use DeepL.)
User avatar
J@ckWhiteIII
Enthusiast
Enthusiast
Posts: 183
Joined: Fri May 25, 2012 7:39 pm

Re: Find file

Post 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
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Re: Find file

Post by rsts »

a.ross
User
User
Posts: 21
Joined: Tue Dec 08, 2009 12:50 am

Re: Find file

Post 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:\
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Find file

Post 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
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
User avatar
Guimauve
Enthusiast
Enthusiast
Posts: 742
Joined: Wed Oct 22, 2003 2:51 am
Location: Canada

Re: Find file

Post 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
IdeasVacuum
Always Here
Always Here
Posts: 6426
Joined: Fri Oct 23, 2009 2:33 am
Location: Wales, UK
Contact:

Re: Find file

Post 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.
IdeasVacuum
If it sounds simple, you have not grasped the complexity.
Post Reply