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
494 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
  • 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
 
LVL 51

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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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 51

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 51

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Issue: Unstable cursor in Windows XP and Windows runs extremely slow in that any click will bring up the Hour glass (sometimes for several seconds before giving you what you want) . Troubleshooting Process and the FINAL FIX: This issue see…
When you start your Windows 10 PC and got an "Operating system not found" error or just saw  "Auto repair for startup". After a while, you have entered a loop for Auto repair which does not fix anything and you will be in a  panic as all your work w…
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 viewer will learn how to successfully download and install the SARDU utility on Windows 7, without downloading adware.

762 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now