我尝试运行以下代码,因此将一些银行文档对相应的文件夹进行分类。如果文件名在同一目录中包含一个文件夹名称,则应将文件移至匹配文件夹。如果foldernames都不是文件名的一部分,则应将相应的文件移至文件夹Other
。
Set-ExecutionPolicy -ExecutionPolicy Unrestricted
$FileType = "*.pdf"
$folder = Get-ChildItem -path "C:UsersMynameDocuments" -Filter $FileType
$map = @{
Wertpapierabrechnung = "*$Wertpapierabrechnung*"
Wertpapierertrag = "*_Wertpapierertrag*"
Kontoauszug = "*$Kontoauszug*"
Depotauszug = "*$Depotauszug*"
Kapitalmaßnahmen = "*$Kapitalmaßnahmen*"
}
ForEach($file in $folder)
{
$file
$key = $file.BaseName
$key
$map.Keys
if ($key -like $map.Keys){
Copy-Item -Path $file.FullName -Destination ### SOME PATH JOINING ###
}
else{
"Copy Item did not work."}
}
但是,我总是在其他循环中运行。我不知道为什么。非常感谢您的帮助!
目前尚不清楚您要寻找的零件以及如何命名文件夹。
样本树之前:
> tree /F
│ bar Wertpapierertrag 2016-04-08.pdf
│ baz Kontoauszug 2018-01-30.pdf
│ blah Depotauszug 2017-10-15.pdf
│ foo other 2019-05-19.pdf
│ foo Wertpapierabrechnung 2017-10-15.pdf
│ test Kapitalmaßnahmen 2016-04-08.pdf
│
├───Depotauszug
├───Kapitalmaßnahmen
├───Kontoauszug
├───Other
├───Wertpapierabrechnung
└───Wertpapierertrag
运行此脚本
## Q:Test2019 519SO_56211858.ps1
$Folder = [Environment]::GetFolderPath("MyDocuments")
$FileType = "*.pdf"
$Files = Get-ChildItem -Path $Folder -Filter $FileType
$RE = [regex]"(Kapitalmaßnahmen|Kontoauszug|Wertpapierabrechnung|Wertpapierertrag|Depotauszug)"
# alternativly build the RegEx from current subfolder names (might need escaping)
# $RE = [regex]((Get-ChildItem -Path $Folder -Directory -Name) -Join '|')
ForEach($file in $Files){
if ($file.BaseName -Match $RE){
$file | Move-Item -Destination (Join-Path $Folder $Matches[0]) -WhatIf
} else {
$file | Move-Item -Destination (Join-Path $Folder "Other") -WhatIf
}
}
如果输出看起来还可以,请删除后续的-WhatIf
参数。
脚本后的树:
> tree /F
├───Depotauszug
│ blah Depotauszug 2017-10-15.pdf
│
├───Kapitalmaßnahmen
│ test Kapitalmaßnahmen 2016-04-08.pdf
│
├───Kontoauszug
│ baz Kontoauszug 2018-01-30.pdf
│
├───Other
│ foo other 2019-05-19.pdf
│
├───Wertpapierabrechnung
│ foo Wertpapierabrechnung 2017-10-15.pdf
│
└───Wertpapierertrag
bar Wertpapierertrag 2016-04-08.pdf