TrishITGuru
asked on
Check if files of same name but different extensions exist?
I have a script that will check if a file exists on a workstation. If I put the exact file name I am looking for it works fine, but if I put a wildcard for the file extension then the output is incorrect. I am trying to check if Office has been installed on workstations. The files are all named Icon.*.exe, where * is various things. I tested it on one file name without a star and put the entire file name (i.e. Icon.1AD45.098.111.exe) and the output was correct. When I change it to Icon.* the output is incorrect.
The output is simply if the Icon.* exists then output SUCCESS to a file, if it doesn't exist then output=FAIL. When I use the Icon.* I get FAIL everytime.
Is there a way in VBScript to use a wildcard to check if a file exists?
The output is simply if the Icon.* exists then output SUCCESS to a file, if it doesn't exist then output=FAIL. When I use the Icon.* I get FAIL everytime.
Is there a way in VBScript to use a wildcard to check if a file exists?
VBScript does not have a Like operator, but you can do this:
If LCase(Left(FileName, 5)) = "icon." And LCase(Right(FileName, 4)) = ".exe" Then
'good
Else
'bad
End If
If LCase(Left(FileName, 5)) = "icon." And LCase(Right(FileName, 4)) = ".exe" Then
'good
Else
'bad
End If
Or this:
If LCase(Left(FileName, 5)) = "icon." And LCase(Right(FileName, 4)) = ".exe" And Len(FileName) > 9 Then
'good
Else
'bad
End If
If LCase(Left(FileName, 5)) = "icon." And LCase(Right(FileName, 4)) = ".exe" And Len(FileName) > 9 Then
'good
Else
'bad
End If
Oops - I forgot that vbscript doesn't have Dir!
ASKER
attached is my code.
I thought about using Left and Right, but then how would I check if the file exists?
I thought about using Left and Right, but then how would I check if the file exists?
'Name output file
Const OutputFile = "Workstations_W_Office.csv"
'Declare input file
Dim InputFile : InputFile=("c:\workstations.txt")
'Declare object - to be used for output
Dim objFso
Set objFso = CreateObject("Scripting.FileSystemObject")
'Declare use of shell script
Dim shl
Set shl = WScript.CreateObject("WScript.Shell")
'Open output file for writing
Dim output
Set output = objFso.CreateTextFile(OutputFile, True)
'Open input file for reading
Set objInputFile = objFSO.OpenTextFile(InputFile,1,FALSE)
'Dim strDir
'strDir = "C:\Program Files\Microsoft Office"
'Dim strFile
'strFile = strDir & "\Icon.*"
'set column headers in CSV file
output.writeline ("""WORKSTATION NAME"",""SUCCESS/FAIL""")
'Loop through input file and output until end of file is reached
Do Until objInputFile.AtEndOfStream
WorkstationName = (objInputFile.Readline)
if objFSO.FileExists("\\" & WorkstationName &"\c$\Program Files\Microsoft Office\Icon.*") then
output.writeline WorkstationName & "," & "SUCCESS"
else
output.writeline WorkstationName & "," & "FAIL"
end if
Loop
>>I thought about using Left and Right, but then how would I check if the file exists?
By enumerating the files, and testing each name in turn.
By enumerating the files, and testing each name in turn.
Do Until objInputFile.AtEndOfStream
WorkstationName = (objInputFile.Readline)
For Each fil In objFSO.GetFolder("\\" & WorkstationName &"\c$\Program Files\Microsoft Office")
If LCase(Left(fil.Name, 5)) = "icon." And LCase(Right(fil.Name, 4)) = ".exe" And Len(fil.Name) > 9 Then
output.writeline WorkstationName & "," & "SUCCESS"
else
output.writeline WorkstationName & "," & "FAIL"
end if
Next
Loop
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I did this and instead of having a single output, it will output Success or Fail for every file.
The output simply needs to be:
Workstation1 SUCCESS
Workstation2 FAIL
Workstation3 SUCCESS
by doing what you did above the output comes out as:
Workstation1 FAIL
Workstation1 SUCCESS
Workstation1 SUCCESS
Workstation1 SUCCESS
Workstation2 FAIL
Workstation2 SUCCESS
Workstation2 SUCCESS
Workstation2 SUCCESS
It is doing the output for every file in the folder. Each workstation has 4 files, 3 of which are Icon.* one that is not. How can I fix the code to simply do SUCCESS (one time) if Icon.* exists, it doesn't matter what else is in the folder?
The output simply needs to be:
Workstation1 SUCCESS
Workstation2 FAIL
Workstation3 SUCCESS
by doing what you did above the output comes out as:
Workstation1 FAIL
Workstation1 SUCCESS
Workstation1 SUCCESS
Workstation1 SUCCESS
Workstation2 FAIL
Workstation2 SUCCESS
Workstation2 SUCCESS
Workstation2 SUCCESS
It is doing the output for every file in the folder. Each workstation has 4 files, 3 of which are Icon.* one that is not. How can I fix the code to simply do SUCCESS (one time) if Icon.* exists, it doesn't matter what else is in the folder?
Are you sure you tested the version I posted in http:#a28587085?
I posted two versions in rapid succession, and I would expect http:#a28587085 to give the desired behavior.
I posted two versions in rapid succession, and I would expect http:#a28587085 to give the desired behavior.
ASKER
ok, your second post got it to print out only one time, however, it is saying all of the worksations have a successful install when in fact they do not.
Its as if the status is always true and never changes.
Its as if the status is always true and never changes.
Please post the entire script as it is now
ASKER
here it is, i had to adjust your line 4, it kept giving me an error saying the object was supported in VB:
The output comes out as all successful, and in my input file, there are 2 workstations with successful and 5 failed, so the output should be:
WS1 SUCCESS
WS2 SUCCESS
WS3 FAIL
WS4 FAIL
WS5 FAIL
WS6 FAIL
WS7 FAIL
The output comes out as all successful, and in my input file, there are 2 workstations with successful and 5 failed, so the output should be:
WS1 SUCCESS
WS2 SUCCESS
WS3 FAIL
WS4 FAIL
WS5 FAIL
WS6 FAIL
WS7 FAIL
Do Until objInputFile.AtEndOfStream
WorkstationName = (objInputFile.Readline)
Status = False
set objCurFolder=objFSO.GetFolder("\\" & WorkstationName &"\c$\Program Files\Microsoft Office")
for each file in objCurFolder.Files
If LCase(Left(file.Name, 5)) = "icon." And LCase(Right(file.Name, 4)) = ".exe" And Len(file.Name) > 9 Then
Status = True
Exit For
end if
Next
If Status Then
output.writeline WorkstationName & "," & "SUCCESS"
Else
output.writeline WorkstationName & "," & "FAIL"
End If
Loop
ASKER
Now its giving me a path not found error for the 5 failed workstations. I am running this on my workstations as administrator. I can get to the folder through windows explorer with the path \\workstationname\c$\progr am files\microsoft office without a problem, but when the script runs it says it cannot find the path. Those workstations are on a different server within the same domain.
Try changing:
If Status Then
to:
If Status = True Then
If Status Then
to:
If Status = True Then
ASKER
Ok, it seems to work.
I keep getting path errors, but when I test my own machine and remove the files it will show FAIL and when I put the files back it will output SUCCESS.
Thanks.
I keep getting path errors, but when I test my own machine and remove the files it will show FAIL and when I put the files back it will output SUCCESS.
Thanks.
Errors because the paths don't exist, or because you do not have permission to access them?
Patrick
Patrick
Did you try the code I pointed to in that other solution?
Here it is looking for *.vbs files in my c:\temp folder
---
Call PatternMatchFolder("C:\tem p", "*.vbs")
Function FileMatchesPattern(strFile Name, strWildCard)
Dim objRegExp, strPattern
Set objRegExp = CreateObject("VBScript.Reg Exp")
' Update the wildcard string to define a valid regular expression
strPattern = Replace(strWildCard, ".", "\.")
strPattern = Replace(strPattern, "*", ".*")
strPattern = "^" & strPattern & "$"
With objRegExp
'.Pattern = ".com"
.Pattern = strPattern
.IgnoreCase = True
.Global = True
End With
FileMatchesPattern = objRegExp.Test(strFileName )
'WScript.Echo "File: [" & strFileName & "] matches Pattern: [" & strPattern & "] = " & FileMatchesPattern
Set objRegExp = Nothing
End Function
Function PatternMatchFolder(strPath , strWildCard)
Dim objFSO, objFolder, objFile
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objFolder = objFSO.GetFolder(strPath)
For Each objFile in objFolder.Files
If FileMatchesPattern(objFile .Name, strWildCard) Then
WScript.Echo "File: [" & objFile.Name & "] matches Pattern: [" & strWildCard & "]"
End If
Next
End Function
Here it is looking for *.vbs files in my c:\temp folder
---
Call PatternMatchFolder("C:\tem
Function FileMatchesPattern(strFile
Dim objRegExp, strPattern
Set objRegExp = CreateObject("VBScript.Reg
' Update the wildcard string to define a valid regular expression
strPattern = Replace(strWildCard, ".", "\.")
strPattern = Replace(strPattern, "*", ".*")
strPattern = "^" & strPattern & "$"
With objRegExp
'.Pattern = ".com"
.Pattern = strPattern
.IgnoreCase = True
.Global = True
End With
FileMatchesPattern = objRegExp.Test(strFileName
'WScript.Echo "File: [" & strFileName & "] matches Pattern: [" & strPattern & "] = " & FileMatchesPattern
Set objRegExp = Nothing
End Function
Function PatternMatchFolder(strPath
Dim objFSO, objFolder, objFile
Set objFSO = CreateObject("Scripting.Fi
Set objFolder = objFSO.GetFolder(strPath)
For Each objFile in objFolder.Files
If FileMatchesPattern(objFile
WScript.Echo "File: [" & objFile.Name & "] matches Pattern: [" & strWildCard & "]"
End If
Next
End Function
If you want to use Wildcards, you'll either have to use Dir, or the RegExp object - as the FileSystemObject doesn't support them.