【问题标题】:Create a folder from a file name which has 2 underscores, then place that file in the folder从包含 2 个下划线的文件名创建一个文件夹,然后将该文件放入该文件夹中
【发布时间】:2020-03-03 23:42:24
【问题描述】:

我尝试使用 PowerShell 为以下文件创建一个文件夹

4_2017-07-16_01-22-52.mp4
4_2017-07-16_01-23-50.mp4
4_2017-07-16_01-24-54.mp4
4_2017-07-16_01-26-21.mp4

我用这个方法

https://stackoverflow.com/a/41468253/13002495

它会创建一个目录 4 然后将文件移动到它的问题,我需要一个目录,如下所示

4_2017-07-16 或类似 4_2017_07_16 的目录

这是第一种方法。

第二种方法,如果你可以帮助有一个脚本来创建以下目录

2017 目录,然后是子目录 02,然后是子目录 16,然后是子目录 4,然后将文件移动到子目录 4

所以如下

2017
--------07
------------16
---------------------04 ----> files will be here 

你能帮忙解决这两种方法吗?

【问题讨论】:

  • _.* 更改为.*_ 以解决第一个问题。

标签: windows powershell


【解决方案1】:

你可以试试这样的:

$folder = 'FILES_FOLDER'

Get-ChildItem -Path $folder | ForEach-Object {
    $subFolders = $_.Name.Split("-_")
    $path = Get-Location
    $order = 1, 2, 3, 0

    $order | ForEach-Object {
        $path = Join-Path -Path $path -ChildPath $subFolders[$_]

        if (-not (Test-Path -Path $path -PathType Container)){
            New-Item -Path $path -ItemType Directory
        }
    }

    Move-Item -Path $_.FullName -Destination $path
}

这会将所有文件移动到:

2017/07/16/4/4_2017-07-16_01-22-52.mp4
2017/07/16/4/4_2017-07-16_01-23-50.mp4
2017/07/16/4/4_2017-07-16_01-24-54.mp4
2017/07/16/4/4_2017-07-16_01-26-21.mp4

说明:

  • "-""_" 上的文件拆分为Split。可以关注about_split了解更多信息。
  • Get-Location获取当前文件夹路径,用于追加到当前路径,制作子目录。
  • 创建一个$order 数组以创建正确的子文件夹顺序,如问题所示。
  • 遍历此$order 数组并创建新目录(如果它们不存在)。可以使用Test-Path检查子文件夹是否存在,New-Item创建新目录。
  • 将文件移动到带有Move-Item 的最终子目录。这些子目录将位于您当前的工作目录中。您显然也可以将其更改为另一个目录位置。

【讨论】:

    【解决方案2】:

    对于您的第一种方法(一个名称类似于 4_2017_07_16 的目标文件夹),您可以这样做:

    $source      = 'D:\Mp4Files'  # rootfolder where the files are
    $destination = 'D:\Test'      # rootfolder where the files need to go
    Get-ChildItem -Path $source -File -Filter '*.mp4' |
      Group-Object { ($_.BaseName -replace'(\d+_[^_]+).*', '$1') } |
      ForEach-Object {
        $targetFolder = Join-Path -Path $destination -ChildPath $_.Name
        # create this folder if it does not already exist
        if (!(Test-Path -Path $targetFolder -PathType Container)) {
            $null = New-Item -Path $targetFolder -ItemType Directory
        }
        $_.Group | Move-Item -Destination $targetFolder
    }
    

    结果:

    D:\TEST\4_2017-07-16
        4_2017-07-16_01-22-52.mp4
        4_2017-07-16_01-23-50.mp4
        4_2017-07-16_01-24-54.mp4
        4_2017-07-16_01-26-21.mp4
    

    第二种方法根据文件名的第一部分创建更多子文件夹:

    $source      = 'D:\Mp4Files'
    $destination = 'D:\Test'
    Get-ChildItem -Path $source -File -Filter '*.mp4' |
      Group-Object { ($_.BaseName -replace'(\d+_[^_]+).*', '$1') } |
      ForEach-Object {
        $index, $year, $month, $day = $_.Name -split '[-_]'
        $targetFolder = Join-Path -Path $destination -ChildPath ('{0}\{1:00}\{2:00}\{3:00}' -f $year, [int]$month, [int]$day, [int]$index)
        # create this folder if it does not already exist
        if (!(Test-Path -Path $targetFolder -PathType Container)) {
            $null = New-Item -Path $targetFolder -ItemType Directory
        }
        $_.Group | Move-Item -Destination $targetFolder
    }
    

    结果:

    D:\TEST\2017
    \---07
        \---16
            \---04
                    4_2017-07-16_01-22-52.mp4
                    4_2017-07-16_01-23-50.mp4
                    4_2017-07-16_01-24-54.mp4
                    4_2017-07-16_01-26-21.mp4
    

    【讨论】:

      猜你喜欢
      • 2017-05-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-29
      • 1970-01-01
      • 2012-06-03
      • 1970-01-01
      • 2021-05-29
      相关资源
      最近更新 更多