I'm trying to sort my files into unique dictionaries based on the file name and delimiters.
File names follow this structure: LocationName-DP-Category-S-L
Where... - is the delimiter
- Location name is a variable length, never has spaces
- DP is the two-letter department code
- Category is a variable length, never has spaces
- S is a single-letter size code
- L is length
Everything after category is irrelevant, as there is often some extra garble at the end of the filename to ignore.
I'd like to have a folder structure created and sorted for every found variation of just the LocationName-DP-Category portion of the filepath that looks something like this:
- Parent Folder: LocationName
- SubFolder: DP
- SubSubFolder: Category
Trying to keep it less rigid as this runs often on various batches of files. Here's what I have:
##GET USER INPUT FROM DIALOG
Add-Type -AssemblyName System.Windows.Forms
$browser = New-Object System.Windows.Forms.FolderBrowserDialog
$null = $browser.ShowDialog()
$path = $browser.SelectedPath + "\"
##SORT ALL FILES
Get-ChildItem -Path $path *-*-*-*-*.* |
Where-Object BaseName -match '(?<store>\w+)\-+(?<dept>\w+)\-+(?<category>\w+)*.*'|
Group-Object {$Matches["store"]+'-'+$Matches["dept"]+'-'+$Matches["Category"]}|
ForEach-Object{mkdir $_.Name;$_.Group|Move-Item -Dest $_.Name}
Would really appreciate the help, been scratching my head on and off for a couple months.
Thanks