I have a part of my script that doesn't work. The goal is, to take files from a folder, filter and organise them by an aspect of their filename, and move them to a new folder which has had new directories made for them. i.e organised by month and year based on file name. E.g. 032 Approved warranty - Croatia - Case-2019 08-1419032, goes into a directory 2019, then 08. The next step was creating a select all function, which cycled through numbers 01-12. Which it does fine. Now the issue is I want to cycle for each year as well between 2017-2019. Which is where i'm stuck.
This is my trial code which doesn't work:
function DoWork {
Param([int]$Month) ([int]$Year)
$StrMonth = $Month.ToString("00")
echo $StrMonth.ToString("00")
$StrYear = $Year.ToString
echo $StrYearToString
$files = Get-ChildItem $destinationpath -Filter "*$StrYear $StrMonth*" -Recurse
foreach ($file in $files) {
$year = $Year.ToString()
$month = $Month.ToString()
$file.Name
$StrYear
$StrMonth
# Set Directory Path
$Directory = $targetPath + "\" + $StrYear + "\" + $StrMonth
if (!(Test-Path $Directory)) {
New-Item $directory -Type Directory
}
$file | Copy-Item -Destination $Directory -Force
}
}
if ($group1 -eq 'Select All') {
2017..2019 | ForEach-Object {
$year = $_
1..12 | ForEach-Object {DoWork($_, $year)}
}
} elseif ($group -eq 'Select All') {
1..12 | ForEach-Object {DoWork($_, $group1)}
} else {
DoWork($group, $group1)
}
I want it to loop through a year, then all the months, then the next year etc. And make folders for them on the other side. I can't quite suss it. The error message is:
DoWork : Cannot process argument transformation on parameter 'Month'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "System.Int32". At line:46 char:39 + 1..12 | ForEach-Object {DoWork($_, $year)} + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [DoWork], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,DoWork