Solved

VBScript Check if RoboCopy Exists

Posted on 2013-11-27
7
820 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
[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
  • 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
Technology Partners: 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

Technology Partners: 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

Script to copy or move mouse-selected collection of files plus targets referenced by shortcuts (.lnk) The purpose of this article is to help illuminate the real challenges and options available (where they may exist) for utilizing simple scriptin…
With User Account Control (UAC) enabled in Windows 7, one needs to open an elevated Command Prompt in order to run scripts under administrative privileges. Although the elevated Command Prompt accomplishes the task, the question How to run as script…
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
Suggested Courses

615 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