我对两个batch file感兴趣:一个生成随机数字(3位数字),并把它们放在一个文件夹中的每个MP3文件的前面,另一批次用于删除这些随机数字。
我在第一批的尝试:
@echo off setlocal EnableDelayedExpansion for %%i in (*.mp3) do ( ren "%%i" "!RANDOM!%%i" ) endlocal
问题是:数字最多为5位数。
我在第二批的尝试:
@echo off setlocal enableextensions enabledelayedexpansion for %%i in (*.mp3) do (set name=%%i && ren "!name!" "!name:~3!") endlocal
问题是:它从第一个文件中删除了6个字符。
也许你可能对我之前写的这两个批处理文件感兴趣:
争夺歌曲.bat :
@echo off setlocal EnableDelayedExpansion rem Scramble *.mp3 files inserting a random number in their names rem Antonio Perez Ayala rem Create a list of numbers in natural order for all files rem with four digits each and a separation space cd "%~DP0\My Music in 4GB" set i=10000 set "list=" for %%a in (*.mp3) do ( set /A i+=1 set "list=!list!!i:~-4! " ) set /A i-=10000 rem Extract random elements from the list of numbers rem and use they in the new name of the files for /F "delims=" %%a in ('dir /B *.mp3') do ( echo !i!- "%%a" set /A "randElem=!random! %% i * 5, i-=1" for %%r in (!randElem!) do for /F %%s in ("!list:~%%r,4!") do ( setlocal DisableDelayedExpansion ren "%%a" "%%s- %%a" endlocal set "list=!list:%%s =!" ) ) pause
解读songs.bat :
@echo off setlocal DisableDelayedExpansion rem Unscramble the *.mp3 files removing the random number from their names rem Antonio Perez Ayala cd "%~DP0\My Music in 4GB" for /F "tokens=1*" %%a in ('dir /B *.mp3') do ( echo %%a "%%b" ren "%%a %%b" "%%b" ) pause
第一个批处理文件在每首歌曲前插入一个四位数字,因为我的4GB卡可以接受超过1000首歌曲,但是我相当确定您可以修改此代码以将其数量减少到3位数。
_
@Echo off & Setlocal EnableDelayedExpansion Set "Delims=_" for %%i in (*.mp3) do ( Set /A RndNum=1000+!Random! %% 1000 Echo Ren "%%i" "!RndNum:~-3!%Delims%%%i" ) Pause
为了测试目的,ren命令前面加了一个echo,当输出OK时将其删除。
@Echo off Set "Delims=_" For /F "tokens=1,* Delims=%Delims%" %%A in ( 'Dir /B "???%Delims%*.mp3" ' ) do Echo ren "%%A%Delims%%%B" "%%B" Pause