微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用powershell将子文件夹内容上移一层;不同的父文件夹

如何解决使用powershell将子文件夹内容上移一层;不同的父文件夹

我已经查看了有关 SO 的各种问题,但无法使脚本正常工作。 Powershell 新手,我相信这很容易 - 任何帮助都会很棒

我有 170 个文件夹的目录结构:

    G:\Data\ 
            folder1\
                     DCIM1\*jpgs
                     DCIM2\*jpgs

            folder2\
                     DCIM1\*jpgs
                     DCIM2\*jpgs

我想将每个 DCIM 子文件夹中的所有 jpg 移动到上一级的父文件夹中:

      G:\Data\ 
            folder1\*jpgs         
            folder2\*jpgs

每个父文件夹都有不同的名称,可能还有不同命名的 DCIM 子文件夹,但所有 jpg 都以其文件夹前缀命名(例如 DCIM1_001.jpg)。

我已经尝试过 Powershell:

G:\> $files = Get-ChildItem "G:\data"
>> Get-ChildItem $files | Move-Item -Destination { $_.Directory.Parent.FullName }
>> $files | Remove-Item -Recurse 

获取目的地为空错误。我也试过通配符:

 G:\> $files = Get-ChildItem "G:\data\*"
    >> Get-ChildItem $files | Move-Item -Destination { $_.Directory.Parent.FullName }
    >> $files | Remove-Item -Recurse 

但我认为我完全错了。我错过了什么?

解决方法

您可以使用 Split-Path 获取父目录:

$JPGs = Get-ChildItem -Path "C:\brief\datefolder" -Recurse -Filter "*.jpg"
    foreach ($JPG in $JPGs) {

        $Parent_Directory = Split-Path -Path $JPG.FullName -Parent
        $Destination_Path = Split-Path -Path $Parent_Directory -Parent
        
        Move-Item -Path $JPG.FullName -Destination $Destination_Path
            if ($null -eq (Get-ChildItem -Path $Parent_Directory)) {

                Remove-Item -Path $Parent_Directory

            }
    }

这只是将其分配给变量并沿线移动的一种方式。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。