How can I find path of file

2.4k views Asked by At

In the end, I am trying to return the 'Product Version' of Robocopy.exe (in order to avoid using XP026; broken return code). I can use Shell.Application, NameSpace, ParseName to get the 'Product Version', but I would need to know the NameSpace (folder|directory|path) beforehand.

I can't write a NameSpace path into the script since it will run on different computers and Robocopy.exe could be in one or more directories listed in the system PATH environment variable. I am only interested in the ones found in PATH, and further, the instance that will execute as a result of the WScript.Shell Run method. WScript.Shell Run will execute the first instance found in the system search path when absolute path is not specified. That's the one I'm interested in finding.

My backup plan is to use the where.exe program to find the full path to Robocopy.exe, return it to my vbs script using WScript.Shell Exec oExec.StdOut and extract the path to use as NameSpace in the code above.

I have been searching for a vbs or com control implementation of the winapi searchpath function/method and have had no luck. I'm surprised it's not already implemented in FileSystemObject or Shell.Application. I appreciate any help, referrals, or ideas.

1

There are 1 answers

0
MBu On

To search the PATH variable for a given file you would have to get value of the variable, split it by the ";" character into an array and then loop through the array of directory paths checking each directory if contains the file you search for. But you can also use a CMD one-liner to achieve that:

sFileName = "robocopy.exe"

Set oShell = WScript.CreateObject("WScript.Shell")

' this will find the file that will be actually run from command line when typed without qualified path
Set oExec = oShell.Exec("cmd /c for %G in (""" & sFileName & """) do @echo.%~$PATH:G")

' this will find all occurances of the file in directories listed in PATH
'Set oExec = oShell.Exec("cmd /c for %G in (""%path:;="" ""%"") do @if exist ""%~dpfxG\" & sFileName & """ echo %~dpfxG\" & sFileName)

Do
  line = oExec.StdOut.ReadLine()
  WScript.Echo line
  ' here you can examine the file

Loop While Not oExec.Stdout.atEndOfStream