Solved

VB script to copy file is no response from PING

Posted on 2010-09-22
11
790 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

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

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

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.
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Introduction to Processes
Suggested Courses

632 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