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

x
?
Solved

VBScript Check if RoboCopy Exists

Posted on 2013-11-27
7
Medium Priority
?
841 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 1500 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
Independent Software Vendors: 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

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

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!

Question has a verified solution.

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

When it comes to writing scripts for a Client/Server computing environment it is essential to consider some way of enabling the authentication functionality within a script. This sort of consideration mainly comes into the picture when we are dealin…
This article is the result of a quest to better understand Task Scheduler 2.0 and all the newer objects available in vbscript in this version over  the limited options we had scripting in Task Scheduler 1.0.  As I started my journey of knowledge I f…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Loops Section Overview
Suggested Courses

916 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