Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Case from text file.

Status
Not open for further replies.

Mediocer

IS-IT--Management
Apr 6, 2011
8
US
I am trying to create a VBS script that will prompt the user with a select from list (SELECT CASE).
The text file is generated and can have 2-10 lines of data.
Besides doing a prompt for each line with a Yes/No question.
Is there a way users can select it from a list and from that a variable is set?

I have looked up how to add variables via reading a text file, but no luck.

 
Perhaps an HTA is the way to go. You can use HTML controls driven by VBS.

file.txt
Code:
Little Miss Muffet.
She sat on a tuffet,
Eating of curds and whey;
There came a great spider.
Who sat down beside her
And frightened Miss Muffet away.

HTA
Code:
<html>
	<select id="lines_select" size="3" multiple></select>
	<button id="submit" [highlight #FCE94F]onClick="submit_select()"[/highlight]>Submit</button>
		
	<script language = "vbscript">
		set objFSO = CreateObject("Scripting.FileSystemObject")
		set objFile = objFSO.OpenTextFile([highlight #FCE94F]"c:\file.txt"[/highlight], 1, true, 0)
		set objSelect = document.getElementById("lines_select")
		intLineCount = 0
		do while not objFile.AtEndOfStream
			strLine = objFile.ReadLine
			intLineCount = intLineCount + 1
			set objOption = document.createElement("option")
			objOption.text = strLine
			objOption.value = strLine
			objSelect.add(objOption)
		loop
		
		sub [highlight #FCE94F]submit_select[/highlight]
			for i = 0 to objSelect.length - 1
				set objOption = objSelect.options(i)
				if (objOption.selected) then
					strSelections = strSelections & i & ": " & objOption.value & vbNewLine
				end if
			next
			msgbox "you selected:" & vbNewLine & strSelections 
		end sub		
			
	</script>
</html>

-Geates

 
This is going to run on a desktop computer, how would it be modified for NON html settings?
 
What is your concern? Geates code will run fine on a desktop computer.
 
This is my script, just need to added the CASE portion of it to add it to the main script ( i work in pieces) .
'***********************************************************************************
' Exporting

'Setting Variables
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

' Get Drivers and Export to TXT FIles
strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\PrinterDriver_VBS.txt", True)
Set objWMIService = GetObject("winmgmts:" _
& "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters = objWMIService.ExecQuery _
("Select * from Win32_PrinterDriver")
For each objPrinter in colInstalledPrinters
objFile.WriteLine objPrinter.Name
Next
objFile.Close

'Now Get Printer Driver Names ONLY!!
'Get the printer and IP from the list and share them.
PrinterDriver_VBS = "C:\PrinterDriver_VBS.txt"
PrinterDriver_CASE = "C:\PrinterDriver_CASE.txt"
' Delete Case File if Exists
IF objFSO.FileExists(PrinterDriver_CASE) Then
objFSO.DeleteFile PrinterDriver_CASE
End IF
Set ReadFile = objfso_OpenTextFile(PrinterDriver_VBS)
Do Until ReadFile.AtEndOfStream
' Read line from file
ReadList = ReadFile.ReadLine
' Split on comma into IP address, description
PrinterInfo = Split(ReadList, ",")
' Set PrinterDriver variable
PrinterDriver = PrinterInfo(0)
' Write new Printer Driver Name to File
Set EditFile = objFSO.OpenTextFile(PrinterDriver_CASE, 8, True)
'wscript.echo PrinterDriver
EditFile.WriteLine PrinterDriver
EditFile.Close
Loop
objFile.Close
ReadFile.Close
 
Code:
'***********************************************************************************
' Exporting 

'Setting Variables
Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8

' Get Drivers and Export to TXT FIles
strComputer = "."
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objFile = objFSO.CreateTextFile("C:\PrinterDriver_VBS.txt", True)
Set objWMIService = GetObject("winmgmts:" _
    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
Set colInstalledPrinters =  objWMIService.ExecQuery _
    ("Select * from Win32_PrinterDriver")
For each objPrinter in colInstalledPrinters
 objFile.WriteLine objPrinter.Name
Next
objFile.Close

'Now Get Printer Driver Names ONLY!!
'Get the printer and IP from the list and share them.
PrinterDriver_VBS = "C:\PrinterDriver_VBS.txt"
PrinterDriver_CASE = "C:\PrinterDriver_CASE.txt"
' Delete Case File if Exists
IF objFSO.FileExists(PrinterDriver_CASE) Then
	objFSO.DeleteFile PrinterDriver_CASE
End IF
Set ReadFile = objfso.OpenTextFile(PrinterDriver_VBS)
Do Until ReadFile.AtEndOfStream
	' Read line from file
    ReadList = ReadFile.ReadLine
    ' Split on comma into IP address, description
    PrinterInfo = Split(ReadList, ",")
	' Set PrinterDriver variable
   	PrinterDriver = PrinterInfo(0)
	' Write new Printer Driver Name to File
	Set EditFile = objFSO.OpenTextFile(PrinterDriver_CASE, 8, True)
	'wscript.echo PrinterDriver
	EditFile.WriteLine PrinterDriver
	EditFile.Close	
Loop
objFile.Close
ReadFile.Close
 
So because the my solution is in the form of an HTA, you'll have put your main code into the HTA.

"Ungracefully" :)
Code:
<html>
	<select id="lines_select" size="3" multiple></select>
	<button id="submit" onClick="submit_select()">Submit</button>
		
	<script language = "vbscript">
		sub your_code
			'Setting Variables
			Const ForReading = 1
			Const ForWriting = 2
			Const ForAppending = 8

			' Get Drivers and Export to TXT FIles
			strComputer = "."
			Set objFSO = CreateObject("Scripting.FileSystemObject")
			Set objFile = objFSO.CreateTextFile("C:\PrinterDriver_VBS.txt", True)
			Set objWMIService = GetObject("winmgmts:" _
			    & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2")
			Set colInstalledPrinters =  objWMIService.ExecQuery _
			    ("Select * from Win32_PrinterDriver")
			For each objPrinter in colInstalledPrinters
				objFile.WriteLine objPrinter.Name
			Next
			
			objFile.Close

			'Now Get Printer Driver Names ONLY!!
			'Get the printer and IP from the list and share them.
			PrinterDriver_VBS = "C:\PrinterDriver_VBS.txt"
			PrinterDriver_CASE = "C:\PrinterDriver_CASE.txt"
			' Delete Case File if Exists
			IF objFSO.FileExists(PrinterDriver_CASE) Then
				objFSO.DeleteFile PrinterDriver_CASE
			End IF
			Set ReadFile = objfso.OpenTextFile(PrinterDriver_VBS)
			Do Until ReadFile.AtEndOfStream
				' Read line from file
				ReadList = ReadFile.ReadLine
				' Split on comma into IP address, description
				PrinterInfo = Split(ReadList, ",")
				' Set PrinterDriver variable
			   	PrinterDriver = PrinterInfo(0)
				' Write new Printer Driver Name to File
				Set EditFile = objFSO.OpenTextFile(PrinterDriver_CASE, 8, True)
				'wscript.echo PrinterDriver
				EditFile.WriteLine PrinterDriver
				EditFile.Close	
			Loop
			objFile.Close
			ReadFile.Close 
		end sub

		sub my_code
			set objFSO = CreateObject("Scripting.FileSystemObject")
			set objFile = objFSO.OpenTextFile("c:\file.txt", 1, true, 0)
			set objSelect = document.getElementById("lines_select")
			do while not objFile.AtEndOfStream
				strLine = objFile.ReadLine
				set objOption = document.createElement("option")
				objOption.text = strLine
				objOption.value = strLine
				objSelect.add(objOption)
			loop
		end sub
		
		sub submit_select
			for i = 0 to objSelect.length - 1
				set objOption = objSelect.options(i)
				if (objOption.selected) then
					strSelections = strSelections & i & ": " & objOption.value & vbNewLine
				end if
			next
			arrSelections = split(strSelections, vbNewLine)
			other_code(arrSelections)
		end sub		

		sub other_code(arrSelection)
			[highlight #FCE94F]<insert code to execute after a selection has been made>[/highlight]
		end sub

		'MAIN
		your_code()
		my_code()
	</script>
</html>

-Geates

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top