2

我有2个文件夹:

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

Folder 1有一些JPG图像。Folder 2有档案1.jpg

我想使用批处理文件从中选择一个随机图像,Folder 1然后复制到Folder 2名称中1.jpg(文件1.jpg已存在于 中Folder 2)。

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

4

2 回答 2

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
于 2012-11-22T19:38:39.523 回答
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
)
于 2012-11-22T22:57:37.630 回答