Powershell:提取部分文件名以创建文件夹并将文件移动到文件夹中
Powershell: Extracting parts of file name to create folders and move files into the folder
我是 powershell 的新手,我正在努力实现以下目标:
我有一个格式相同的文件列表:SURNAME_FIRSTNAME_CODE1_CODE2_NAME.pdf/docx 等等
我想创建一个名为:SURNAME FIRSTNAME CODE1 的子文件夹
然后将所有相关文件移动到文件夹中。
我设法找到一篇文章来处理第一部分(即 SURNAME),但不太确定如何让它与其他部分一起使用...任何帮助将不胜感激。
这是我目前的情况:(感谢发帖人)
Get-ChildItem -File -Path $directory -Filter "*.*" |
ForEach-Object {
New-Item -ItemType Directory "$directory$($_.Name.Split("_")[0])" -Force;
Move-Item -Path $_.FullName -Destination "$directory$($_.Name.Split("_")[0])$($_.Name)"
}
我就是这样做的。它使用 Group-Object to group files that belong to the same directory, and the format operator 创建目录名称。如果您有任何问题,请告诉我。
Get-ChildItem $directory -File |
group { "{0} {1} {2}" -f $_.BaseName.Split("_") } |
foreach {
$newDir = Join-Path $directory $_.Name
New-Item $newDir -ItemType Directory
$_.Group | Move-Item -Destination $newDir
}
我是 powershell 的新手,我正在努力实现以下目标:
我有一个格式相同的文件列表:SURNAME_FIRSTNAME_CODE1_CODE2_NAME.pdf/docx 等等
我想创建一个名为:SURNAME FIRSTNAME CODE1 的子文件夹
然后将所有相关文件移动到文件夹中。
我设法找到一篇文章来处理第一部分(即 SURNAME),但不太确定如何让它与其他部分一起使用...任何帮助将不胜感激。
这是我目前的情况:(感谢发帖人)
Get-ChildItem -File -Path $directory -Filter "*.*" |
ForEach-Object {
New-Item -ItemType Directory "$directory$($_.Name.Split("_")[0])" -Force;
Move-Item -Path $_.FullName -Destination "$directory$($_.Name.Split("_")[0])$($_.Name)"
}
我就是这样做的。它使用 Group-Object to group files that belong to the same directory, and the format operator 创建目录名称。如果您有任何问题,请告诉我。
Get-ChildItem $directory -File |
group { "{0} {1} {2}" -f $_.BaseName.Split("_") } |
foreach {
$newDir = Join-Path $directory $_.Name
New-Item $newDir -ItemType Directory
$_.Group | Move-Item -Destination $newDir
}