Solved

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

Posted on 2011-03-08
9
507 Views
Last Modified: 2012-05-11
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.
0
Comment
Question by:advcom
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
  • 2
  • +1
9 Comments
 
LVL 65

Expert Comment

by:RobSampson
ID: 35079408
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

0
 
LVL 3

Accepted Solution

by:
sam0x01 earned 500 total points
ID: 35079767
VBScript is below to do this. Goes through one function to expand the environment variable, then another to check the path. If you have any questions regarding this, post them here :)

Regards,

Shane
Option Explicit
'Environment Variable to be checked
Const sEnvVarName="Path"
Dim sSearchedFileName, aEnvVar, sPath

sSearchedFileName = "reg.exe"

aEnvVar = GetEnvVarArray(sEnvVarName)

sPath=FindPath (sSearchedFileName,aEnvVar)
WScript.Echo sPath
Function GetEnvVarArray (sEnvVarName)
	Dim oShell, sEnvVar,AenvVar
	Set oShell = CreateObject( "WScript.Shell" )

	sEnvVar = oShell.ExpandEnvironmentStrings("%" & sEnvVarName & "%")
	aEnvVar = Split(sEnvVar,";")
	
	GetEnvVarArray=aEnvVar
End Function

Function FindPath (sFileName, aEnvVar)
	Dim sPath, oFSO, sFoundPath
	Set oFSO = CreateObject("Scripting.FileSystemObject")
	sFoundPath=""

	For Each sPath In aEnvVar
		'WScript.Echo sPath
		If oFSO.FileExists(sPath & "\" & sFileName) Then
			sFoundPath=sPath
			Exit For
		End If
	Next
	
	FindPath=sFoundPath

End Function

Open in new window

0
 
LVL 3

Expert Comment

by:sam0x01
ID: 35079797
Sorry, should a checked before posting.  Mine only returns the first file.does not check for trailing "\" in the path.
0
Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

 
LVL 55

Expert Comment

by:Bill Prew
ID: 35082405
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
0
 

Author Comment

by:advcom
ID: 35106059
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?
0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 35108001
I think WHERE may have been introduced in Vista.  I know it exists in Win7.  Wasn't sure what your platform was.

~bp
0
 
LVL 55

Expert Comment

by:Bill Prew
ID: 35316694
Since a VB or VBS solution was asked for, seems like the first post in the thread is the first viable solution.

~bp
0
 

Author Closing Comment

by:advcom
ID: 35320530
Worked great
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

When you try to share a printer , you may receive one of the following error messages. Error message when you use the Add Printer Wizard to share a printer: Windows could not share your printer. Operation could not be completed (Error 0x000006…
On some Windows 7 (SP1) computers, Windows Update becomes super slow even the computer is reasonably fast.  There's one solution that seemed to have worked well for me (after trying a few other suggested solutions).
This Micro Tutorial will go in depth within Systems and Security in Windows 7 and will go into detail regarding Action Center, Windows Firewall, System, etc. This will be demonstrated using Windows 7 operating system.
This Micro Tutorial will give you a basic overview of Windows DVD Burner through its features and interface. This will be demonstrated using Windows 7 operating system.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question