• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 224
  • Last Modified:

need time in this script, plus order

i have this script: not done by me... old employee set it up

is they a way to get it to show date/time by each ipaddress?

if not would it be possible to get it to rename csv file to date/time.csv?
Const ForReading = 1
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
If not objFSO.FileExists("servers.txt") THEN
wscript.echo "Please create a file named 'servers.txt' with one PC name to be pinged per line,"&_
vbcrlf&"with a hard return at the end of each line."
wscript.quit
end if
tempobj="temp.txt"
 
Set objTextFile = objFSO.OpenTextFile("servers.txt", ForReading)
logfile="C:\Inetpub\wwwroot\ipallresults.csv"
Set ofile=objFSO.CreateTextFile(logfile,True)
strText = objTextFile.ReadAll
objTextFile.Close
 
 
 
arrComputers = Split(strText, vbCrLF)
for each item in arrcomputers
objShell.Run "cmd /c ping -n 1 -w 1000 " & item & " >temp.txt", 0, True
Set tempfile = objFSO.OpenTextFile(tempobj,ForReading)
Do Until tempfile.AtEndOfStream 
temp=tempfile.readall
  striploc = InStr(temp,"[")
        If striploc=0 Then
            strip=""
        Else
            strip=Mid(temp,striploc,16)
            strip=Replace(strip,"[","")
            strip=Replace(strip,"]","")
            strip=Replace(strip,"w"," ")
            strip=Replace(strip," ","")
        End If      
    
        If InStr(temp, "Reply from") Then
             ofile.writeline item & ","&strip&","&"Online." 
        ElseIf InStr(temp, "Request timed out.") Then
             ofile.writeline item &","&strip&","&"No response (Offline)." 
        ELSEIf InStr(temp, "try again") Then
             ofile.writeline item & ","&strip&","&"Unknown host (no DNS entry)."
         
End If 
Loop
Next
tempfile.close
objfso.deletefile(tempobj)
ofile.writeline 
ofile.writeline ","&"Ping batch complete "&now()

Open in new window

0
James Murrell
Asked:
James Murrell
  • 7
  • 6
1 Solution
 
ExcelGuideConsultantCommented:
you would get something like in the code snippet. Test the script and reply with your results.
Const ForReading = 1
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
If Not objfso.FileExists("servers.txt") Then
wscript.echo "Please create a file named 'servers.txt' with one PC name to be pinged per line,"&_
vbCrLf& "with a hard return at the end of each line."
wscript.Quit
End If
tempobj = "temp.txt"
 
Set objTextFile = objfso.OpenTextFile("servers.txt", ForReading)
logfile = "C:\Inetpub\wwwroot\" & Now() & ".csv"
Set ofile = objfso.CreateTextFile(logfile, True)
strText = objTextFile.readall
objTextFile.Close
 
 
 
arrcomputers = Split(strText, vbCrLf)
For Each Item In arrcomputers
objShell.Run "cmd /c ping -n 1 -w 1000 " & Item & " >temp.txt", 0, True
Set tempfile = objfso.OpenTextFile(tempobj, ForReading)
Do Until tempfile.AtEndOfStream
temp = tempfile.readall
  striploc = InStr(temp, "[")
        If striploc = 0 Then
            strip = ""
        Else
            strip = Mid(temp, striploc, 16)
            strip = Replace(strip, "[", "")
            strip = Replace(strip, "]", "")
            strip = Replace(strip, "w", " ")
            strip = Replace(strip, " ", "")
        End If
    
        If InStr(temp, "Reply from") Then
             ofile.writeline now() & "," & item & ","&strip&","&"Online."
        ElseIf InStr(temp, "Request timed out.") Then
             ofile.writeline now() & "," & item &","&strip&","&"No response (Offline)."
        ElseIf InStr(temp, "try again") Then
             ofile.writeline now() & "," & item & ","&strip&","&"Unknown host (no DNS entry)."
         
End If
Loop
Next
tempfile.Close
objfso.deletefile (tempobj)
ofile.writeline
ofile.writeline "," & "Ping batch complete " & Now()

Open in new window

0
 
James MurrellProduct SpecialistAuthor Commented:
error line 13
path not found
0
 
ExcelGuideConsultantCommented:
yeah...I know what the problem is.

your question:
if not would it be possible to get it to rename csv file to date/time.csv?

it is possible but then pls tell me how it should look like. Because now it is defined like:
13.2.2009 12:00:52.CSV

Windows does not like special characters like ":" in a filename....

would you like to have it like:
13.2.2009 120052.CSV
or
13.2.2009 12.00.52.CSV

please clarify
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
James MurrellProduct SpecialistAuthor Commented:
13.2.2009 120052.CSV would be great
0
 
ExcelGuideConsultantCommented:
please test and reply with results, thanks.
Const ForReading = 1
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
If Not objfso.FileExists("servers.txt") Then
wscript.echo "Please create a file named 'servers.txt' with one PC name to be pinged per line,"&_
vbCrLf& "with a hard return at the end of each line."
wscript.Quit
End If
tempobj = "temp.txt"
 
Set objTextFile = objfso.OpenTextFile("servers.txt", ForReading)
logfile = "C:\Inetpub\wwwroot\" & Replace(Now(), ":", "") & ".csv"
Set ofile = objfso.CreateTextFile(logfile, True)
strText = objTextFile.readall
objTextFile.Close
 
 
 
arrcomputers = Split(strText, vbCrLf)
For Each Item In arrcomputers
objShell.Run "cmd /c ping -n 1 -w 1000 " & Item & " >temp.txt", 0, True
Set tempfile = objfso.OpenTextFile(tempobj, ForReading)
Do Until tempfile.AtEndOfStream
temp = tempfile.readall
  striploc = InStr(temp, "[")
        If striploc = 0 Then
            strip = ""
        Else
            strip = Mid(temp, striploc, 16)
            strip = Replace(strip, "[", "")
            strip = Replace(strip, "]", "")
            strip = Replace(strip, "w", " ")
            strip = Replace(strip, " ", "")
        End If
    
        If InStr(temp, "Reply from") Then
             ofile.writeline now() & "," & item & ","&strip&","&"Online."
        ElseIf InStr(temp, "Request timed out.") Then
             ofile.writeline now() & "," & item &","&strip&","&"No response (Offline)."
        ElseIf InStr(temp, "try again") Then
             ofile.writeline now() & "," & item & ","&strip&","&"Unknown host (no DNS entry)."
         
End If
Loop
Next
tempfile.Close
objfso.deletefile (tempobj)
ofile.writeline
ofile.writeline "," & "Ping batch complete " & Now()

Open in new window

0
 
James MurrellProduct SpecialistAuthor Commented:
sorry still
error line 13
path not found
0
 
ExcelGuideConsultantCommented:
are you sure C:\Inetpub\wwwroot\ exist?
0
 
James MurrellProduct SpecialistAuthor Commented:
yes when i run the original script which does have
logfile="C:\Inetpub\wwwroot\ipallresults.csv" it works



0
 
ExcelGuideConsultantCommented:
strange

I was able to test the code now and it works great here....

it seems like something goes wrong with these line:
logfile = "C:\Inetpub\wwwroot\" & Replace(Now(), ":", "") & ".csv"
Set ofile = objfso.CreateTextFile(logfile, True)

and the code says this path does not exist:
C:\Inetpub\wwwroot\

Try to change the path into
logfile = "C:\" & Replace(Now(), ":", "") & ".csv"

and see if you still get that error
0
 
James MurrellProduct SpecialistAuthor Commented:
still get error.....

i have managed to get it to work with



Const ForReading = 1
Set objfso = CreateObject("Scripting.FileSystemObject")
Set objShell = CreateObject("WScript.Shell")
If Not objfso.FileExists("servers.txt") Then
wscript.echo "Please create a file named 'servers.txt' with one PC name to be pinged per line,"&_
vbCrLf& "with a hard return at the end of each line."
wscript.Quit
End If
tempobj = "temp.txt"
 
Set objTextFile = objfso.OpenTextFile("servers.txt", ForReading)
logdir ="C:\Inetpub\wwwroot\"
logfile = logdir &  10000*Year(Date)+100*Month(Date)+Day(Date) & "_" & Replace(Time,":","-")& ".csv"
Set ofile = objfso.CreateTextFile(logfile, True)
strText = objTextFile.readall
objTextFile.Close
 
 
 
arrcomputers = Split(strText, vbCrLf)
For Each Item In arrcomputers
objShell.Run "cmd /c ping -n 1 -w 1000 " & Item & " >temp.txt", 0, True
Set tempfile = objfso.OpenTextFile(tempobj, ForReading)
Do Until tempfile.AtEndOfStream
temp = tempfile.readall
  striploc = InStr(temp, "[")
        If striploc = 0 Then
            strip = ""
        Else
            strip = Mid(temp, striploc, 16)
            strip = Replace(strip, "[", "")
            strip = Replace(strip, "]", "")
            strip = Replace(strip, "w", " ")
            strip = Replace(strip, " ", "")
        End If
    
        If InStr(temp, "Reply from") Then
             ofile.writeline now() & "," & item & ","&strip&","&"Online."
        ElseIf InStr(temp, "Request timed out.") Then
             ofile.writeline now() & "," & item &","&strip&","&"No response (Offline)."
        ElseIf InStr(temp, "try again") Then
             ofile.writeline now() & "," & item & ","&strip&","&"Unknown host (no DNS entry)."
         
End If
Loop
Next
tempfile.Close
objfso.deletefile (tempobj)
ofile.writeline
ofile.writeline "," & "Ping batch complete " & Now()

Open in new window

0
 
ExcelGuideConsultantCommented:
So all is ok?

happy to hear
0
 
James MurrellProduct SpecialistAuthor Commented:
yes thanks but you did solve the main problem :

Windows does not like special characters like ":" in a filename.... so thanks
0
 
James MurrellProduct SpecialistAuthor Commented:
cheers
0

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

  • 7
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now