从文件夹中选择随机文件并将其移动到另一个文件夹的Windows批处理脚本

10
我需要一个批处理脚本,随机选择一个文件夹中的X个文件,并将它们移动到另一个文件夹中。我该如何编写能够实现此功能的Windows批处理脚本?

1
这个必须用批处理吗?用PowerShell实现会简单得多。 - RB.
1
我不知道如何使用PowerShell,但我可以尝试一下。请帮帮我。 - techdaemon
5个回答

17

我假设你的 X 已经事先知道,用变量 $x 表示在以下代码中。

既然你不反对使用 PowerShell 解决方案:

Get-ChildItem SomeFolder | Get-Random -Count $x | Move-Item -Destination SomeOtherFolder

或者更短:

gci somefolder | random -c $x | mi -dest someotherfolder

1
我在 PowerShell 控制台中输入了 Get-ChildItem C:\temp\source | Get-Random -Count 100 | Move-Item -Destination C:\temp\output,但没有任何反应。 - techdaemon
@Joey,我在PowerShell中尝试了这个,因为我想编写一个脚本来随机更改我的登录屏幕。我的源目录在我的用户目录中有几个文件夹的深度,但出于某种原因,当我尝试执行Move-Item命令时,它抛出了一个错误。它说:“无法找到路径'C:\ Users <username> \ <random desktop.png>',因为它不存在。”基本上它在我的源目录中找到了一个随机文件,但然后它搞砸了路径,指向我的用户目录顶层中不存在的文件。你有什么想法吗? - James Martineau
@rironin,我编辑了Joey的原始脚本以修复错误。在从文件夹获取项目时,我们需要先获取文件的FullName,然后再将其传递给Get-Random函数。 - Oleg D.

3
以下批处理代码可以实现此功能。请注意,您需要使用以下命令行启动cmd:
cmd /v:on

启用延迟环境变量扩展。还要注意,它将从0到32767中随机选择文件的数量 - 您可能需要修改此部分以适应您的要求!

@ECHO OFF
SET SrcCount=0
SET SrcMax=%RANDOM%
FOR %F IN (C:\temp\source\*.*) DO IF !SrcCount! LSS %SrcMax% (
      SET /A SrcCount += 1
      ECHO !SrcCount! COPY %F C:\temp\output
      COPY %F C:\temp\output
      )

这是一个批处理文件。你为什么要将它复制到 PowerShell 控制台并期望它能正常工作? - kurumi
@techdaemon 你想要能够指定文件数量,还是指定随机文件数量的范围? - RB.
@RB 我想要能够指定文件数量。 - techdaemon
在这种情况下,只需将SrcMax设置为您想要的值即可。例如,SET SrcMax=25以复制25个文件。 - RB.
仍然不起作用。它给了我许多“系统找不到指定的文件”错误。但是文件名正在显示。我不确定原因是否是文件名中的空格和特殊字符。 - techdaemon
显示剩余8条评论

2

这是一个CMD代码,可以输出随机文件名(根据您的需求进行自定义):

@echo off & setlocal
set "workDir=C:\source\folder"
::Read the %random%, two times is'nt a mistake! Why? Ask Bill.
::In fact at the first time %random% is nearly the same.
@set /a "rdm=%random%"
set /a "rdm=%random%"
::Push to your path.
pushd "%workDir%"
::Count all files in your path. (dir with /b shows only the filenames)
set /a "counter=0"
for /f "delims=" %%i in ('dir /b ^|find "."') do call :sub1
::This function gives a value from 1 to upper bound of files
set /a "rdNum=(%rdm%*%counter%/32767)+1"
::Start a random file
set /a "counter=0"
for /f "delims=" %%i in ('dir /b ^|find "."') do set "fileName=%%i" &call :sub2
::Pop back from your path.
popd "%workDir%"
goto :eof
:: end of main
:: start of sub1
:sub1
::For each found file set counter + 1.
set /a "counter+=1"
goto :eof
:: end of sub1
:: start of sub2
:sub2
::1st: count again,
::2nd: if counted number equals random number then start the file.
set /a "counter+=1"
if %counter%==%rdNum% (
:: OUTPUT ALERT BOX with FILENAME
MSG * "%fileName%"
)
goto :eof
:: end of sub2

LOL 问比尔 :D - thepirat000

1
@echo off
setlocal EnableDelayedExpansion
cd \particular\folder
set n=0
for %%f in (*.*) do (
   set /A n+=1
   set "file[!n!]=%%f"
)
set /A "rand=(n*%random%)/32768+1"
copy "!file[%rand%]!" \different\folder

需要创建一个批处理文件,从文件夹中选择一个随机文件并复制到另一个文件夹。


请查看此链接(https://ss64.com/nt/syntax-random.html)以了解“%random%”的解释。 - Dorian Grv

0
一个示例的 PowerShell 代码,将 1000 个随机文件从 C:\Test\A 移动到 C:\Test\B
$d = gci "C:\Test\A" | resolve-path  |  get-random -count 1000

按下 Enter 键,然后执行以下代码

Move-Item $d  -destination "C:\Test\B"

不要忘记在文件夹路径的前后添加"标记。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接