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

VBSCript to search for multiple files on multiple servers

Status
Not open for further replies.
May 22, 2003
42
US
Hi

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...
\\Altiris0010CG02\\Express\Images\Windows7\W7110108\W7110108.002 = True
\\Altiris0010CG02\\Express\Images\Windows7\W7110108\W7110108.003 = True
\\Altiris0010CG02\\Express\Images\Windows7\W7110108\W7110108.img = True
\\Altiris0010CG02\\Express\Images\XP110102\XP110102.002 = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110102.003 = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110102.img = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110102.txt = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110101.txt = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110102.txt = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110103.txt = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110104.txt = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110105.txt = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110106.txt = False
\\Altiris0010CG02\\Express\Images\XP110102\XP110107.txt = False
\\Altiris0010CG02\\Express\Images\ = False
\\Altiris0010CG02\\Express\Images\ = False
\\Altiris0010CG02\\Express\Images\ = False
\\Altiris0010CG02\\Express\Images\ = 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 = "\Express\Images\"
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 help to fix this code or show me a better way how to accomplish this task.

Thanks
 
Either change this line:
strPath = "\Express\Images\"
to this:
strPath = "\Express\Images"

Or change this line:
strCompleteFile = "\\" & strComputer & "\" & strPath & strLine
to this:
strCompleteFile = "\\" & strComputer & strPath & strLine

Either change should fix the problem. Also, change references of w[highlight #FCE94F]a[/highlight]cript.quit to wscript.quit
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top