How to use a script to create a GUID

Source: Internet
Author: User

Ask:
Hello, Scripting Guy! We need to use a unique identification number to track certain items. My boss suggested that we use GUIDs for this purpose. Is there any way you can use a script to create a GUID?
--DX

For:
Hello, DX. A GUID (globally unique identifier) applies to tasks that are similar to the following: We do not understand the algorithm on which the GUID is based, but we can almost certainly say that each GUID you generate is unique. Although there is a theoretical possibility of generating duplicate GUIDs, the possibility that Bill Gates would give all his money to the Scripting Guys is theoretically the same. Let's not bother with either of these two possibilities.

In fact, you can use a very simple method to generate GUIDs, but this approach is almost like cheating. (as you can hear, we're talking about "near.") The "Scriptlet.Typelib" object is designed to help you create a Windows scripting component (essentially, this is a way to make the script you write work like a COM object). The Scriptlet.Typelib object contains a method that generates a GUID that is used with the Windows Script Component type library, but there is no reason why this method cannot be used to generate GUIDs for other purposes. (after all, the GUID is the GUID.) If you need a GUID, the following script consisting of two lines of code can provide you with one:

Set TypeLib = CreateObject ("Scriptlet.Typelib")
WScript.Echo Typelib.guid

It's not bad, is it? Of course, this script simply echoes the GUID in a message box, and if you actually want to use the GUID elsewhere, you must type it. This is not a difficult task if the GUID is not as verbose as the following:

So it's a bit of a modification to the script, of course, to help people like us, who like to advertise themselves. Use the following script to generate the GUID, and then copy it to the clipboard instead of displaying it in a message box:

Set TypeLib = CreateObject ("Scriptlet.Typelib")
strGUID = Typelib.guid

Set objIE = CreateObject ("Internetexplorer.application")
Objie.navigate ("About:blank")
ObjIE.document.parentwindow.clipboardData.SetData "Text", strGUID
Objie.quit

If you want to know how we did it, in fact, we played a little trick. VBScript cannot copy items to the Clipboard, but Internet Explorer can. Therefore, we simply generate the GUID and then store the value in a variable named strGUID. Next, create an invisible instance of Internet Explorer and use the "Clipboarddata.setdata" method to copy the GUID to the Clipboard. You can do this by using the following line of code:

ObjIE.document.parentwindow.clipboardData.SetData "Text", strGUID

Quit Internet Explorer, and now where you want to use the GUID, you can paste it where.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.