08-18-2011 08:57 AM
Here's the script:
# PowerShell 2.0
# Expand-LenovoBaseFileNames
# This script parses the saved source code of Lenovo's My Download List and adds
# the display name, version and date information to the original file name for
# those files associated with that particular My Download List.
# To save the My Download List, first add all desired downloads to the list.
# Then click on My Download List. The following instructions apply to IE9:
# View the source code by typing ALT, V, C.
# Save the source code by typing CTRL+S, ENTER.
# Specify the file name below:
$myDownloadListSourceCode = "C:\Users\Don\Documents\Lenovo Support - My Downloads.htm"
# Download the files in My Download List and specify the saved directory below:
$downloadsDirectory = "C:\Users\Don\Downloads\"
# Get source code lines containing display name, version and date information:
$selectionString = "myDownLoadUrl"
$selection = Select-String -Path $myDownloadListSourceCode -Pattern $selectionString
# Evaluate each line for the necessary information, verify that the file has
# actually been downloaded, and then expand the file name.
foreach ($line in $selection)
{
# A little regular expression magic.
$matchFound = $line -match "href=`"http://download\.lenovo\.com.*/" +
"(?<baseName>[^\.]*)" + "\." +
"(?<baseExtension>[^`"]*)" + "`">" +
"(?<displayName>[^<]*)" + "(.*</td><td>){3}" +
"(?<displayVersion>[^<]*)" + ".*" +
"(?<displayDate>\d{2}\s[a-z]{3}\s\d{4})"
# If the regular expression fails, exit this line.
if (!$matchFound)
{continue}
# If the file does not exist, exit this line.
if (!(Test-Path -Path ($downloadsDirectory +
$matches.baseName + "." +
$matches.baseExtension)))
{continue}
# Expand the file name to include the My Download List details.
Rename-Item -Path ($downloadsDirectory +
$matches.baseName + "." +
$matches.baseExtension) `
-NewName ($matches.baseName + " - " +
$matches.displayName.trim() + " - " +
$matches.displayVersion.trim() + " - " +
$matches.displayDate + "." +
$matches.baseExtension)
}
# Goodbye.
""
"Expand-LenovoBaseFileNames has completed."