Solved

VB script to copy file is no response from PING

Posted on 2010-09-22
11
751 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
split53 challenge 7 95
advertisement module in core php 4 159
When i run adoquery my application freezes 26 165
Macro Excel - Multiple If conditions 2 71
I was working on a PowerPoint add-in the other day and a client asked me "can you implement a feature which processes a chart when it's pasted into a slide from another deck?". It got me wondering how to hook into built-in ribbon events in Office.
Entering a date in Microsoft Access can be tricky. A typo can cause month and day to be shuffled, entering the day only causes an error, as does entering, say, day 31 in June. This article shows how an inputmask supported by code can help the user a…
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

813 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

15 Experts available now in Live!

Get 1:1 Help Now