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



我需要一个批处理脚本来随机选择一个文件夹中的X个文件,并将它们移动到另一个文件夹。如何编写一个可以执行此操作的windows批处理脚本?

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

因为你并不反对PowerShell解决方案:

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

或更短:

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

以下批处理代码将完成此操作。请注意,您需要使用以下命令行启动cmd:

cmd /v:on

以实现延迟的环境变量扩展。还要注意的是,它会从0到32767中随机选择一些文件——您可能需要修改这个部分以满足您的要求!

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

这里有一个CMD代码,它输出随机文件名(根据您的需要进行自定义(:

@echo off & setlocal
set "workDir=C:sourcefolder"
::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 particularfolder
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%]!" differentfolder

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

一个示例Powershell代码,它将1000随机文件从C:\Test\A移动到C:\Test\B

$d = gci "C:TestA" | resolve-path  |  get-random -count 1000

输入键,然后执行以下代码

Move-Item $d  -destination "C:TestB"

不要忘记添加">标记在文件夹

的路径之前和之后

相关内容

  • 没有找到相关文章

最新更新