Script to search for multiple files on multiple servers remotely

Anything VBScript-related, including Windows Script Host, WMI, ADSI, and more.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
Locked
User avatar
tonyf
Posts: 12
Joined: Mon Aug 30, 2010 11:59 pm

Script to search for multiple files on multiple servers remotely

Post by tonyf » Thu Dec 20, 2012 8:37 am

Hi

For reason when I run the follwoing code some incorrect file paths are returned when I loop through the file names and servers.

Output:
Altiris0010CG02 Ping Successful...
Altiris0010CG02ExpressImagesWindows7W7110108W7110108.002 = True
Altiris0010CG02ExpressImagesWindows7W7110108W7110108.003 = True
Altiris0010CG02ExpressImagesWindows7W7110108W7110108.img = True
Altiris0010CG02ExpressImagesXP110102XP110102.002 = False
Altiris0010CG02ExpressImagesXP110102XP110102.003 = False
Altiris0010CG02ExpressImagesXP110102XP110102.img = False
Altiris0010CG02ExpressImagesXP110102XP110102.txt = False
Altiris0010CG02ExpressImagesXP110102XP110101.txt = False
Altiris0010CG02ExpressImagesXP110102XP110102.txt = False
Altiris0010CG02ExpressImagesXP110102XP110103.txt = False
Altiris0010CG02ExpressImagesXP110102XP110104.txt = False
Altiris0010CG02ExpressImagesXP110102XP110105.txt = False
Altiris0010CG02ExpressImagesXP110102XP110106.txt = False
Altiris0010CG02ExpressImagesXP110102XP110107.txt = False
Altiris0010CG02ExpressImages = False
Altiris0010CG02ExpressImages = False
Altiris0010CG02ExpressImages = False
Altiris0010CG02ExpressImages = False

Code:
'Run Script in Cscript mode only
'------------------------------------------------------------------------------------------------------
Sub forceCScriptExecution
Dim Arg, Str
If Not LCase( Right( WScript.FullName, 12 ) ) = "cscript.exe" Then
For Each Arg In WScript.Arguments
If InStr( Arg, " " ) Then Arg = """" & Arg & """"
Str = Str & " " & Arg
Next
CreateObject( "WScript.Shell" ).Run "cscript //nologo """ & WScript.ScriptFullName & """" & Str
WScript.Quit
End If
End Sub
forceCScriptExecution
'------------------------------------------------------------------------------------------------------
Function Ping(Target)
Dim results

On Error Resume Next

Set shell = CreateObject("WScript.Shell")

' Send 1 echo request, waiting 2 seconds for result
Set exec = shell.Exec("ping -n 6 -w 2000 " & Target)
results = LCase(exec.StdOut.ReadAll)

Ping = (InStr(results, "reply from") > 0)
End Function

'Usage: If Ping("192.168.1.100") Then
' Do something to access the resource
'End If
'---------------------------------------------------------------------------------
Function FileToArray(ByVal strFile, ByVal blnUNICODE)
Const FOR_READING = 1
Dim objFSO, objTS, strContents

' BEGIN CALLOUT A
FileToArray = Split("")
' END CALLOUT A

Set objFSO = CreateObject("Scripting.FileSystemObject")

If objFSO.FileExists(strFile) Then
On Error Resume Next
Set objTS = objFSO.OpenTextFile(strFile, FOR_READING, False, blnUNICODE)
If Err = 0 Then
strContents = objTS.ReadAll
objTS.Close
' BEGIN CALLOUT B
FileToArray = Split(strContents, vbNewLine)
' END CALLOUT B
End If
End If
End Function
'---------------------------------------------------------------------------------

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Error Handling
'---------------------------------------------------------------------------------
If objFSO.FileExists("Files.txt") Then
Else
wscript.echo "Could not find files.txt...ending script"
wacript.quit
End If

If objFSO.FileExists("Servers.txt") Then
Else
wscript.echo "Could not find Servers.txt...ending script"
wacript.quit
End If
'----------------------------------------------------------------------------------

If objFSO.FileExists("Results.txt") Then
objFSO.DeleteFile "Results.txt"
End If

Set objInputFile = objFSO.OpenTextFile("servers.txt",1)
Set objOutputFile = objFSO.OpenTextFile("Results.txt",8,True)

'Set drive and Path here...
'-----------------------------------------------------------------------------------
strDrive = "D"
strPath = "ExpressImages"
'-----------------------------------------------------------------------------------
Do until objInputFile.AtEndofStream
strcomputer = objInputFile.ReadLine

'----------------------------------------------------------------------------------
If Ping(strComputer) Then
objOutputFile.WriteLine(strComputer & " " & "Ping Successful...")
Else
objOutputFile.WriteLine(strComputer & " " & "Could not be reached...")

End If
'---------------------------------------------------------------------------------

For Each strLine In FileToArray("files.txt", False)

'strCompleteFile = "" & strComputer & "" & strDrive & "$" & strPath & strLine
strCompleteFile = "" & strComputer & "" & strPath & strLine

wscript.echo "Searching for:" & " " & strCompleteFile
wscript.echo "Writing Output:"

If objFSO.FileExists(strCompleteFile) Then
Data = strCompleteFile & " " & "= True"
objOutputFile.WriteLine(Data)

Else
Data = strCompleteFile & " " & "= False"
objOutputFile.WriteLine(Data)

End If
Next
Loop

'----------------------------------------------------------------

objOutputFile.Close
objInputFile.Close

Wscript.quit

Please helpto fix this code or show me a better way how to accomplish this task.

Thanks

User avatar
jvierra
Posts: 13266
Joined: Tue May 22, 2007 9:57 am
Contact:

Script to search for multiple files on multiple servers remotely

Post by jvierra » Thu Dec 20, 2012 9:56 am

Go back and simplify. YOu do not need an array to process a file. Just read through the file and process each record.

Your structure should go like this:

read a line
' call sub to process line
loop until no more lines.


Like this:
While Not file.EOF
' line = file.Readline()
' ProcessLine line
Wend

Now make your code do for one manually typed in sample line until it works. Using WScript.StdErr.WriteLine to output trace messages will help you decode what is happening. StdErr will not place its output into a file that is created by redirecting the output of the script.

Build up a script from the middle out. Write teh code to process one element of the collection being processed then enumerate the collection and pass it to the element processing code.

Programming (scripting) is all about structures and flows (patterns).

User avatar
jvierra
Posts: 13266
Joined: Tue May 22, 2007 9:57 am
Contact:

Script to search for multiple files on multiple servers remotely

Post by jvierra » Thu Dec 20, 2012 10:32 am

Here (attached) is a starter and an example. This does all of the major bits. You just need to add any bells. Ask for how to do that as each step after this is exceptionally easy once you know how to do it.

Keep the script clean. Do not add large amounts of commenting. Use clear names so as to bypass the need to write comments. Modern code is "self-documenting". Excess comemnts an pretty straight lines only make the script harder to read and harder to understand.

Notice how short and concise the attached file is. Once you can get this working correctly (correct data output) you can add to it as needed and knwo the fundamentals work correctly.

Attached files /FileUpload/2a/6c9793bc88ac6e0143b36e47806fb8.txt (1.1 KB)

Locked