我想用随机字母数字名称重命名一堆图像(所有图像的扩展名为 .jpg)。例如或08kDRH1s.jpg
。0FwzUc3.jpg
经过一番搜索,我确实找到了一些批处理脚本,$RANDOM
但这些脚本只使用 0 到 32767 之间的值
一位用户向我介绍了一个可以生成随机 8 个字符的字母数字字符串的批处理脚本。调整这个脚本来重命名文件比直接将名称输出到命令行难吗?
@Echo Off
Setlocal EnableDelayedExpansion
Set _RNDLength=8
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
Set _Str=%_Alphanumeric%987654321
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
Echo Random string is !_RndAlphaNum!
答案1
以下简单脚本将当前文件夹中的所有 .jpg 文件重命名为随机的 8 个字符的字母数字名称,并保留 .jpg 扩展名。请注意,随机名称生成器可能会生成已存在的名称,因此脚本会循环执行,直到成功生成唯一名称。
@echo off
setlocal disableDelayedExpansion
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /b /a-d *.jpg') do call :renameFile "%%F"
exit /b
:renameFile
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
set /a I=!random!%%36
for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)
echo if exist !name!.jpg goto :retry
endlocal & ren %1 %name%.jpg
再多写一点代码,就可以得到一个多功能实用程序,它允许您指定源路径和文件掩码,并且还提供处理子目录的选项。该实用程序始终保留每个文件的扩展名。使用此实用程序时要小心!
重命名文件随机.bat
:: renameFilesRandom.bat [filter] [/s]
@echo off
setlocal disableDelayedExpansion
:: Parse and validate arguments
set "option="
set "filter="
if "%~3" neq "" (
>&2 echo ERROR: Too many arguments
exit /b 1
)
if /i "%~1" equ "/S" (set "option=/S") else if "%~1" neq "" set "filter=%~1"
if /i "%~2" equ "/S" (set "option=/S") else if "%~2" neq "" (
if defined filter (
>&2 echo ERROR: Only one filter allowed
exit /b 1
) else set "filter=%~2"
)
if "%filter:~0,1%" equ "/" (
>&2 echo ERROR: Invalid option %filter%
exit /b 1
)
if not defined filter set "filter=*"
:: Convert a directory filter into a file filter with wildcards
if exist "%filter%\" set "filter=%filter%\*"
:: Determine source if /S option not specified
set "src="
if not defined option for /f "eol=: delims=" %%F in ("%filter%") do set "src=%%~dpF"
:: Rename the specified files
set "chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for /f "eol=: delims=" %%F in ('dir /a-d /b %option% "%filter%"') do call :renameFile "%%F"
exit /b
:renameFile
setlocal
if not defined src set "src=%~dp1"
set "old=%~nx1"
set "ext=%~x1"
setlocal enableDelayedExpansion
:retry
set "name="
for /l %%N in (1 1 8) do (
set /a I=!random!%%36
for %%I in (!I!) do set "name=!name!!chars:~%%I,1!"
)
if exist "!src!!name!!ext!" goto :retry
ren "!src!!old!" "!name!!ext!"
以下是一些示例用法。
重命名当前目录中的所有文件:
renameFilesRandom
重命名当前目录中的所有.jpg文件:
renameFilesRandom *.jpg
重命名 c:\test 文件夹中的所有 .jpg 文件及其所有子文件夹(递归):
renameFilesRandom c:\test\*.jpg /s
答案2
@echo off
REM The length of the random file name.
Set _RNDLength=8
REM Alphanumeric characters from which to choose.
Set _Alphanumeric=ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789
SET _Str=%_Alphanumeric%987654321
REM Define the folder to be searched. This value will be used later
REM to build the full path to the file. This is necessary because we
REM are not using /S with the dir command.
SET folder=C:\Users\patrick.seymour\Pictures
REM Loop through all of the JPEGs in the specified folder.
for /f "tokens=*" %%a in ('dir /b "%folder%\*.jpg"') do (
SET RESULT=
SETLOCAL ENABLEDELAYEDEXPANSION
CALL :GetRandomName
echo Renaming "%folder%\%%a" to "!RESULT!.jpg"
ren "%folder%\%%a" "!RESULT!.jpg"
ENDLOCAL
)
GOTO :EOF
:GetRandomName
SET /A _Len=0
:_LenLoop
IF NOT "%_Str:~18%"=="" SET _Str=%_Str:~9%& SET /A _Len+=9& GOTO :_LenLoop
SET _tmp=%_Str:~9,1%
SET /A _Len=_Len+_tmp
Set _count=0
SET _RndAlphaNum=
:_loop
Set /a _count+=1
SET _RND=%Random%
Set /A _RND=_RND%%%_Len%
SET _RndAlphaNum=!_RndAlphaNum!!_Alphanumeric:~%_RND%,1!
If !_count! lss %_RNDLength% goto _loop
REM Echo Random string is !_RndAlphaNum!
SET RESULT=!_RndAlphaNum!
GOTO :EOF
REM EXIT /B
:EOF