【发布时间】:2020-03-15 05:51:02
【问题描述】:
我的脚本有一部分不起作用。目标是从文件夹中获取文件,通过文件名的某个方面过滤和组织它们,然后将它们移动到为它们创建了新目录的新文件夹中。即根据文件名按月和年组织。例如。 032 批准的保修 - 克罗地亚 - Case-2019 08-1419032,进入目录 2019,然后是 08。 下一步是创建一个全选功能,该功能在数字 01-12 之间循环。它做得很好。现在的问题是我想在 2017 年至 2019 年之间每年循环一次。这就是我卡住的地方。
这是我的试用代码,它不起作用:
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)
}
我希望它循环一年,然后是所有月份,然后是第二年等等。并在另一边为它们制作文件夹。我不太确定。错误信息是:
DoWork:无法处理参数“月”的参数转换。无法转换“System.Object[]” “System.Object[]”类型的值以键入“System.Int32”。 在行:46 字符:39 + 1..12 | ForEach-Object {DoWork($_, $year)} + ~~~~~~~~~~~ + CategoryInfo : InvalidData: (:) [DoWork], ParameterBindingArgumentTransformationException + FullyQualifiedErrorId : ParameterArgumentTransformationError,DoWork【问题讨论】:
标签: function powershell foreach