我有一部分脚本无法使用。目标是从文件夹中获取文件,并根据文件名的一个方面对它们进行过滤和组织,然后将其移动到为其创建新目录的新文件夹中。即根据文件名按月份和年份进行组织。例如。 032批准的保修-克罗地亚-案例2019 08-1419032,进入目录2019,然后进入08。 下一步是创建全选功能,该功能在数字01-12之间循环。它做的很好。现在的问题是,我也想在2017-2019年之间每年循环一次。这就是我卡住的地方。 这是我的代码,可以正常工作,但仅适用于所有月份和所选的1年:
function DoWork { param ([int]$Month)
$StrMonth = $Month.ToString("00")
Echo $StrMonth.ToString("00")
$files = Get-ChildItem $destinationpath -Filter "*$group1 $StrMonth*" -Recurse
foreach ($file in $files)
{
$year = $group1.ToString()
$month = $Month.ToString()
$file.Name
$year
$StrMonth
# Set Directory Path
$Directory = $targetPath + "\" + $year + "\" + $StrMonth
# Create directory if it doesn't exsist
if (!(Test-Path $Directory))
{
New-Item $directory -type directory
}
# Move File to new location
$file | move-Item -Destination $Directory -Force
}
}
if ($group -eq 'Select All') {
1..12 | ForEach-Object {DoWork($_)}
} else {
DoWork($group)
}
我也希望它可以重复多次(全选)。 建议使用此代码,但不起作用:
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)
}
答案 0 :(得分:0)
我发现您的函数中的param块不正确,请如下定义param块:
Param([int]$Month,[int]$Year)