I have a series of documents that all begin with their file number (File number is always 6 characters):
A12345_Letter.pdf
A12346_Email.pdf
I need to move them to the appropriate subfolder in their file. Each file will only have one destination folder, but sometimes the folder name contains additional characters:
C:\Directory\Destination\A12345\Corres C:\Directory\Destination\A12346 - KlA69 LJ236\Corres
There may be numbers or letters, there may or may not be a space after the file number- there is no pattern or set number of characters.
This is what I've come up with:
$sourceDirectory = "C:\Directory\Source"
$files = Get-ChildItem -File -Path $sourceDirectory
$destination = "C:\Directory\Destination"
$filesDictionary = @{}
foreach ($file in $files) {
$prefix = $file.Name.Substring(0, 6)
$folderPath = Join-Path -Path $destination -ChildPath $prefix | Join-Path -ChildPath "Correspondence"
$destinationPath = Join-Path -Path $folderPath -ChildPath $file.Name
Move-Item -Path $file.FullName -Destination $destinationPath
}
(At this time I do not want to create a destination folder if one doesn't exist)
This works for scenarios where the name of the destination folder is just the 6-character file number, but not scenarios where there are additional characters in the destination folder name.
I'm trying to edit my $folderPath line so the first -Childpath starts with $prefix. I am just teaching myself Powershell in my spare time; I've done some reading about StartsWith, but I can't figure out how to make it work in this Join-Path scenario.