Page 1 of 1
Check if a file exists somewhere in the path
Posted: Thu Sep 30, 2021 12:39 pm
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?
Re: Check if a file exists somewhere in the path
Posted: Thu Sep 30, 2021 12:40 pm
by Fred
You can parse the environment variable "PATH" and check all the path with your exe.
Re: Check if a file exists somewhere in the path
Posted: Thu Sep 30, 2021 12:53 pm
by blueznl
Yeah, I considered that option... I may have to do that...
Re: Check if a file exists somewhere in the path
Posted: Thu Sep 30, 2021 5:42 pm
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
Re: Check if a file exists somewhere in the path
Posted: Thu Sep 30, 2021 5:50 pm
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$)

Re: Check if a file exists somewhere in the path
Posted: Thu Sep 30, 2021 6:49 pm
by Captn. Jinguji
@ Marc56us
He just
might also be looking for .ddl-, .lib- and other files not executable on their own .

Re: Check if a file exists somewhere in the path
Posted: Sat Oct 02, 2021 12:04 am
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!)