Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

VB script to copy file is no response from PING

Posted on 2010-09-22
11
Medium Priority
?
799 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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
 
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 2000 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Whether you’re a college noob or a soon-to-be pro, these tips are sure to help you in your journey to becoming a programming ninja and stand out from the crowd.
What do responsible coders do? They don't take detrimental shortcuts. They do take reasonable security precautions, create important automation, implement sufficient logging, fix things they break, and care about users.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Starting up a Project

876 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