Code Snippets
This is a collection of code snippets, in various languages, that I'd like to share. Some are just functions, subroutines, or classes and others are complete scripts that can be used as-is.
VBScript: Create Shortcut (.lnk) File
Submitted by Matthew Clark on Sat, 31 Oct 2009 - 14:20Following is a starting point for a script which will create a standard shortcut on the desktop.
- Add new comment
- 149 reads
VB.NET: Get User's SID from Active Directory
Submitted by Matthew Clark on Thu, 29 Oct 2009 - 20:31This function will connect to Active Directory and search for a specified username, and if found, return that user's SID.
Usage: GetSIDUsingADSearch("john.doe")
Returns: S-1-5-21-53580104-3876648466-4083845538-427214
- Add new comment
- 311 reads
VB.NET: Parse "Special" Shortcuts
Submitted by Matthew Clark on Thu, 29 Oct 2009 - 14:16I recently had a problem in one of my applications where, when trying to parse .lnk files for target (using the WshShell approach), I would get something like C:\Windows\Installer\{GUID} instead of the path to the executable. Turns out some .lnk files (shortcuts) are of a different breed, ala "MSI Shortcut" or "Windows Installer Advertised Application".
Here is a module which will let you get the Target path of such a shortcut. It returns NULL if it's a standard .lnk, which you could use to determine whether to use the WshShell way instead.
- Add new comment
- 175 reads
VB.NET: Encrypt/Decrypt String (DES)
Submitted by Matthew Clark on Tue, 27 Oct 2009 - 18:28These functions will encrypt and decrypt (using DES) a string using a secret key.
Encrypt("I need this string to be a secret", "$ThIsIsAsUpErSeCrEtKeY!") will return a string with the encrypted text.
Decrypt("5FkBed78KfrLmoU==BNu37N5kl", "$ThIsIsAsUpErSeCrEtKeY!") will return the original string that was encrypted with Encrypt() and the same key.
If a different key is used for Decrypt() than was originally used for Encrypt(), an exception will be thrown.
- Add new comment
- 657 reads
VBScript: Recursively Delete Files
Submitted by Matthew Clark on Fri, 18 Sep 2009 - 16:49This script will attempt to delete all files in a particular location, including subfolders, but silently continue on error.
- Add new comment
- 138 reads
VBScript: Get a computer's IP address(es)
Submitted by Matthew Clark on Mon, 07 Sep 2009 - 02:19Returns a string of addresses, separated by pipe (|), with a leading pipe:
|192.168.0.12|172.16.31.64|10.1.1.2
- Add new comment
- 163 reads
VBScript: Get Script's Location
Submitted by Matthew Clark on Fri, 24 Jul 2009 - 23:45The scriptLocation variable will contain the path to executing script.
Set objFSO = CreateObject("Scripting.FileSystemObject") scriptLocation = objFSO.GetFile(Wscript.ScriptFullName).ParentFolder
- Add new comment
- 189 reads
CSS: Remove indent and bullet from list items
Submitted by Matthew Clark on Tue, 30 Jun 2009 - 06:27This style will remove the indent and bullet from a list
ul, ol { list-style: none; margin-left: 0; padding-left: 1em; text-indent: -1em; }
- Add new comment
- 510 reads
PHP: Create a Microsoft GUID
Submitted by Matthew Clark on Sun, 28 Jun 2009 - 01:34This function will return a standard 128-bit GUID, consisting of one group of 8 hexadecimal digits, followed by three groups of 4 hexadecimal digits each, followed by one group of 12 hexadecimal digits.
Example: 6B29FC40-CA47-1067-B31D-00DD010662DA
- Add new comment
- 163 reads
Visual Basic .NET: Get carat position in a RichTextBox control
Submitted by Matthew Clark on Fri, 26 Jun 2009 - 00:05This function can be called by KeyPress, Click, and similar events to return a string with the X and Y coordinates of the carat within a RichTextBox control.
Example: "Line 35, Col 18"
Function CursorPosition(ByVal Control) Return "Line " & (Control.GetLineFromCharIndex(Control.SelectionStart) + 1).ToString & ", Col " & (Control.SelectionStart - Control.GetFirstCharIndexOfCurrentLine() + 1).ToString End Function
- Add new comment
- 160 reads
