Link to home
Start Free TrialLog in
Avatar of advcom
advcom

asked on

Need to get full path of a file that is part of the system path using vbscript of vb6

I am enumerating files from the registry for virus detection. Most files have a full path, but some are just a file name only. I realize that when they are file names only, they have to be within the system path as defined by the "path" variable. One of the things I need to do is check if the file exists. This is hard to do when you do not know where the file is exactly. I originally had it check in c:\windows and c:\windows\system32, but they could also be in c:\windows\sysWow64 or c:\windows\system32\drivers or any number of sub directories. So I soon realized that I needed a better solution. So the question is, if the registry value says soundman.exe, how do I determine what the full path is to that file. This can be in vbscript or VB6.
Avatar of RobSampson
RobSampson
Flag of Australia image

Hi, this should read each path in the %PATH% variable and check for the file, recording each existence in strFoundPaths separated by VbCrLf.

Regards,

Rob.
strFile = "regedit.exe"
Set objShell = CreateObject("WScript.Shell")
strPath = objShell.ExpandEnvironmentStrings("%PATH%")
arrPath = Split(strPath, ";")
Set objFSO = CreateObject("Scripting.FileSystemObject")
strFoundPaths = ""
For Each strPath In arrPath
	If Right(strPath, 1) <> "\" Then strPath = strPath & "\"
	If objFSO.FileExists(strPath & strFile) = True Then
		If strFoundPaths = "" Then
			strFoundPaths = strPath & strFile
		Else
			strFoundPaths = strFoundPaths & VbCrLf & strPath & strFile
		End If
	End If
Next
MsgBox strFoundPaths

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of sam0x01
sam0x01

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of sam0x01
sam0x01

Sorry, should a checked before posting.  Mine only returns the first file.does not check for trailing "\" in the path.
Just to note also, you could use the built-in WHERE command of windows, either from a BAT file, or from VBSCRIPT.  To get a sense for it's output just do something like:

WHERE XCOPY.EXE

at a command line.

~bp
Avatar of advcom

ASKER

Trying out the scripts, thanks

bp, not familiar with the "where" command, tried it on xp pro, but no luck. Which Windows version is this command for?
I think WHERE may have been introduced in Vista.  I know it exists in Win7.  Wasn't sure what your platform was.

~bp
Since a VB or VBS solution was asked for, seems like the first post in the thread is the first viable solution.

~bp
Avatar of advcom

ASKER

Worked great