简体   繁体   English

windows 批处理文件脚本,用于从文件夹中选择随机文件并将它们移动到另一个文件夹

[英]windows batch file script to pick random files from a folder and move them to another folder

I need a batch script to randomly select X number of files in a folder and move them to another folder.我需要一个批处理脚本来随机 select X 个文件夹中的文件并将它们移动到另一个文件夹。 How do I write a windows batch script that can do this?如何编写 windows 批处理脚本来执行此操作?

(I'm assuming that your X is known beforehand – represented by the variable $x in the following code). (我假设您的X事先已知 - 由以下代码中的变量$x表示)。

Since you weren't adverse to a PowerShell solution: 由于您不会对PowerShell解决方案产生负面影响:

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

or shorter: 或更短:

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

The following Batch code will do it. 以下批处理代码将执行此操作。 Note that you will need to launch cmd using the following command line: 请注意,您需要使用以下命令行启动cmd:

cmd /v:on

to enable delayed environment variable expansion. 启用延迟环境变量扩展。 Note also that it will pick a random number of files from 0 to 32767 - you will probably want to modify this part to fit your requirements! 另请注意,它将从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
      )

here is a CMD code, which outputs the random file name (customize it to your needs): 这是一个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
@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

from Need to create a batch file to select one random file from a folder and copy to another folder 需要创建批处理文件,从文件夹中选择一个随机文件并复制到另一个文件夹

A sample Powershell code that Moves 1000 Random files From C:\Test\A To C:\Test\B1000 个随机文件从C:\Test\A移动到C:\Test\B的示例Powershell 代码

$d = gci "C:\Test\A" | resolve-path  |  get-random -count 1000

Press Enter key and then execute below code回车键然后执行下面的代码

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

Don't forget to add " mark before and after the path of the folders不要忘记文件夹路径前后添加"标记

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Windows批处理文件-在一个文件夹中选择(最多)四个随机文件 - Windows batch file - Pick (up to) four random files in a folder 如何在一个文件夹中播放随机视频文件并移至下一个文件夹,并使用批处理脚本执行相同操作? - How to play random video files in a folder and move on to the next folder and do the same with batch script? 批处理文件从不同的子文件夹中随机选择一个具有相同文件名的文件到一个文件夹 - Batch file randomly pick files that are same file names from different sub folder to one folder 快速轻松地从文件夹中选择随机文件 - Quickly and lightly pick a random file from a folder 随机将文件从一个文件夹移动到另一个文件夹? - randomly move files from a folder to another folder? 具有随机名称的批处理移动文件夹 - Batch move folder with random name 从某个文件夹启动随机批处理文件 - Starting random batch file from a certain folder 如何从文件夹树中快速选择随机文件? - How to quickly pick a random file from a folder-tree? 从bash脚本中的文件夹执行随机文件 - Execute random file from a folder in a bash script 使用Applescript从文件夹中打开随机脚本文件 - Open random script files from folder using Applescript
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM