Solved

VB script to copy file is no response from PING

Posted on 2010-09-22
11
743 Views
Last Modified: 2013-11-25
I want a VB script to copy a file if there is no response from an ip address, all I have so far is:

On Error Resume Next

Dim strTarget, strPingResults
strTarget = "10.78.33.253" 'IP address or hostname

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
Else
fso.CopyFile "C:\batch\*.eml", "q:\", True
End If
0
Comment
Question by:Cuteadder
  • 6
  • 5
11 Comments
 
LVL 13

Expert Comment

by:Surone1
ID: 33738921
i dont see the scripting.filesystemobject being created, but since it's only one file to copy you should probably use shell or shellexecute
0
 
LVL 7

Author Comment

by:Cuteadder
ID: 33765925
ok, sounds good, but I'm not sure about the usage on shell or shellexecute

I'm near enough a beginner on VB scripts
0
 
LVL 7

Author Comment

by:Cuteadder
ID: 33782349
I just need to move a file or run a batch file if there is no response on ping

-------------------------------------------------------------------------------------------------
On Error Resume Next

Dim strTarget, strPingResults
strTarget = "10.78.33.253" 'IP address or hostname

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
Else
ObjShell.Exec d:\hello.bat, 0, True
End If
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33782387
ObjShell.Exec "copy d:\hello.bat d:\destdir", 0, True
0
 
LVL 7

Author Comment

by:Cuteadder
ID: 33782531
Its not coming up with an error, it is coming up with a box that I presume is pinging something, but the end result isn't happening - the file is not been copied

--------------------------------------------------------------------

On Error Resume Next

Dim strTarget, strPingResults
strTarget = "10.78.33.253" 'IP address or hostname

Set WshShell = WScript.CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 3 -w 2000 " & strTarget) 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
Else
ObjShell.Exec "copy d:\hello.bat d:\destdir", 0, True
End If
0
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 13

Expert Comment

by:Surone1
ID: 33782980
Private Sub Form_Load()
Set WshShell = CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 3 -w 2000 " & "www.sr.net") 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
Else
WshShell.Exec "copy C:\batch\*.eml q:\"
End If
End Sub
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33782991
oops i left my testing url inthere
this gives me a file not found error so it is attempting to copy something
0
 
LVL 7

Author Comment

by:Cuteadder
ID: 33783259
Tried the code below, no result and no error message. tried on both a Win7 machine and a 2008 server

---------------------------------------------------------------------------------

Private Sub Form_Load()
Set WshShell = CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 3 -w 2000 " & "www.yahoo.co.uk") 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
WshShell.Exec "copy c:\new.txt c:\new"
Else
WshShell.Exec "copy c:\new.txt c:\new"
End If
End Sub
0
 
LVL 13

Accepted Solution

by:
Surone1 earned 500 total points
ID: 33783360
tried it myself got file not found error we are getting there, i gues we will have to call command prompt or supply the path to copy
the following should work:
Set WshShell = CreateObject("WScript.Shell")
Set WshExec = WshShell.Exec("ping -n 3 -w 2000 " & "www.yahoo.co.uk") 'send 3 echo requests, waiting 2secs each
strPingResults = LCase(WshExec.StdOut.ReadAll)
If InStr(strPingResults, "reply from") Then
WshShell.Exec "c:\windows\system32\xcopy c:\new.txt c:\new\"
Else
WshShell.Exec "c:\windows\system32\xcopy c:\new.txt c:\new"
End If
0
 
LVL 7

Author Closing Comment

by:Cuteadder
ID: 33783865
fantastic, thank you very much!
0
 
LVL 13

Expert Comment

by:Surone1
ID: 33783873
my pleasure
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Report events not being triggered 8 114
topping2 challenge 13 83
vb6 connector to mongodb 2 50
vbModal 12 35
This article is meant to give a basic understanding of how to use R Sweave as a way to merge LaTeX and R code seamlessly into one presentable document.
This is about my first experience with programming Arduino.
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now