【问题标题】:windows batch file script to pick random files from a folder and move them to another folderwindows批处理文件脚本从文件夹中选择随机文件并将它们移动到另一个文件夹
【发布时间】:2011-07-29 23:18:05
【问题描述】:

我需要一个批处理脚本来随机选择一个文件夹中的 X 个文件并将它们移动到另一个文件夹。如何编写可以做到这一点的 Windows 批处理脚本?

【问题讨论】:

  • 这必须是批处理吗?在 PowerShell 中实现会简单得多。
  • 我不知道如何使用Powershell。但我可以试试。请帮帮我。

标签: random file batch-file


【解决方案1】:

(我假设您的 X 是事先已知的——由以下代码中的变量 $x 表示)。

既然您不反对 PowerShell 解决方案:

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

或更短:

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

【讨论】:

  • 我在 powershell 控制台中输入了Get-ChildItem C:\temp\source | Get-Random -Count 100 | Move-Item -Destination C:\temp\output,但什么也没发生
  • @Joey,我在 PowerShell 中尝试过这个,因为我正在尝试获取一个脚本来随机更改我的登录屏幕。我的源目录是我的用户目录深处的几个文件夹,由于某种原因,当我尝试执行时,Move-Item 命令引发了错误。它说Cannot find path 'C:\Users\<username>\<random desktop.png>' because it does not exist. 基本上它在我的源目录中找到了一个随机文件,但随后它搞砸了路径,指向我的用户目录顶层不存在的文件。知道为什么吗?
  • @rironin,我编辑了 Joey 的原始脚本来修复错误。从文件夹中获取项目时,我们需要获取文件的全名,然后将其传递给 Get-Random
【解决方案2】:

以下批处理代码将执行此操作。请注意,您需要使用以下命令行启动 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 控制台。恐怕它没有用。它给了我红色文本中的错误。
  • 嗨 techdaemon - 我发布的代码是 Batch,它是与 PowerShell 完全不同的语言。我已经更新了批处理代码,它现在应该可以工作了,只要你按照我所说的那样使用“/v:on”。
  • 这是一个批处理文件。为什么将它复制到 powershell 控制台并期望它可以工作?
  • “SrcCount”是我指定要选择的文件数量的地方,不是吗?
  • 不 - 这是一个计数器,所以它知道它复制了多少文件。 SrcMax 是要复制的文件数。我已将其设置为随机数,因为这就是问题所要求的。
【解决方案3】:

这是一个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

【讨论】:

  • 大声问比尔:D
【解决方案4】:
@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

来自Need to create a batch file to select one random file from a folder and copy to another folder

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多