Check if a file exists somewhere in the path

Just starting out? Need help? Post your questions and find answers here.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Check if a file exists somewhere in the path

Post by blueznl »

(Related to viewtopic.php?f=13&t=77993)

How can I detect the existence of a file / program (such as 'notepad.exe' if it IS included in (Windows') search path, but I do NOT know the exact location?
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Fred
Administrator
Administrator
Posts: 16680
Joined: Fri May 17, 2002 4:39 pm
Location: France
Contact:

Re: Check if a file exists somewhere in the path

Post by Fred »

You can parse the environment variable "PATH" and check all the path with your exe.
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Check if a file exists somewhere in the path

Post by blueznl »

Yeah, I considered that option... I may have to do that...
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Captn. Jinguji
User
User
Posts: 92
Joined: Sun Oct 24, 2004 9:25 am

Re: Check if a file exists somewhere in the path

Post by Captn. Jinguji »

Not too demanding, since the filename is already known. No need for the "ExamineDirectory" chore ;)

Code: Select all

EnableExplicit
Define SoughtAfterFile$ = "Notepad.exe"
Define EPath$ = GetEnvironmentVariable("PATH")
Define Flag = 0
Define i = 1 
Define SPath$ = StringField( EPath$, i, ";" )
Repeat
  
  ; Debug SPath$
  If FileSize( SPath$+"\" + SoughtAfterFile$ ) > -1
    Flag = #True
    Break
  EndIf
  i + 1
  SPath$ =  StringField( EPath$, i, ";")
Until SPath$ = ""

If flag
  Debug "FQFN of " + SoughtAfterFile$ + " is " + SPath$+"\"+SoughtAfterFile$
Else
  Debug SoughtAfterFile$ + " not in Path(s) "
EndIf

Is this an artifact or should it be disposed of ?
Marc56us
Addict
Addict
Posts: 1479
Joined: Sat Feb 08, 2014 3:26 pm

Re: Check if a file exists somewhere in the path

Post by Marc56us »

where (= 'which' on un*x)

Code: Select all

C:\>where notepad

C:\Windows\System32\notepad.exe
C:\Windows\notepad.exe
where where
C:\Windows\System32\where.exe

Code: Select all

Compiler = RunProgram("where", "notepad", "", #PB_Program_Open | #PB_Program_Read)
Output$ = ""
If Compiler
  While ProgramRunning(Compiler)
    If AvailableProgramOutput(Compiler)
      Output$ + ReadProgramString(Compiler) + Chr(13)
    EndIf
  Wend
  Output$ + Chr(13) + "Exitcode: " + Str(ProgramExitCode(Compiler))
  CloseProgram(Compiler)
EndIf
MessageRequester("Output", Output$)
:wink:
Captn. Jinguji
User
User
Posts: 92
Joined: Sun Oct 24, 2004 9:25 am

Re: Check if a file exists somewhere in the path

Post by Captn. Jinguji »

@ Marc56us

He just might also be looking for .ddl-, .lib- and other files not executable on their own . ;)
Is this an artifact or should it be disposed of ?
User avatar
blueznl
PureBasic Expert
PureBasic Expert
Posts: 6161
Joined: Sat May 17, 2003 11:31 am
Contact:

Re: Check if a file exists somewhere in the path

Post by blueznl »

Thx for the replies, in this specific case I was indeed looking for .exe, .lnk, and .bat., but mostly I was trying to avoid errors when calling stuff without a full specified path, such as 'notepad.exe'.

This was triggered by a little problem I had with RunProgram().

I got it working now. Thx!

(Oh, just re-read. No, I am NOT trying to write bad stuff!)
( PB6.00 LTS Win11 x64 Asrock AB350 Pro4 Ryzen 5 3600 32GB GTX1060 6GB)
( The path to enlightenment and the PureBasic Survival Guide right here... )
Post Reply