GUID Filename Renaming for Easy Identification Using PowerShell

Posted by:

|

On:

|

A GUID (Globally Unique ID) is a random set of characters sometimes used as a filename. This script has a use case that is not uncommon.

GUID Rename Use Case

  • 3rd party drops files on server
  • files are named by GUID and placed in generic folders
  • generic folders are held by folders that can be identified by name
  • it is your job to identify and move these files to a processing folder

I have alchemy reports sorted by active ingredient in my “C:\Users\Wiz\Alchemy” folder, in this case Mercury. Inside, I have many subfolders. The subfolder “apprentices_maimed” holds the specific report I want to analyze and later send for insurance reasons. I have many folders, such as “Alc-Lead” or “Alc-Hemlock”, for their own insurance like Toxic Metals, Plants, etc with their own apprentices maimed. I have lots of reports to file, so I’m going to send these to my lawyer at \\WizFile\Legal. The reports are not saved in helpful manners, ie “7ef8-24ks-40is-vnbm.xlsx”.

Parameters Needed

$filePattern – The string of the parent folder that won’t change: "Alc-"
$processFolder – Destination folder of files: "\\WizFile\Legal"
$fileServer – Where I’m taking the files from: "C:\Users\Wiz\Alchemy"
$subFolder – The sub folder I’m looking for: $apprentices_maimed"
$fileType – The type of file to grab: "xlsx"

Getting the GUID Step-by-Step

First off, I want to list all files in my Alchemy directory. This will pull “Alc-*”

PowerShell
$sourceDirectories = Get-ChildItem -Path $fileServer -Filter "$filePattern*" -Directory

Next, I want to extract the Insurance ID Mercury, then search for the contents of “apprentices_maimed” for my report. I get the folders by concatenating the child folder I’m expecting with the folder I know should be a parent.

PowerShell
foreach ($childDir in $sourceDirectories) {
        $InsuranceID = $childDir.Name -replace "$filePattern", ""
        $subDirectory = Join-Path -Path $childDir.FullName -ChildPath $subFolder
        $sourceFiles = Get-ChildItem -Path $subDirectory -Filter "*$fileType"

Afterward, I want to search the folders for the worksheet. For ease of tracking, I’m also going to output where each file is going. I’m using $_.FullName to ensure the correct files are getting moved. Afterward, I’m going to copy the file while renaming it to Mercury.xlsx so that when it’s in the folder, my lawyer will know which one it is at a glance.

PowerShell
foreach ($file in $sourceFiles) {
    $destination = Join-Path -Path $processFolder -ChildPath "$InsuranceID.$FileType"
    Copy-Item $file.FullName -Destination $destination
    Write-Output ("Copying " + $file.FullName + " to " + $destination)

That’s about it. It’s a slow script and not optimized for speed, but gets the job done.

Putting it All Together to get the GUID

Here is the same thing in function format:

PowerShell
function Move-WizFiles {
    [Parameter(Mandatory = $true)]
    [string]$filePattern,

    [Parameter(Mandatory = $true)]
    [string]$processFolder,

    [Parameter(Mandatory = $true)]
    [string]$fileServer, 

    [Parameter(Mandatory = $true)]
    [string]$subFolder, 

    [Parameter(Mandatory = $true)]
    [string]$fileType

    $sourceDirectories = Get-ChildItem -Path $fileServer -Filter "$filePattern*" -Directory

    foreach ($ChildDir in $sourceDirectories) {
        $InsuranceD = $ChildDir.Name -replace "$filePattern", ""
        $subDirectory = Join-Path -Path $sourceDir.FullName -ChildPath $subFolder
        $sourceFiles = Get-ChildItem -Path $subDirectory -Filter "*$fileType"
    
        foreach ($file in $sourceFiles) {
            $destination = Join-Path -Path $processFolder -ChildPath "$InsuranceID.$FileType"
            Copy-Item $file.FullName -Destination $destination
            Write-Output ("Copying " + $file.FullName + " to " + $destination)
        }
    }
    Return("All files procesed.")
}