小编Max*_*axT的帖子

Powershell:循环子目录并移动文件

我的目标是简单的任务。我想在提供的根文件夹“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”的无限循环。请问我的代码和逻辑哪里失败了?

powershell

3
推荐指数
1
解决办法
8467
查看次数

标签 统计

powershell ×1