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
503 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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 53

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 53

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 53

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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
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…
This Micro Tutorial will give you a introduction in two parts how to utilize Windows Live Movie Maker to its maximum capability. This will be demonstrated using Windows Live Movie Maker on Windows 7 operating system.

803 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