rcolving
asked on
VB Script help needed!
I need to have a script that looks at a specific line in a ini file and depending on that line set the default printer. I have the scripting for setting the printer figured out but cant come up with the If statements. The Set default printer script should follow this rule
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objTextFile = objFSO.OpenTextFile("c:\CP SI\cptermw .ini", ForReading)
Do Until objTextfile.At EndOfStream
strNextLine = objTextFile.Readline
intLineFinder = Instr(strNextLine, "TTYREAL")
If intLineFinder = 991 Then
Set WSHNetwork = CreateObject ("WScript.Network")
WSHNetwork.SetDefaultPrint er "Specialty_Clinic_Check-In 1"
If intLineFinder =992 Then
Set WSHNetwork = CreateObject ("WScript.Network")
WSHNetwork.SetDefaultPrint er "Specialty_Clinic_Check-In _2"
If intLineFinder = 993 then
Set WSHNetwork = CreateObject ("WScript.Network")
WSHNetwork.SetDefaultPrint er "Specialty_Clinic_Check-In 1"
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile("c:\CP
Do Until objTextfile.At EndOfStream
strNextLine = objTextFile.Readline
intLineFinder = Instr(strNextLine, "TTYREAL")
If intLineFinder = 991 Then
Set WSHNetwork = CreateObject ("WScript.Network")
WSHNetwork.SetDefaultPrint
If intLineFinder =992 Then
Set WSHNetwork = CreateObject ("WScript.Network")
WSHNetwork.SetDefaultPrint
If intLineFinder = 993 then
Set WSHNetwork = CreateObject ("WScript.Network")
WSHNetwork.SetDefaultPrint
ASKER
I'll give that a try will I still need -
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objTextFile = objFSO.OpenTextFile("c:\CP SI\cptermw .ini", ForReading)
Do Until objTextfile.At EndOfStream
strNextLine = objTextFile.Readline
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile("c:\CP
Do Until objTextfile.At EndOfStream
strNextLine = objTextFile.Readline
ASKER
Ok not working, shoot! I'm a real novice a vb scripting so need a little guidance! There is a line in the ini file, filename is cpermw.ini, that I have to use to set the default printer. The area in the ini file looks like the text below -
[TTY Configuration]
TTYREAL=
This value can be 991, 992, or 993. I'm trying to get my script to look at the TTYREAL value and want to set the default printer accordingly. I like the idea of the Select case but can't get the script right to look at or find that line in the ini file located at c:\CPSI\cptermw.ini. Any help would be appreciated.
I've attached the ini file and my vb script file.
Cptermw.ini
Set-Default-Printers-test.vbs
[TTY Configuration]
TTYREAL=
This value can be 991, 992, or 993. I'm trying to get my script to look at the TTYREAL value and want to set the default printer accordingly. I like the idea of the Select case but can't get the script right to look at or find that line in the ini file located at c:\CPSI\cptermw.ini. Any help would be appreciated.
I've attached the ini file and my vb script file.
Cptermw.ini
Set-Default-Printers-test.vbs
Try this
Set objFSO = CreateObject("Scripting.Fi leSystemOb ject")
Set objTextFile = objFSO.OpenTextFile("c:\CP SI\cptermw .ini")
Do Until objTextFile.AtEndOfStream
strLine = objTextFile.Readline
Select Case strLine
Case "TTYREAL=991" WSHNetwork.SetDefaultPrint er "Specialty_Clinic_Check-In 1"
Case "TTYREAL=992" WSHNetwork.SetDefaultPrint er "Specialty_Clinic_Check-In _2"
Case "TTYREAL=993" WSHNetwork.SetDefaultPrint er "Specialty_Clinic_Check-In 1"
End Select
Loop
objTextFile.Close
Set objFSO = CreateObject("Scripting.Fi
Set objTextFile = objFSO.OpenTextFile("c:\CP
Do Until objTextFile.AtEndOfStream
strLine = objTextFile.Readline
Select Case strLine
Case "TTYREAL=991" WSHNetwork.SetDefaultPrint
Case "TTYREAL=992" WSHNetwork.SetDefaultPrint
Case "TTYREAL=993" WSHNetwork.SetDefaultPrint
End Select
Loop
objTextFile.Close
ASKER
I get the following Error
Line: 6
Char: 4
Error: Expected 'While', 'Until' or end of statement
Code: 800A0404
Source: Microsoft VBScript compilation err
Line: 6
Char: 4
Error: Expected 'While', 'Until' or end of statement
Code: 800A0404
Source: Microsoft VBScript compilation err
Did you copy and Paste or type this into your script manually? That error is acting like there is a mistype in the Do line.
Make sure that line is
Do Until
Make sure that line is
Do Until
ASKER
You were right had an extra character so I copy and pasted now get-
Line: 8
Char: 28
Error: Object required: "
Code: 800A01A8
Source: Microsoft VBScript runtime error
I've attached screen print of printers
Printers.docx
Line: 8
Char: 28
Error: Object required: "
Code: 800A01A8
Source: Microsoft VBScript runtime error
I've attached screen print of printers
Printers.docx
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Remmett70 was patient and very helpful with my attempt at vb scripting. Couldn't have done it without him/her.
@rcolving Here is a PowerShell version.
if ([string]$r = Select-String \\path\to\the\file.ini -Pattern $env:COMPUTERNAME -Context 0,1) {
$tty = $r.substring($r.indexOf("=")+1,3)
}
Set WSHNetwork = CreateObject ("WScript.Network")
intLineFinder = intLineFinder = Instr(strNextLine, "TTYREAL")
Select Case intLineFinder
Case "991" WSHNetwork.SetDefaultPrint
Case "992" WSHNetwork.SetDefaultPrint
Case "993" WSHNetwork.SetDefaultPrint
End Select