【发布时间】:2019-07-19 01:49:19
【问题描述】:
我目前有 20000 多个文件夹,这些文件夹在创建时会给出随机字符串。我想用每个文件夹中修改的最后一个 PDF 的名称重命名每个文件夹。我肯定在我的头上。当前脚本似乎只是移动 PDF 和/或文件夹,而不重命名它或使用 PDF 名称创建文件夹。
Get-ChildItem -Path $SourceFolder -Filter *.pdf |
ForEach-Object {
$ChildPath = Join-Path -Path $_.Name.Replace('.pdf','') -ChildPath $_.Name
[System.IO.FileInfo]$Destination = Join-Path -Path $TargetFolder -ChildPath $ChildPat
if( -not ( Test-Path -Path $Destination.Directory.FullName ) ){
New-Item -ItemType Directory -Path $Destination.Directory.FullName
}
Copy-Item -Path $_.FullName -Destination $Destination.FullName
}
【问题讨论】:
-
使用
Get-ChildItem获取您的文件列表,使用Sort-Object按日期排序,使用Group-Object对目录名称进行分组,然后抓取最后一项以找到所需的PDF。最后,使用Rename-Item根据最新的PDF重命名目录。
标签: powershell file pdf directory renaming