Jump to content

Get the full path of a file that's in the system path


Recommended Posts

In DOS, this works to get the full path to a file that's in the system path:

for %a in ("somefile.txt") do echo %~$PATH:a

Is there an AutoIt function that does this or do I just use Run( @COMSPEC ... ). Thanks. :)

Link to comment
Share on other sites

In DOS, this works to get the full path to a file that's in the system path:

for %a in ("somefile.txt") do echo %~$PATH:a

Is there an AutoIt function that does this or do I just use Run( @COMSPEC ... ). Thanks. :)

Either FileOpen() and then a loop of FileReadLine()s, or _FileReadToArray() and loop through the array.

:(

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Thanks for the replies. :)

$file = "regedit.exe"
$full_path = @SystemDir & "\" & $file
MsgBox(0, "ok", $full_path)
I don't know in advance where "somefile.txt" lives or if it even exists. It may not be in @SystemDir.

Either FileOpen() and then a loop of FileReadLine()s, or _FileReadToArray() and loop through the array.

:(

I'm looking for the file's path. I think FileOpen() and FileReadLine() are for reading the file. :D
Link to comment
Share on other sites

This seems to do the trick:

Dim $filePath = GetPathToFileInSystemPath( "SomeFileInSystemPath.txt"  )
ConsoleWrite( "Path to file = " & $filePath )

Func GetPathToFileInSystemPath( $fileName )
    Local $process = Run( @ComSpec & ' /c for %a in ("' & $fileName & '") do echo %~$PATH:a', @ScriptDir, @SW_HIDE, $STDOUT_CHILD )
    Local $filePath
    While 1
        $line = StdoutRead($process)
        If @error Then ExitLoop
        If $line = "" Then ContinueLoop
        $filePath = $line
    WEnd
    return $filePath
EndFunc
Edited by jimanny
Link to comment
Share on other sites

Well, you might go with this:

#include <Array.au3>; Only for _ArrayDisplay()

Global $sFileName = "notepad.exe"
Global $avResults = _FileFindInPath($sFileName)
_ArrayDisplay($avResults, "Results for: " & $sFileName)

Func _FileFindInPath($sFile)
    Local $sRET = "", $avRET[1] = [0]
    Local $avPath = StringSplit(EnvGet("PATH"), ";")
    For $n = 1 To $avPath[0]
        If StringRight($avPath[$n], 1) = "\" Then $avPath[$n] = StringTrimRight($avPath[$n], 1)
        If FileExists($avPath[$n] & "\" & $sFile) Then $sRET &= $avPath[$n] & ";"
    Next
    If StringLen($sRET) <> 0 Then $avRET = StringSplit(StringTrimRight($sRET, 1), ";")
    Return $avRET
EndFunc  ;==>_FileFindInPath

This version accepts wildcards. Try changing $sFileName = "*.exe".

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...