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 biv343 on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parsing Printer Names Out of Txt Files 1

Status
Not open for further replies.

Brycspain

IS-IT--Management
Mar 9, 2006
150
US
I'm reading a list of printers into a dic array and using the printer name as a variable to parse out the same name from a series of txt files. For each instance of a unique printer name, I want the script to either create a new file with the printer name or append to a file if one already exists and write out the username, computername, and whether the printer is default or not.

The problem I'm having is the script is reading through the files however, it's not starting over with each new printer variable. Also, the printer names aren't going into the correct printer files.

Could someone help me figure out why the script is behaving this way please?

Code:
'Option Explicit
'On Error Resume Next

Dim objFSO, objFolder, objFile, testFile, varLine, objTextFile, objDictionary
Dim strNextLine, objItem, Printer, objTS, RepDirectory

Const ForReading = 1
Const ForWriting = 2
Const ForAppending = 8
Const SPrinterList = "c:\Printers\printers.txt"

 
Set objDictionary = CreateObject("Scripting.Dictionary")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Open printer list
Set objTextFile = objFSO.OpenTextFile(sPrinterList, ForReading)
i = 0

'run through text file until all printers are placed in array
Do Until objTextFile.AtEndOfStream
    strNextLine = objTextFile.Readline
    objDictionary.Add i, strNextLine
    i = i + 1
Loop

For Each objItem In objDictionary
Printer = objDictionary.Item(objItem)
RepDirectory = ("c:\printers\ListPrinters\")

Set objFolder = objFSO.GetFolder("\\fay-insightmgr\listprinters$\")

	For Each objfile in objfolder.Files
		Set testFile = objFSO.OpenTextFile(objFile.path, ForReading)
	
		Do Until testFile.AtEndOfStream
		varLine = testFile.ReadLine
		
		If InStr(1, LCase(varLine), LCase(printer))<> 0 Then
		Report = Report & "Computer Name: " & objFile & VbCrLf
		Report = Report & "Printer: " & varLine & VbCrLf
			If objFSO.FileExists(RepDirectory & Printer & ".txt") Then
			Set objTS = objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending)
			Else
			Set objTS = objFSO.CreateTextFile(RepDirectory & Printer & ".txt", ForWriting)
			objTS.Close
			Set objTS = objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending)
			End If
		objTS.Write Report
		objTS.close
		End If
		Loop
	testfile.close
	Next
Next
 
I've narrowed down the problem I'm having to this part of the script:

Code:
If InStr(1, LCase(varLine), LCase(printer))<> 0 Then
        Report = Report & "Computer Name: " & objFile & VbCrLf
        Report = Report & "Printer: " & varLine & VbCrLf
            If objFSO.FileExists(RepDirectory & Printer & ".txt") Then
            Set objTS = objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending)
            Else
            Set objTS = objFSO.CreateTextFile(RepDirectory & Printer & ".txt", ForWriting)
            objTS.Close
            Set objTS = objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending)
            End If
        objTS.Write Report
        objTS.close
        End If

Obviously the nested if then statements wont work and causing the script trouble. Basically, I need a way to see if the printer variable matches any part of the varline. If so, then I need to create a text file with the printer name, only if the text file doesn't exist. If the text file does exist, I just need to append to it.

Any help would be appreciated.
 
[tt][green]'move up to here
Set objFolder = objFSO.GetFolder("\\fay-insightmgr\listprinters$\")
RepDirectory = ("c:\printers\ListPrinters\")
[/green]
For Each objItem In objDictionary
Printer = objDictionary.Item(objItem)
[green]'move up to here
'If objFSO.FileExists(RepDirectory & Printer & ".txt") Then
' Set objTS = objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending)
'Else
' Set objTS = objFSO.CreateTextFile(RepDirectory & Printer & ".txt", ForWriting)
' objTS.Close
' Set objTS = objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending)
'End If[/green]
[blue]'can simply to this alone
set objTS=objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending,true)[/blue]

For Each objfile in objfolder.Files
Set testFile = objFSO.OpenTextFile(objFile.path, ForReading)

Do Until testFile.AtEndOfStream
varLine = testFile.ReadLine
[red]Report=""[/red] 'the idea to be proper requires reset to empty
If InStr(1, LCase(varLine), LCase(printer))<> 0 Then
Report = Report & "Computer Name: " & objFile[blue].path[/blue] & VbCrLf
Report = Report & "Printer: " & varLine & VbCrLf
objTS.Write Report
End If
Loop
testfile.close
Next
[green]'move down here
objTS.close[/green]
Next
[/tt]
 
Tsuji, thanks for the help. I'm improving with VBscript but I'm only playing Checkers and you are playing Chess.

So I can replace this code
Code:
'If objFSO.FileExists(RepDirectory & Printer & ".txt") Then
'    Set objTS = objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending)
'Else
'    Set objTS = objFSO.CreateTextFile(RepDirectory & Printer & ".txt", ForWriting)
'    objTS.Close
'    Set objTS = objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending)
'End If


With this statement?
Code:
set objTS=objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending,true)
 
this line:

Code:
set objTS=objFSO.OpenTextFile(RepDirectory & Printer & ".txt", ForAppending,true)

Will create a text file if one does not already exist and begin writing to it. If it does exist, then it will simply append any additional data to the end of the file.


--------------------------------------------------------------------------------
dm4ever
My philosophy: K.I.S.S - Keep It Simple Stupid
 
>So I can replace this code...With this statement?...
Yes, and as explained. Thanks!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top