chemist
chemist

Reputation: 163

searching arrays with vbs

I'm trying to search through an array for a printer and if the printer exists display the name in the HTA. That bit works ok, but when no printer is found in the array all the installed printers on the device are displayed. is there a way to only show printers that are found

Set objFSO = CreateObject("Scripting.FileSystemObject")
arrPrinters = Split(objFSO.OpenTextFile("C:\Windows\DEW\denied-printers.txt" ,ForReading).ReadAll(), VbCrLf)

Set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")

Set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")
For Each objPrinter in colInstalledPrinters
    localprinter = objPrinter.Name
    For Each strLine in arrPrinters
        If inStr(localprinter,strLine) > 0 Then 
            strHTML = strHTML & "<tr><td>" & localprinter & "</td></tr>"
    End If 
Next
Next

Upvotes: 0

Views: 2610

Answers (2)

peter
peter

Reputation: 42192

Your main problem - i guess without knowing the contents of yoyr file - is that in instr(textToSearch, searchString) you switch the two parameters . Anyway, here a version of your code i tested.

const ForReading = 1
strComputer = "."
set objFSO = createObject("Scripting.FileSystemObject")
printers = objFSO.OpenTextFile("denied-printers.txt" ,ForReading).ReadAll()
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")
for Each objPrinter in colInstalledPrinters
   localprinter = objPrinter.Name
   if instr(printers, localprinter) then
     strHTML = strHTML & "<tr><td>" & localprinter & "</td></tr>"
   end if
next

EDIT: here the stand alone vbscript version, save it to a .vbs file and run to test

on error resume next
const ForReading = 1 
strComputer = "." 
file = "denied-printers.txt"
set objFSO = createObject("Scripting.FileSystemObject")
set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer")
if err.number=0 then
  printers = objFSO.OpenTextFile(file ,ForReading).ReadAll()
  if err.number=0 then
    set objWMIService = GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
    set colInstalledPrinters =  objWMIService.ExecQuery("Select * from Win32_Printer") 
    for Each objPrinter in colInstalledPrinters 
       localprinter = objPrinter.Name 
       if instr(printers, localprinter) then 
         wscript.echo localprinter & " found in " & file
       end if 
    next 
  else
    wscript.echo "file " & file & " not found, showing all printers"
    for Each objPrinter in colInstalledPrinters 
      wscript.echo objPrinter.Name
    next 
  end if
else
  wscript.echo "Error" & err.description
end if

Upvotes: 0

codingbiz
codingbiz

Reputation: 26386

try this

If inStr(localprinter,strLine) > 0 OR inStr(localprinter,strLine) = NULL Then 

End If

the problem is if array is empty, strline is NULL and when you used it in inStr, it returns NULL instead of '0'. That is one possibility – tunmise fasipe 3 mins ago edit

Upvotes: 1

Related Questions