我希望用隨機字母數字名稱重命名一堆圖像(全部帶有 .jpg 擴展名)。範例是08kDRH1s.jpg
or 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