vb.net - 从文件夹中随机选择 1000 个文件

发布于 2025-01-11 16:39:31 字数 469 浏览 0 评论 0原文

我有一个文件夹,里面有很多文件。大约在 1000 到 15000 之间。

我需要从此文件夹中随机挑选 1000 个文件并将其复制到另一个文件夹。 我知道如何通过将文件列表插入到数组中然后随机选择一个来从文件夹中获取单个随机文件,但不知道如何获取多个文件,同时避免选择同一文件两次。

例如,如果我的文件夹中有 1001 个文件,那么获取前几个文件不会有任何问题,但到最后,很可能会选择一个已经复制过来的文件,并且需要多次尝试才能找到例如最后一个文件只是运气好...

我的第一个想法是将文件数除以 1000。例如 1500/1000 = 1.5。然后创建一个 0 到 1.5 之间的随机整数。这将返回 1。然后在第 n 个图像和第 n 个图像 * 1.5 之间执行下一个随机数。

如果文件夹有 15000 个文件,它会在 1 到 15 之间随机选择第一个文件,然后在 6 到 30 之间,依此类推。

但必须有一个更智能的解决方案。

感谢任何帮助

I have a folder that has a many files in it. somewhat between 1000 and 15000.

I would need to randomly pick 1000 files from this folder and copy it over to another folder.
I know how to get a single random file from a folder by inserting the list of files in an array and then selecting one randomly, but dont know how to get many while avoiding to select the same file twice.

If for example I have 1001 file in my folder, it will have no trouble getting the fist few, but then towards the end, it is very likely to pick a file that has already been copied over and it would take many tries to find for example the last file just by luck...

my first idea was to divide the number of files by 1000. so for example 1500/1000 = 1.5. and then create a random integer between 0 and 1.5. this would return 1. then do the next rand between nth image and nth image * 1.5.

if the folder hast 15000 files, it would pick the first file randomly between 1 and 15, and then between 6 and 30 and so on..

but there must be a smarter solution for this..

any help appreciated

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

望她远 2025-01-18 16:39:31

您可以随机排序它们:

Dim allFiles = Directory.EnumerateFiles("path")
Dim rnd As New Random()
Dim random1000 = From f In allfiles
                 Order By rnd.Next()
                 Select f
                 Take 1000
Dim list = random1000.ToList()

这是使用 System.Linq

You can order them randomly:

Dim allFiles = Directory.EnumerateFiles("path")
Dim rnd As New Random()
Dim random1000 = From f In allfiles
                 Order By rnd.Next()
                 Select f
                 Take 1000
Dim list = random1000.ToList()

This is using System.Linq

独留℉清风醉 2025-01-18 16:39:31

使用文件列表的索引

Private Shared prng As New Random

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim ListOfiles() As String = IO.Directory.GetFiles(BasePath)
    Dim indices As IEnumerable(Of Integer)
    indices = Enumerable.Range(0, ListOfiles.Length).OrderBy(Function() prng.Next).Take(1000)

    For Each idx As Integer In indices
        Dim fn As String = ListOfiles(idx)
        Stop
    Next

End Sub

Using indices into the list of files

Private Shared prng As New Random

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

    Dim ListOfiles() As String = IO.Directory.GetFiles(BasePath)
    Dim indices As IEnumerable(Of Integer)
    indices = Enumerable.Range(0, ListOfiles.Length).OrderBy(Function() prng.Next).Take(1000)

    For Each idx As Integer In indices
        Dim fn As String = ListOfiles(idx)
        Stop
    Next

End Sub
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文