Solved

VBScript Check if RoboCopy Exists

Posted on 2013-11-27
7
808 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Welcome to part one of a multi-part tutorial series, VBScript for Windows System Administrators.  The goal of this series is to teach non-programmers how to write useful VBS code to automate their environment, and perform tasks faster, and in a more…
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…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

863 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

16 Experts available now in Live!

Get 1:1 Help Now