簡體   English   中英

windows 批處理文件腳本,用於從文件夾中選擇隨機文件並將它們移動到另一個文件夾

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

我需要一個批處理腳本來隨機 select 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:\temp\source\*.*) DO IF !SrcCount! LSS %SrcMax% (
      SET /A SrcCount += 1
      ECHO !SrcCount! COPY %F C:\temp\output
      COPY %F C:\temp\output
      )

這是一個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

需要創建批處理文件,從文件夾中選擇一個隨機文件並復制到另一個文件夾

1000 個隨機文件從C:\Test\A移動到C:\Test\B的示例Powershell 代碼

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

回車鍵然后執行下面的代碼

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

不要忘記文件夾路徑前后添加"標記

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM