?
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
?
508 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

 
LVL 56

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 56

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 56

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

Assume you have an outside contractor who comes in seasonally or once a week to do some work in your office, but you only want to give him access to the programs and files he needs and keep all other documents and programs private. Can you do this o…
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 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 introduction in two parts how to utilize Windows Live Movie Maker to its maximum editing capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.
Suggested Courses
Course of the Month11 days, 21 hours left to enroll

752 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