© 2009 marky-b jukebox/tv

VBScript GUIDs (and how i used them too!) :)

My brother’s wedding was just last weekend, and they were looking to pay someone to combine a bunch of photos together into a slideshow to play during the reception.  Of course, I thought this was a task that wasn’t worth the amount of money they were going to pay (think triple digits).  So, by mentioning that doing such a project was not worth it, I think they took it that I would do it for them for free.. well, that is what happened anyways.

So, getting a ton of photos from them (little did I know, most were film, and would have warranted the triple digit price), I set to work.  Being the perfectionist that I am, I set off on the multi-night, multi-case-of-beer journey that is leveling all of their photos to a more eye-pleasing tone.

Once all the photo’s were looking good (or as good as they could :S ), I put them all in one folder, so it would be easier to access all of them from the slideshow software I used.  I soon came across my first problem.

The slideshow software I used didn’t have a “random” feature when adding photos to the timeline… what a pain!  Due to camera and scanner naming conventions,  a majority of the timeline showed a couple of random pictures, followed by a series of photos, then more random, then a series, and so on.

My first thought was to write a script to run through the photo directory and rename the files to [randomInts].jpg, and did just that.  However, my script bombed out about 10 photo’s in, when it hit a duplicate “random” filename.

Now, I suppose I could’ve just put a filename check into the script, but I didn’t feel like poking with it over and over, so I thought.. hmm.. GUIDs :)

Some googling later, I found a VBScript library that is used for GUIDs, and did a bit more research to come up with the following:

Function getGuid()
  Set objTypeLib = CreateObject("Scriptlet.TypeLib")
  guidNew = Left(objTypeLib.GUID, 38)
  guidNew = Left(guidNew, Len(guidNew) - 1)
  guidNew = Right(guidNew, Len(guidNew) - 1)
  Set objTypeLib = Nothing
  getGuid = guidNew
End Function

Now, because the GUID is returned with curly braces ( { }’s ) on either end, and those aren’t allowed in filenames, I chose to remove them with the Left() and Right() functions.

Just by calling that function, it will return a GUID (without the braces) as a string for use it just about anything you can throw it into.  I chose to use it for file renaming :)

Here’s the rest of the script.  I know I could have done something a bit better with the directory scanning, but it was a piece of code that I already had hanging around.  And, being the efficient coder that I am, reused.

strComputer = "."
strPhotoDir = "C:\Users\Admin\Desktop\Photos"
Set objWMIService = GetObject("winmgmts:\\" & _
  strComputer & "\root\cimv2")
 
Set colFiles = objWMIService.ExecQuery _
  ("ASSOCIATORS OF {Win32_Directory.Name='" _
  & strPhotoDir & "'} Where ResultClass =" _
  & "CIM_DataFile")
 
For Each objFile In colFiles
  strEnd = Right(objFile.Name, 8 )
  strNewName = objFile.Drive & objFile.Path & getGuid() & ".jpg"
  result = objFile.Rename(strNewName)
Next
 
Function getGuid()
  Set objTypeLib = CreateObject("Scriptlet.TypeLib")
  guidNew = Left(objTypeLib.GUID, 38)
  guidNew = Left(guidNew, Len(guidNew) - 1)
  guidNew = Right(guidNew, Len(guidNew) - 1)
  Set objTypeLib = Nothing
  getGuid = guidNew
End Function

That’s it!  Well, I know it wasn’t that complicated of a task anyways, but hey, someone out there might find it useful.

Enjoy

Photo By Unknown

Post a Comment

Your email is never published nor shared. Required fields are marked *

*
*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre lang="" line="" escaped="">