如何使用批处理文件将随机图像从文件夹复制到另一个文件夹?

时间:2012-11-22 19:04:19

标签: image random batch-file copy overwrite

我有2个文件夹:

D:\Folder 1\
D:\Folder 2\

Folder 1有一些JPG图片。 Folder 2有一个文件1.jpg

我希望使用批处理文件从Folder 1中选择随机图片,然后复制到名称为Folder 2的{​​{1}}(文件1.jpg已存在于1.jpg

我该怎么办?非常感谢你。

2 个答案:

答案 0 :(得分:1)

@Echo OFF

REM By Elektro H@cker

:: Generate random numbers before filenames
FOR %%# in ("D:\Folder 1\*") Do (CALL Echo %%RANDOM:~5,1%%%%RANDOM:~4,1%%%%RANDOM:~3,1%%%%RANDOM:~2,1%%%%RANDOM:~1,1%%;%%# >> "%TEMP%\TempFile.txt")

:: Sorts the generated random numbers and choose a random file.
For /F "TOKENS=*" %%# in ('Type "%TEMP%\TempFile.txt" ^| SORT ^| MORE +%RANDOM:~1,1%') DO (

:: Copy the file to a folder
    For /F "TOKENS=2 Delims=;" %%@ in ('Echo %%#') do (
        Echo  [+] File choosed: "%%~f@"
        Copy /Y "%%~f@" "D:\folder 2\1.jpg" 1>NUL
        GOTO:EXIT
    )
)

:EXIT
:: Delete the temp file
DEL /Q "%TEMP%\Tempfile.txt"
Pause&Exit

答案 1 :(得分:1)

@echo off
setlocal

:: Initialization
set "ext=jpg"
set "source=folder 1"
set "target=folder 2"

:: Get count of images and select random image
for /f %%N in ('dir /b "%source%\*.%ext%"^|find /c /v ""') do (
  set /a "imageNum=%random% %% %%N"
)

:: Copy the image
if %imageNum% gtr 0 (set "skip=skip=%imageNum%") else set "skip="
for /f "%skip% eol=: delims=" %%F in ('dir /b "%source%\*.%ext%"') do (
  echo copy /y "%source%\%%F" "%target%\1.%ext%"
  exit /b
)