Solved

VBScript Check if RoboCopy Exists

Posted on 2013-11-27
7
803 Views
Last Modified: 2013-11-27
Hello all,

I'm working on writing a VBScript HTA to copy some files around based on the date they were modified and I'd love to use RoboCopy to do this over Xcopy. However, this script will be used on some 2003 servers and XP computers - not all of which are under my control. Therefore, not all of them have RoboCopy.

What I'd like to do here is check, when the script is run, if RoboCopy exists on this machine and if so set CopyMethod="RoboCopy". Otherwise, set CopyMethod="Xcopy". Then, every time I need to copy a file, I can just check the CopyMethod variable I created to see which method I should use.

I'm having a problem trying to determine if RoboCopy exists, though. From what I've read, it is not always in the System32 directory on all computers so I can't easily just check if the file exists (unless I can get a list of all of the possible locations where it might be). I found this on StackOverflow which checked the PATH system variable to see if RoboCopy is listed, but I'm not sure how to do this in VBScript. Any suggestions?

Thanks!
0
Comment
Question by:DawComp
  • 4
  • 3
7 Comments
 
LVL 67

Accepted Solution

by:
sirbounty earned 500 total points
ID: 39681335
Something like this should be sufficient...

On Error Resume Next
Dim objShell : Set objShell = CreateObject("Wscript.Shell")
Set objExec = objShell.Exec ("robocopy.exe")
CopyMethod = "XCopy"
If Err.Number = 0 Then
  CopyMethod = "Robocopy"
End If


wscript.echo "Copying via " & CopyMethod

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39681342
If you need to check for the specific error info, the details are:

Error: -2147024894
The system cannot find the file specified.
0
 

Author Comment

by:DawComp
ID: 39681356
Thanks for the response!

I guess I could do that. It's a little dirty but it would work.

I'd prefer to be able to do something like you can in batch like
for %%F in (robocopy.exe) do echo "%%~$path:F"

Open in new window

which checks if RoboCopy.exe exists and if so returns the path.
0
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 

Author Comment

by:DawComp
ID: 39681373
Well I guess I could do that via the method given here: http://stackoverflow.com/questions/8812848/how-can-i-find-path-of-file

Again kinda dirty but gets the job done. I might go this route just because it doesn't try to execute a file which may or may not exist. Not that it really matters.

I'll try both methods and see which works better for me. Thanks again for your help SirBounty! I'm marking your response as solution.
0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39681477
I would have preferred the chance to earn an "A", but glad that you found something to work with...

If you had given me a chance to fully identify what you were after, I would have given you this:

Dim objShell : Set objShell = CreateObject("Wscript.Shell")
Set objExec = objShell.Exec ("%comspec% /c for %f in (robocopy.exe) do echo ""%~$path:f""")

CopyMethod = "Xcopy" 'unless robocopy found

objExec.StdOut.ReadLine 'Read header
strData = objExec.StdOut.ReadLine
If Instr(lCase(strData), "robocopy") > 0 Then
  wscript.echo "Robocopy found at " & Mid(strData, Instr(strData, chr(34)))
  CopyMethod = "Robocopy"
End If

wscript.echo "CopyMethod using " & CopyMethod

Open in new window

0
 
LVL 67

Expert Comment

by:sirbounty
ID: 39681508
Ah, I see that link is very similar, though mine does use the environment %comspec% which ensures you're using the environmental CLI, as opposed to targeting cmd.exe, which might not exist on older platforms.
0
 

Author Comment

by:DawComp
ID: 39681731
That does it too. :) Thanks again.

Sorry, I didn't realize that you can't go back and change the accepted solution if a better solution is given.
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

759 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

17 Experts available now in Live!

Get 1:1 Help Now