VBScript Check if RoboCopy Exists

Posted on 2013-11-27
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?

Question by:DawComp
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
  • 4
  • 3
LVL 67

Accepted Solution

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

LVL 67

Expert Comment

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

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

Author Comment

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.
Industry Leaders: 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!


Author Comment

ID: 39681373
Well I guess I could do that via the method given here:

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.
LVL 67

Expert Comment

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

LVL 67

Expert Comment

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.

Author Comment

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.

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article we want to have a look at the directory attributes which are used by Microsoft to store the so called Security Identifiers (SID). These SIDs plays an important role in delegating and granting permissions and in authentication of trus…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit If you want to manage em…

734 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