我对两个批处理文件感兴趣:一个生成随机数(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个字符。
发布于 2017-07-17 14:55:50
也许您会对我不久前写的这两个批处理文件感兴趣:
扰码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位数.
发布于 2017-07-17 12:41:54
_这样的分隔符 @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"
Pausehttps://stackoverflow.com/questions/45135663
复制相似问题