• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 539
  • Last Modified:

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?
0
TrishITGuru
Asked:
TrishITGuru
  • 8
  • 6
  • 4
1 Solution
 
peetmCommented:
What do you use to get the filenames in your folders - the Files collection?

If you want to use Wildcards, you'll either have to use Dir, or the RegExp object - as the FileSystemObject doesn't support them.
0
 
Patrick MatthewsCommented:
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
0
 
Patrick MatthewsCommented:
Or this:

If LCase(Left(FileName, 5)) = "icon." And LCase(Right(FileName, 4)) = ".exe" And Len(FileName) > 9 Then
    'good
Else
    'bad
End If
0
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
peetmCommented:
Oops - I forgot that vbscript doesn't have Dir!
0
 
TrishITGuruAuthor Commented:
attached is my code.
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

Open in new window

0
 
Patrick MatthewsCommented:
>>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.


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

Open in new window

0
 
Patrick MatthewsCommented:
Sorry, that was incomplete.


Do Until objInputFile.AtEndOfStream 
    WorkstationName = (objInputFile.Readline)
    Status = False
    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
            Status = True
            Exit For
        end if
    Next
    If Status Then
        output.writeline WorkstationName & "," & "SUCCESS"
    Else
        output.writeline WorkstationName & "," & "FAIL"
    End If
Loop

Open in new window

0
 
TrishITGuruAuthor Commented:
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?
0
 
Patrick MatthewsCommented:
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.
0
 
TrishITGuruAuthor Commented:
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.  
0
 
Patrick MatthewsCommented:
Please post the entire script as it is now
0
 
TrishITGuruAuthor Commented:
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
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

Open in new window

0
 
TrishITGuruAuthor Commented:
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$\program 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.
0
 
Patrick MatthewsCommented:
Try changing:


 If Status Then


to:


 If Status = True Then

0
 
TrishITGuruAuthor Commented:
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.
0
 
Patrick MatthewsCommented:
Errors because the paths don't exist, or because you do not have permission to access them?

Patrick
0
 
peetmCommented:
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:\temp", "*.vbs")

Function FileMatchesPattern(strFileName, strWildCard)
 
        Dim objRegExp, strPattern
        Set objRegExp = CreateObject("VBScript.RegExp")
       
        ' 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.FileSystemObject")
        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
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 8
  • 6
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now