Powershell Scripting help for sorting files into directories

I have an existing script that someone wrote for me that takes phone recording files from one  location where all the files are dumped and sorts them into many sub directories. The information for sorting them is all within the file names.

They currently go by Extension\date\(Incoming or Outgoing)

The extension directories are growing quite large making it hard to find the day you are looking for.

I need to modify this to split the date into directories for year\month\day

So and incoming call today would end up like this ...\Ext-201\2017\10\30\incoming\filename.mp3

Filenames are formatted like this:
20170516_041636PM_From-9145555555_To-201_Ext-201.mp3 (this is an incoming call)
20170516_013713PM_From-201_To-9145555555_Ext-201.mp3 (this is an outgoing call)

Here is what I currently have:
param ( [string]$p1 )

# Set source directory to Incoming FTP directory
$SourceFolder = "E:\FTP\$p1"

#Set target directory where the organized folders will be created
$TargetRoot = "E:\Recordings\$p1"

$Pattern = '(?<Date>\d{8})_(?:.*?)_From-(?<From>[^_]+)_To-(?<To>\d+)_Ext-(?<Ext>\d+)'
Get-Childitem -Path $SourceFolder -Filter *.mp3 | ForEach-Object {
	Write-Host "Processing '$($_.Name)'" -NoNewline
	If ($_.BaseName -match $Pattern) {
		$CallType = If ($Matches['From'] -eq $Matches['Ext']) {'Outgoing'} Else {'Incoming'}
		$TargetFolder = "$($TargetRoot)\Ext-$($Matches['Ext'])\$($Matches['Date'])\$($CallType)"
		Write-Host " --> '$($TargetFolder)'"
		If (-not (Test-Path -Path $TargetFolder)) {
			New-Item -Path $TargetFolder -ItemType Directory | Out-Null
		}
		Move-Item -Path $_.FullName -Destination $TargetFolder
	} Else {
		Write-Host ""
		Write-Warning -Message "File '$($_.Name)' does not match the expected pattern."
	}
}

Open in new window


Thanks in advance for any help.
LVL 1
bts86Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Panagiotis ToumpaniarisSystem EngineerCommented:
Check if this works for you :

param ( [string]$p1 )

# Set source directory to Incoming FTP directory
$SourceFolder = "E:\FTP\$p1"

#Set target directory where the organized folders will be created
$TargetRoot = "E:\Recordings\$p1"

$Pattern = '(?<Date>\d{8})_(?:.*?)_From-(?<From>[^_]+)_To-(?<To>\d+)_Ext-(?<Ext>\d+)'
Get-Childitem -Path $SourceFolder -Filter *.mp3 | ForEach-Object {
	Write-Host "Processing '$($_.Name)'" -NoNewline
	If ($_.BaseName -match $Pattern) {
        $Year = $($Matches['Date']).Substring(0,4)
        $Month = $($Matches['Date']).Substring(4,2)
        $Day = $($Matches['Date']).Substring(4,2)
		$CallType = If ($Matches['From'] -eq $Matches['Ext']) {'Outgoing'} Else {'Incoming'}
		$TargetFolder = "$($TargetRoot)\Ext-$($Matches['Ext'])\$($Year)\$($Month)\$($Day)\$($CallType)"
		Write-Host " --> '$($TargetFolder)'"
		If (-not (Test-Path -Path $TargetFolder)) {
			New-Item -Path $TargetFolder -ItemType Directory | Out-Null
		}
		Move-Item -Path $_.FullName -Destination $TargetFolder
	} Else {
		Write-Host ""
		Write-Warning -Message "File '$($_.Name)' does not match the expected pattern."
	}
}

Open in new window

1

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
aikimarkCommented:
This was for my own learning.  Panagiotis Toumpaniaris has a complete solution and deserves the points.

Purpose of this different pattern is to parse the date, replacing the substring operations.  Also, replacing the If statement with an index based on the $From -eq $Ext comparson.  Also, all variables are assigned in a single statement.
$Pattern = '^(\d{4})(\d{2})(\d{2})_[^_]+_From-([^_]+)_To-(\d+)_Ext-(\d+)'
Get-Childitem -Path $SourceFolder -Filter *.mp3 | ForEach-Object {
    Write-Host "Processing '$($_.Name)'" -NoNewline
    If ($_.BaseName -match $Pattern) {
        $Year, $Month, $Day, $From, $To, $Ext = $matches[1..($matches.count)]
        $CallType= @('Outgoing', 'Incoming')[$From -eq $Ext]

        #Rest of code remains unchanged
    }

Open in new window

0
bts86Author Commented:
Thanks, this works. I just had to make a minor edit on line 15:
$Day = $($Matches['Date']).Substring(6,2)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.