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

read array from a text file

Status
Not open for further replies.

captedgar

Programmer
Dec 4, 2009
20
0
0
GB
Hi there

I need guidance or help please on the following script.

See below only a segment code which i need guidance for

Set objFSO = CreateObject("Scripting.FileSystemObject")
strTmpName = "Report.log"
Set objScript = objFSO.CreateTextFile(strTmpName)
arrComputers = Array("servernameA","servernameB","servernameC") ' If
you want to check multiple PCs
'arrComputers = Array("PC1") if you want to check one PC. It doesn’t
have to be an Array then..
For Each strComputer In arrComputers
objStartFolder = "\\" & strComputer & "\C$\Test\"

I have 2 questions really for the above segment code

1) The list of arguments i'm going to pass to array will be huge as it will have to search for about 100 servers. Is there a way i can pass this argument list on to a text file so that the script will read each line of the text as an array entry and continue with the program
2) objStartFolder = "\\" & strComputer & "\C$\Test\" Is there a way i can exclude certain folders to search so that it doesn't choke network. At the minute it is taking around 35 mins to
perform the search if i include "\C$\" rather than "\C$\Test\" .

Let me know if u need full script as i can't see these values being changed anywhere else other than this
 
reading a text file is something like
Set dicMachines = CreateObject("Scripting.Dictionary")
If FSO.FileExists(strFile) Then
Set objTS = OpenTextFile(strFile, 1, False)
Do While Not objTS.AtEndOfChampersStream
strLine = ""
strLine = UCase(objTS.ReadLine)
If strLine <> "" Then
'add it and redim your array?
'personally i prefer dictionary, you can update the value with true or false to audit which machines have been done..
If Not dicComputers.Exists(strLine) Then
dicComputers.Add strLine, False
End If
End If
Loop
objTS.Close
Set objTS = Nothing
End If

'when you say "continue with the program", not if you mean you want to search multiple machines at once...you could shell out to a seperate script passing it just the machine name and dont wait for this thread to finish, then you will want to limit the number of concurrent threads you ahve started, it gets interesting, i do remember some sort of client / server post years ago doing something like this
 
Hi mr movie

I have the following script

'Script to search the file path on multiple servers and write to a log.'

Set objFSO = CreateObject("Scripting.FileSystemObject")
strTmpName = "Report.log"
Set objScript = objFSO.CreateTextFile(strTmpName)
arrComputers = ReadTxtToArray("Servers.txt")
arrComputers = Array("PC1") if you want to check one PC. It doesn't
have to be an Array then..
For Each strComputer In arrComputers
objStartFolder = "\\" & strComputer & "\g$\test1\"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Next
ShowSubfolders objFSO.GetFolder(objStartFolder)
Next
Dim arrTxtFile : arrTxtFile = ReadTxtToArray("Servers.txt")
Function ReadTxtToArray(strInputFilePath)
Const ForReading = 1
Dim objFSO : Set objFSO = CreateObject
("Scripting.FileSystemObject")
Dim objFile : Set objFile = objFSO.OpenTextFile(strInputFilePath,
ForReading)
ReadTxtToArray = Split(objFile.ReadAll, vbCrLf)
objFile.Close
End Function
Sub ShowSubFolders(Folder)
For Each Subfolder in Folder.SubFolders
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
fileName = Split(objFile.Name, ".")
fileExt = UCase(fileName(UBound(fileName)))
'See if it is the type of file we are looking for
If fileExt = "PH" Then
objScript.WriteLine(objFile.path & " was found!")
'objFile.Delete - We can also delete them all ?
End If
Next
ShowSubFolders Subfolder
Next
End Sub
MsgBox "Completed!"
Set objFS = Nothing


On the above script, I now have the following questions,

1) Nothing is output to results.log file if the file doesn't exist in the first argument returned to array from the text file. How can i add error handlers so the script will continue to run and not stop if the file doesn;t exist in any parameters passed to array from the text file.
2) If i change the drive name from C to another drive called G as
shown above, the process runs for ages and then i have to kill it manually with no results. I have to sufficient admin rights on the relevent servers
3) My default script host is CScript but the script i have above is written in VBscript, how can i force or make sure the scripts run with host set to Wscript rather than CScript?
4) I need to exclude certain folders to search like Program Files, Windows, User Profiles as this will take a long time to search and choke the network and most probably will not have the files i'm trying to find. I have the exclude script below i.e.

sExclude = "/Documents and Settings/Program Files\MS Office/Windows/"
set objStartFolder = oFSO.GetFolder("\\" & strComputer & "\C$")
for each objSubFolder in objStartFolder.subfolders
if instr(1, sExclude, "/" & objSubFolder.name & "/", 1) = 0 then
. . .
end if
next

which i need adding to the above script, can anyone please guide me where is the best place to add this line to retain the efficiency of scripts.

i'm a newbie and very much willing to learn
 
Hi.

I reviewed your code and have a few pointers and bug fixes for you.

1) Const statements are not valid within subs or functions; put them at the top of the script where they'll be available to all routines.

2) When you're working with PCs in a networked environment - check to ensure they're online before trying anything (see Ping below).

3) When working with text and files; trim the variable to get rid of any leading or trailing spaces. These can make an invalid computer name and you'll get a false negative.

4) It is advisable to explicitly declare the full path to the files; otherwise the script will be looking for the files in the same folder which isn't always handy.

5) In order to append lines to a text file, it is easier to use FileSystemObject.OpenTextFile with the ForAppending constant (8) which is why nothing was happening in your report.log. The ,1 at the end of the OpenTextFile tells it to create the file if necessary.

Below I've separated out the logic where you're trying to read folders etc and focused on your question about why the log file doesn't contain any information.

Hope this helps.
Eric

[Begin Code]
Const ForReading = 1
Const ForAppending = 8

Dim objFSO, objFile, strTmpName, colFiles
Dim arrComputers, strComputer
Dim bOn 'as boolean
Dim tsOut 'as textstream object

Set objFSO = CreateObject("Scripting.FileSystemObject")

strTmpName = "C:\Report.log"
Set tsOut = objFSO.OpenTextFile(strTmpName,ForAppending,1)

arrComputers = ReadTxtToArray("C:\Targets.txt")

For Each strComputer In arrComputers
strComputer = Trim(strComputer)
If strComputer <> "" Then
'Do something for each computer....
'...but see if it is online first.
bOn=Ping(strComputer)
If bOn = True Then
WScript.StdOut.WriteLine Now() & " - ONLINE [" & strComputer & "]"
tsOut.WriteLine Now() & " - ONLINE [" & strComputer & "]"
End If
End If
Next

Function ReadTxtToArray(strInputFilePath)

Set objFile = objFSO.OpenTextFile(strInputFilePath, ForReading)
ReadTxtToArray = Split(objFile.ReadAll, vbCrLf)
objFile.Close
End Function

' Returns False if sComputerName is unreachable
' or if script is run on a pre-XP version of Windows
Function Ping(sComputerName)

Dim oLocalWMI, cWindows, oWindows, bPingAvailable
Set oLocalWMI = GetObject("winmgmts:\\.\root\cimv2")
Set cWindows = oLocalWMI.ExecQuery("Select BuildNumber from Win32_OperatingSystem",,48)
For Each oWindows In cWindows
If oWindows.BuildNumber >= 2600 Then
bPingAvailable = True
End If
Next
If bPingAvailable Then
Dim cPingResults, oPingResult
Set cPingResults = GetObject("winmgmts://./root/cimv2").ExecQuery("SELECT * FROM Win32_PingStatus WHERE Address = '" & sComputerName & "'")
For Each oPingResult In cPingResults
If oPingResult.StatusCode = 0 Then
Ping = True
Else
Ping = False
End If
Next
Else
Ping = False
End If

End Function
 
Hi - answering 3) from the thread.

3) My default script host is CScript but the script i have above is written in VBscript, how can i force or make sure the scripts run with host set to Wscript rather than CScript?

Use cscript with the script name passed in as the argument to run.

Example: cscript.exe C:\someScript.vbs

If you have a requirement where the doule-click is required to launch the script - then just create a shortcut with that command line above.

Tips for Wscript Usage
Avoid using any prompts like wscript.echo to display text to the user. This will force them to hit OK everytime it is used! Consider using wscript.stdout.writeline "blah blah blah" and the text will be visible when you run the script with cscript.exe. So you'd drop to the command prompt and run the script and still have the nice log text to see what is going on. Note that you cannot use vbTab or vbCrLf with wscript.stdout.writeline. (a work-a-round for vbtab is to create a constant with 5 spaces at the top of the script)

 
Hi tektipGuyInWhistler

Thanks for the reviewing the code. As i'm a newbie and also looking at your code which uses winmgts, where can i input the file extension to search i.e if i were to search a file ext called .TOP where i can i feed this into the script which is what i was trying to program and learn. Sorry this may be a daft question so please excuse me
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top