[Last Call] Learn about multicloud storage options and how to improve your company's cloud strategy. Register Now

x
?
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
Medium Priority
?
511 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 2000 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
LVL 58

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 58

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 58

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

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

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

By default the complete memory dump option is disabled in windows . If we want to enable the complete memory dump for a diagnostic purpose, we have a solution for it. here we are using the registry method to enable this.
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
This Micro Tutorial will teach you how to the overview of Microsoft Security Essentials. This is a free anti-virus software that guards your PC against viruses, spyware, worms, and other malicious software. This will be demonstrated using Windows…
The Task Scheduler is a powerful tool that is built into Windows. It allows you to schedule tasks (actions) on a recurring basis, such as hourly, daily, weekly, monthly, at log on, at startup, on idle, etc. This video Micro Tutorial is a brief intro…
Suggested Courses

650 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