提问人:user1719210 提问时间:11/2/2023 最后编辑:user1719210 更新时间:11/3/2023 访问量:47
如何随机播放jpg图像文件并使用batch / Windows按顺序重命名
How to shuffle jpg image files and rename sequentially with batch / Windows
问:
我有一个 520 jpg 文件的图像序列,名为:
001.jpg
002.jpg
.
.
.
520.jpg
我想使用 Windows 批处理脚本随机播放图像(保持相同的序列文件名)。 这可能吗?
答:
0赞
user1719210
11/3/2023
#1
我发现这要洗牌:
@ECHO OFF
REM Randomly renames every file in a directory.
SETLOCAL EnableExtensions EnableDelayedExpansion
REM 0 = Rename the file randomly.
REM 1 = Prepend the existing file name with randomly generated string.
SET PrependOnly=0
REM 1 = Undo changes according to the translation file.
REM This will only work if the file "__Translation.txt" is in the same folder.
REM If you delete the translaction file, you will not be able to undo the changes!
SET Undo=0
REM --------------------------------------------------------------------------
REM Do not modify anything below this line unless you know what you are doing.
REM --------------------------------------------------------------------------
SET TranslationFile=__Translation.txt
IF NOT {%Undo%}=={1} (
REM Rename files
ECHO You are about to randomly rename every file in the following folder:
ECHO %~dp0
ECHO.
ECHO A file named %TranslationFile% will be created which allows you to undo this.
ECHO Warning: If %TranslationFile% is lost/deleted, this action cannot be undone.
ECHO Type "OK" to continue.
SET /P Confirm=
IF /I NOT {!Confirm!}=={OK} (
ECHO.
ECHO Aborting.
GOTO :EOF
)
ECHO Original Name/Random Name > %TranslationFile%
ECHO ------------------------- >> %TranslationFile%
FOR /F "tokens=*" %%A IN ('DIR /A:-D /B') DO (
IF NOT %%A==%~nx0 (
IF NOT %%A==%TranslationFile% (
SET Use=%%~xA
IF {%PrependOnly%}=={1} SET Use=_%%A
SET NewName=!RANDOM!-!RANDOM!-!RANDOM!!Use!
ECHO %%A/!NewName!>> %TranslationFile%
RENAME "%%A" "!NewName!"
)
)
)
) ELSE (
ECHO Undo mode.
IF NOT EXIST %TranslationFile% (
ECHO Missing translation file: %TranslationFile%
PAUSE
GOTO :EOF
)
FOR /F "skip=2 tokens=1,2 delims=/" %%A IN (%TranslationFile%) DO RENAME "%%B" "%%A"
DEL /F /Q %TranslationFile%
)
这样就可以使序列:
@echo off
setlocal ENABLEDELAYEDEXPANSION
set /a a=0
for /f "delims=" %%I in ('dir /b *.jpg') do (
set "idx=00!a!"
ren "%%~I" "!idx:~-3!%%~xI"
set /a a += 1
)
希望这对:)有所帮助
0赞
Aacini
11/3/2023
#2
这不是这个网站的工作方式。您应该提供您有问题的代码,我们会帮助您解决问题......
但是,我现在有一些空闲时间,所以我为你写了这个:
@echo off
setlocal EnableDelayedExpansion
rem Load file names in both "in" and "out" arrays
set "i=0"
for %%f in (*.jpg) do (
set /A i+=1
set "in[!i!]=%%~Nf"
set "out[!i!]=%%~Nf"
)
rem Shuffle "in" array and rename the files in opposite "out" order
for /L %%i in (%i%,-1,1) do (
set /A "pos=!random! %% %%i + 1"
for %%p in (!pos!) do (
ren "!in[%%p]!.jpg" "!out[%%i]!.new"
set "in[%%p]=!in[%%i]!"
)
)
ren *.new *.jpg
评论
0赞
Aacini
11/15/2023
你看到我的答案了吗?
上一个:验证序列内容是否连续
下一个:如何透视数据帧?
评论