我的目标是简单的任务。我想在提供的根文件夹“D:Temp\IMG”的所有子文件夹中创建常量名称“jpg”的文件夹,并将每个扩展名为“.jpg”的子文件夹中的所有文件移动到新创建的“jpg”文件夹。
我以为我可以在不深入了解 powershell 的情况下自己解决这个问题,但看来我不得不问。
到目前为止,我创建了这段代码
$Directory = dir D:\Temp\IMG\ | ?{$_.PSISContainer};
foreach ($d in $Directory) {
Write-Host "Working on directory $($d.FullName)..."
Get-ChildItem -Path "$($d.FullName)" -File -Recurse -Filter '*.jpg' |
ForEach-Object {
$Dest = "$($d.DirectoryName)\jpg"
If (!(Test-Path -LiteralPath $Dest))
{New-Item -Path $Dest -ItemType 'Directory' -Force}
Move-Item -Path $_.FullName -Destination $Dest
}
}
Run Code Online (Sandbox Code Playgroud)
我从中得到的是在每个子文件夹中创建文件夹“jpg”的无限循环。请问我的代码和逻辑哪里失败了?
小智 5
下面的脚本可以完成这项工作。
$RootFolder = "F:\RootFolder"
$SubFolders = Get-ChildItem -Path $RootFolder -Directory
Foreach($SubFolder in $SubFolders)
{
$jpgPath = "$($SubFolder.FullName)\jpg"
New-Item -Path $jpgPath -ItemType Directory -Force
$jpgFiles = Get-ChildItem -Path $SubFolder.FullName -Filter "*.jpg"
Foreach($jpgFile in $jpgFiles)
{
Move-Item -Path $jpgFile.FullName -Destination "$jpgPath\"
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
8467 次 |
| 最近记录: |