我对2个批处理文件感兴趣:一个生成随机数(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个字符。
答案 0 :(得分:0)
_
等分隔符 @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, 输出正常时将其删除。
@Echo off
Set "Delims=_"
For /F "tokens=1,* Delims=%Delims%" %%A in (
'Dir /B "???%Delims%*.mp3" '
) do Echo ren "%%A%Delims%%%B" "%%B"
Pause
答案 1 :(得分:0)
也许您可能会对我前一段时间写过的这两个批处理文件感兴趣:
争夺songs.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位...... / p>