AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / computer / 问题 / 1663317
Accepted
Phil
Phil
Asked: 2021-07-19 08:47:52 +0800 CST2021-07-19 08:47:52 +0800 CST 2021-07-19 08:47:52 +0800 CST

从带有子目录的文件夹中随机复制 50 个音乐文件到另一个文件夹

  • 772

我有一个文件夹结构,其中包含格式中的音乐文件,mp3文件flac 夹结构如下所示:

S:\Music\
     |___\ABBA
     |___\...
     |___\ZZTop

我正在尝试创建一个 Powershell 脚本,该脚本从目录(包括其子目录)中随机选择50 个文件Music并将其放入另一个文件夹中。

我试过这个脚本,但它不起作用。

$d = gci "S:\Music\*.mp3" | resolve-path  |  get-random -count 50
Copy-Item $d  -destination 'S:\Music\RandomFIles'
 

它抛出这个错误:Copy-Item : Cannot bind argument to parameter 'Path' because it is null.

windows powershell
  • 1 1 个回答
  • 98 Views

1 个回答

  • Voted
  1. Best Answer
    Theo
    2021-07-19T11:20:26+08:002021-07-19T11:20:26+08:00

    为什么需要| Resolve-Path |?您需要将开关附加-File到 Get-ChildItem,这样您就不会将 DirectoryInfo 对象与 FileInfo 对象一起检索。

    然后使用参数.FullName上的属性-Path(这是第一个需要字符串数组的位置参数)
    参见Copy-Item

    根据音乐收藏的大小,这可能需要一些时间才能完成

    通过在递归迭代文件的文件夹内使用目标文件夹,您可能会遇到异常: “复制项目:无法用自身覆盖项目 blah.mp3。” 因此为避免这种情况,您需要附加-ErrorAction SilentlyContinue到 Copy-Item 命令。

    下面复制 .MP3 和 .FLAC 文件:

    # loop through the music folders and search for the file patterns you need to copy
    $files = Get-ChildItem -Path 'S:\Music' -File -Include '*.mp3', '*.flac' -Recurse | Get-Random -Count 50
    Copy-Item -Path $files.FullName -Destination 'S:\Music\RandomFiles' -Force -ErrorAction SilentlyContinue
    

    但是,如果您只想复制 .MP3 文件,请注意该参数的-Filter工作速度比-Include但是..

    • -Filter 只允许 ONE 文件名模式,例如 '*.mp3'
    • -Include允许名称模式数组,但只能在路径以\*, 结尾或-Recurse同时使用开关时使用
    # loop through the music folders and search for the single file pattern you need to copy
    $files = Get-ChildItem -Path 'S:\Music' -File -Filter '*.mp3' -Recurse | Get-Random -Count 50
    Copy-Item -Path $files.FullName -Destination 'S:\Music\RandomFiles' -Force -ErrorAction SilentlyContinue
    

    在这两种情况下,请确保目标路径存在。


    编辑

    正如您所发现的,多次运行第一个代码块,每次运行的总数可能少于 50 个。这是因为Get-Random -Count 50可以返回已经在目标文件夹中的文件,并且上面的代码只是简单地复制它们。
    另外,对我来说,不清楚它Get-Random -Count 50本身是否不返回重复项。到目前为止,我还没有看到,但有可能在返回时,50 个项目可能并不总是唯一的。

    为了克服这个问题,下面确保复制的每个文件都没有在目标文件夹中找到,并且它将始终返回 50 个没有重复的文件。

    但是有一个警告:
    一遍又一遍地运行时,如果源文件夹中没有更多尚未复制的文件,您可能最终会出现一个 endess 循环。

    $sourceFolder = 'S:\Music'
    $destination  = 'S:\Music\RandomFiles'  # this folder MUST already exist 
    
    # first create a Hashtable with file already in the destination folder (Names only)
    $existing = @{}
    Get-ChildItem -Path $destination  -File -Filter '*.mp3' | ForEach-Object { $existing[$_.Name] = $true }
    
    # next loop through the music folders and search for the single file pattern you need to copy
    # if the destination folder is NOT in the sourcefolder, you can leave out the Where-Object clause
    $files = Get-ChildItem -Path $sourceFolder -File -Filter '*.mp3' -Recurse | Where-Object{ -not $_.DirectoryName.StartsWith($destination) }
    # make sure you do not exceed the total number of files
    $totalFiles = @($files).Count
    $maxFiles = [math]::Min($totalFiles, 50)
    # start a counted loop
    for ($i = 0; $i -lt $maxFiles; $i++) {
        $rnd = Get-Random -Maximum $totalFiles                  # get a random array index from the complete $files collection
        if ($existing.ContainsKey($files[$rnd].Name)) { $i-- }  # file is already present; decrease the counter
        else {
            Write-Host "Copying file no. $($i + 1)/$maxFiles"
            $files[$rnd] | Copy-Item -Destination $destination  # copy the file
            $existing[$files[$rnd].Name] = $true                # and add its name to the Hashtable
        }
    }
    
    • 1

相关问题

  • Python 的“pass”参数的批处理等价物是什么?

  • 禁用后无法启用 Microsoft Print to PDF

  • 我可以让这个 PowerShell 脚本接受逗号吗?

  • 在 Windows 上与 Docker 守护进程通信

  • 资源管理器侧面板中的桌面外壳快捷方式

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何减少“vmmem”进程的消耗?

    • 11 个回答
  • Marko Smith

    从 Microsoft Stream 下载视频

    • 4 个回答
  • Marko Smith

    Google Chrome DevTools 无法解析 SourceMap:chrome-extension

    • 6 个回答
  • Marko Smith

    Windows 照片查看器因为内存不足而无法运行?

    • 5 个回答
  • Marko Smith

    支持结束后如何激活 WindowsXP?

    • 6 个回答
  • Marko Smith

    远程桌面间歇性冻结

    • 7 个回答
  • Marko Smith

    子网掩码 /32 是什么意思?

    • 6 个回答
  • Marko Smith

    鼠标指针在 Windows 中按下的箭头键上移动?

    • 1 个回答
  • Marko Smith

    VirtualBox 无法以 VERR_NEM_VM_CREATE_FAILED 启动

    • 8 个回答
  • Marko Smith

    应用程序不会出现在 MacBook 的摄像头和麦克风隐私设置中

    • 5 个回答
  • Martin Hope
    Saaru Lindestøkke 为什么使用 Python 的 tar 库时 tar.xz 文件比 macOS tar 小 15 倍? 2021-03-14 09:37:48 +0800 CST
  • Martin Hope
    CiaranWelsh 如何减少“vmmem”进程的消耗? 2020-06-10 02:06:58 +0800 CST
  • Martin Hope
    Jim Windows 10 搜索未加载,显示空白窗口 2020-02-06 03:28:26 +0800 CST
  • Martin Hope
    v15 为什么通过电缆(同轴电缆)的千兆位/秒 Internet 连接不能像光纤一样提供对称速度? 2020-01-25 08:53:31 +0800 CST
  • Martin Hope
    andre_ss6 远程桌面间歇性冻结 2019-09-11 12:56:40 +0800 CST
  • Martin Hope
    Riley Carney 为什么在 URL 后面加一个点会删除登录信息? 2019-08-06 10:59:24 +0800 CST
  • Martin Hope
    zdimension 鼠标指针在 Windows 中按下的箭头键上移动? 2019-08-04 06:39:57 +0800 CST
  • Martin Hope
    jonsca 我所有的 Firefox 附加组件突然被禁用了,我该如何重新启用它们? 2019-05-04 17:58:52 +0800 CST
  • Martin Hope
    MCK 是否可以使用文本创建二维码? 2019-04-02 06:32:14 +0800 CST
  • Martin Hope
    SoniEx2 更改 git init 默认分支名称 2019-04-01 06:16:56 +0800 CST

热门标签

windows-10 linux windows microsoft-excel networking ubuntu worksheet-function bash command-line hard-drive

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve