【问题标题】:Anyway to split files to diff folder?无论如何要将文件拆分到 diff 文件夹?
【发布时间】:2020-02-20 10:04:26
【问题描述】:

我在一个文件夹名称中有超过 100,000 个文件:A,都是图片文件,要么是 jpeg,要么是具有随机文件名的 jpg 扩展名,例如 abc.jpg、xcc.jpg、bbb.jpeg

有没有办法将所有 100k 文件移动到以下结构的文件夹中:

图片顺序无所谓,B下每个文件夹5000张图片即可

Folder B -> Folder 1 (5000 of the jpg files)
            Folder 2 (next 5000 of the jpg files) 
            .
            Folder n (last 5000 remaining of files)

批处理文件或 powershell 并不重要,只要在 windows 或 cmd 中运行,我就是想不出任何方法可以将文件溢出到每个文件夹 5000 个。

任何帮助表示赞赏...谢谢

【问题讨论】:

标签: powershell batch-file cmd


【解决方案1】:

对于您的方案,一种简单的方法是使用 Select-Object 创建文件批次,并使用计数器变量 $i 创建与批次编号对应的文件夹名称。

$i = 1
while (($files = Get-ChildItem *.jpg, *.jpeg).Count -gt 0) 
{
    $folderPath = ".\Folder $i"
    New-Item -Type Directory $folderPath
    $files | Select-Object -First 5000 | Move-Item -Destination $folderPath
    $i += 1
}

请参阅@Matthew 在 cmets 中发布的question,了解解决此类问题的几种替代方法。

注意:我假设您还没有名为“文件夹 1”、“文件夹 2”等的子文件夹...

【讨论】:

  • 感谢您的测试和只是一个学习者,您的移动文件运行得更快
  • @StevenPoon 我对我的代码和 Manuel Batsching 的代码进行了基准测试。事实证明我的代码更快。查看我的更新。
【解决方案2】:

下面的代码假设有文件夹folder_a 和folder_b。您的图像在folder_a 中,代码会将这些图像移动到folder_b\folder_1\...、folder_b\folder_2\...、folder_b\folder_3\... 等。根据您的实际需要更改这些目录名称。在folder_a 中运行它,它将完成工作。

$split_at = 5000
# First, create the directories to store the images
$image_count = Get-ChildItem -File | Measure-Object | Select-Object -ExpandProperty Count
1..[math]::Ceiling($image_count/$split_at) | %{New-Item -Name "folder_$($_)" -ItemType Directory -Path "..\folder_b"}

# Then move the images in batch to the destination directories
$folder_n = 1
while(Test-Path .\*) {
    Get-ChildItem | select -First $split_at | Move-Item -Destination "..\folder_b\folder_$($folder_n)" -Force
    $folder_n++
}

基准测试

我对我的代码和 Manuel Batsching 的代码进行了基准测试。事实证明我的代码更快。下面是基准测试代码和结果。在这里,我每轮创建 2999 个图像文件,并将它们从 folder_a 移动到 folder_b(在文件系统中处于同一级别)。

Remove-Item -Path ..\folder_b\* -Force -Recurse
1..2999 | %{New-Item -Name "item $_.jpg"}

Measure-Command {
    $split_at = 43
    # First, create the directories to store the images
    $image_count = Get-ChildItem -File | Measure-Object | Select-Object -ExpandProperty Count
    1..[math]::Ceiling($image_count/$split_at) | %{New-Item -Name "folder_$($_)" -ItemType Directory -Path "..\folder_b"}

    # Then move the images in batch to the destination directories
    $folder_n = 1
    while(Test-Path .\*) {
        Get-ChildItem | select -First $split_at | Move-Item -Destination "..\folder_b\folder_$($folder_n)" -Force
        $folder_n++
    }
}

Remove-Item -Path ..\folder_b\* -Force -Recurse
1..2999 | %{New-Item -Name "item $_.jpg"}

Measure-Command {
    $i = 1
    while (($files = Get-ChildItem *.jpg, *.jpeg).Count -gt 0) 
    {
        $folderPath = "..\folder_b\folder_$($i)"
        New-Item -Type Directory $folderPath
        $files | Select-Object -First 43 | Move-Item -Destination $folderPath
        $i += 1
    }
}

结果(我的代码):

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 7
Milliseconds      : 632
Ticks             : 76323557
TotalDays         : 8.83374502314815E-05
TotalHours        : 0.00212009880555556
TotalMinutes      : 0.127205928333333
TotalSeconds      : 7.6323557
TotalMilliseconds : 7632.3557

结果(Manuel Batsching):

Days              : 0
Hours             : 0
Minutes           : 0
Seconds           : 13
Milliseconds      : 961
Ticks             : 139616227
TotalDays         : 0.000161592855324074
TotalHours        : 0.00387822852777778
TotalMinutes      : 0.232693711666667
TotalSeconds      : 13.9616227
TotalMilliseconds : 13961.6227

【讨论】:

  • 您好,现在更改文件夹名称并运行它,认为移动所有文件需要一些时间。谢谢
  • 不幸的是,对于最后一个文件夹,它没有创建文件夹,而是只是移动文件并将文件覆盖为文件夹名称。例如:最后一个文件夹 31,未创建文件夹,但文件将继续移动并命名为文件夹 31,而不是其余文件
  • 哦,我很遗憾听到这个消息。你有备份吗?稍后我会检查代码。我昨天测试了少量文件,它看起来还可以。
  • 我确实在代码中发现了一个错误,我已经更正了它。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-15
  • 2021-11-08
  • 2018-05-29
  • 2018-03-19
  • 2012-06-26
  • 2021-04-09
相关资源
最近更新 更多