使用 Windows bat 脚本将随机文件从随机子文件夹复制到目标文件夹

使用 Windows bat 脚本将随机文件从随机子文件夹复制到目标文件夹

我试图随机挑选 100 首歌曲复制到我的 mp3 播放器中,这样我每天早上就可以听一些新歌曲,而不必自己拖放随机文件(我无法一次将我的整个库放到播放器上)。

我正在使用 bat 脚本来执行此操作,但遇到了一些问题。下面这个脚本可以工作,但它会复制随机文件夹中的所有文件,而不是随机文件夹中的随机文件,然后再移动到下一个文件夹。

我对此完全是新手,所以所有内容都是从这里的其他解决方案中筛选出来并拼凑在一起的。

echo off
:randomstart
setlocal EnableDelayedExpansion
rem Enter into the directory that contain the folders
pushd D:\test1\
rem Create an array with all folders
set i=0
for /D %%a in (*) do (
   set /A i+=1
   set folder[!i!]=%%a
)
rem Randomly select one folder
set /A index=(%random%*i)/32768 + 1
rem Copy the desired file
copy "!folder[%index%]!\" "D:\output2\" /Y
rem And return to original directory
popd
ping -n 2 localhost >nul
goto:randomstart

我也尝试过添加一个 for 循环来从 1 计数到 100,但我完全搞不懂。有谁能救救这个白痴吗?

我尝试在服务器故障上询问这个问题,并被告知这是一个更好的询问地点。

答案1

尝试这个(设置文件夹名称和要复制的文件数量):

@echo off&setlocal enabledelayedexpansion
set "musicroot=test"
set "playfolder=output"
set /a filecount=20

pushd "%musicroot%"
for /r %%i in (*.mp3) do set /a files+=1& set "$!files!=%%~i"
popd
pushd "%playfolder%"
:randomloop
set /a rd=%random%%%files+1
set "mp3=!$%rd%!"
if not defined mp3 goto :randomloop
set "$%rd%="
for %%i in ("%mp3%") do if exist "%%~nxi" echo "%%~nxi" already exist in %playfolder%.& goto:randomloop
copy "%mp3%"
set /a filecount-=1
if %filecount% gtr 0 goto:randomloop
popd

答案2

这是一个 powershell 解决方案

#edit this for your settings
$sourceFolder = 'E:\test'
$destFolder = 'E:\Test2'
$filesToCopy = 100
$searchFilter = '*.mp3'



function Copy-RandomFiles
{
param (
    [Parameter(Mandatory=$true, Position=0)]
    [string]$SourceDirectory,

    [Parameter(Mandatory=$true, Position=1)]
    [string]$DestinationDirectory,

    [int]$FilesToCopy = 100,

    [string]$SearchFilter = '*.*'
    )

    $rand = New-Object System.Random

    $files = [System.IO.Directory]::GetFiles($SourceDirectory, $SearchFilter, [System.IO.SearchOption]::AllDirectories)

    $usedIndexes = @{}
    $filteredList = New-Object System.Collections.ArrayList

    #build list of random indexes
    for([int]$i = 0; ($i -lt $FilesToCopy) -and ($i -lt $files.Length); $i++)
    {
        $index = $rand.Next(0, $files.Length)
        #loop till we find an available index
        while($usedIndexes.ContainsKey($index))
        {
            $index = $rand.Next(0, $files.Length)
        }

        $usedIndexes.Add($index, $null)
        $dump = $filteredList.Add($files[$index]) #dump is so it does not display a count
    }

    if($filteredList.Count -gt 0)
    {
        Copy-Item -Path $filteredList.ToArray() -Destination $DestinationDirectory
    }

    $count = $filteredList.Count

    Write-Host "$count file(s) copied"
}

Get-ChildItem $destFolder | Remove-Item
Copy-RandomFiles $sourceFolder $destFolder -FilesToCopy $filesToCopy -SearchFilter $searchFilter

Write-Host "Press any key to continue . . ."
$x = $host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")

将其保存为带有扩展名的文本文件,保存在硬盘上的某个位置.ps1。然后在桌面上创建一个快捷方式链接,并以此作为路径

%windir%\system32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Unrestricted -file C:\Path\To\CopyFileScript.ps1

这将删除文件夹中的所有内容,并使用过滤器从其子文件夹中$destFolder复制$filesToCopy文件$sourceFolder$searchFilter

相关内容