如何改写文件名的前三个字母
- 使用设置字母表:ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789 来随机化字符
- 定位文件夹 *.ext 中特定扩展名的所有文件
- 偶然重复 3 个字符并不重要(已有 46,656 个变量)
- 在Windows环境中运行的批处理脚本解决方案。
- 最好使用简单的脚本来减少运行时间。
建议的代码和分解。注意:我仍然不知道如何编码。
原来的:
032_name.ext 039_name.ext 0D8_name.ext 333_other.txt
写在前三个字符的上方
XXX-name.ext
后
D7K_name.ext L2V_name.ext 720_name.ext 333_other.txt
制作批处理文件(SetRename.bat 或 SetRename.cmd),然后运行如下命令:
SetRename %r *.ext /or/ SetRename %r3%name% *.ext /or/ SetRename
代码 1 改编自 DavidPostill,完整解决方案的示例
@echo off setlocal enabledelayedexpansion rem initialise counter set "alphabet=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9" rem process jpg and png files for /f "usebackq tokens=*" %%i in (`dir /b *.ext *.ex2`) do ( rem split into name and extension set _name=[reduce 3 characters]]%%~ni set _ext=%%~xi rem do the rename ren "%%i" "!y!-!_name!-!_ext!" increment counter set /alphabet "%Random%" ) endlocal
代码 2 改编自 3 个 SU 帖子
@echo off setlocal enableextensions enabledelayedexpansion set "alphabet=A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 0 1 2 3 4 5 6 7 8 9" for %%a in (%alphabet%) do ( set "a.!name!=%%a" set o=-%3 [reduce by 3] ) set "y=" for /l %%a in (1 1 3) do ( set /a "r=!random!" for %%b do set "y=!-%3!!r!!name!" ) echo(%y% endlocal
目前的研究:
答案1
如何用随机字符重写一组文件名的前三个字母?
使用以下批处理文件(SetRename.cmd):
@echo off
setlocal enableDelayedExpansion
set "_chars=ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
for %%f in (%*) do (
rem split into name and extension
set "_name=%%~nf"
rem remove first 3 characters from _name
set "_name=!_name:~3!"
set "_ext=%%~xf"
rem create a 3 character random prefix
set "_prefix="
for /l %%n in (1 1 3) do (
set /a i=!random!%%36
for %%i in (!i!) do (
set "_prefix=!_prefix!!_chars:~%%i,1!" (
)
)
echo ren "%%f" "!_prefix!!_name!!_ext!"
)
endlocal
笔记:
- 当你对命令
echo
满意时删除ren
使用示例:
> dir /b
032_name.ext
039_name.ext
0D8_name.ext
333_other.txt
SetRename.cmd
> SetRename *.ext
> dir /b
333_other.txt
C42_name.ext
FZW_name.ext
MYQ_name.ext
SetRename.cmd
> SetRename *.ext
> dir /b
333_other.txt
A6O_name.ext
A8H_name.ext
H5P_name.ext
SetRename.cmd