Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/batch-file/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Windows 批处理文件,用于在文件前添加随机数(3位)和删除随机数_Windows_Batch File_Random_Command Line_Batch Rename - Fatal编程技术网

Windows 批处理文件,用于在文件前添加随机数(3位)和删除随机数

Windows 批处理文件,用于在文件前添加随机数(3位)和删除随机数,windows,batch-file,random,command-line,batch-rename,Windows,Batch File,Random,Command Line,Batch Rename,我对两个批处理文件感兴趣:一个生成随机数(3位数)并将其放在文件夹中每个MP3文件的前面,另一个用于删除这些随机数 我的第一批尝试: @echo off setlocal EnableDelayedExpansion for %%i in (*.mp3) do ( ren "%%i" "!RANDOM!%%i" ) endlocal 问题是:数字最多有5位 我在第二批中的尝试: @echo off setlocal enableextensions enabledelayedexpansi

我对两个批处理文件感兴趣:一个生成随机数(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个字符。

  • 要得到一个有3个位置的随机数,你可以用 一千
  • 要获得100以下数字的前导零,您可以添加1000,并且只能添加 使用数字的最后3个位置
  • 为了简化数字的拆分,我会使用一个分隔符,如
    \uuu

出于测试目的,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

也许您会对我不久前编写的这两个批处理文件感兴趣:

扰码歌曲。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
@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
解读歌曲。蝙蝠

@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
@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位数…

Cool。你在哪里尝试代码?@thomas不要在评论中发布代码。我看到的是您正在将
setlocal
for
命令放在一起。谢谢您的代码!但是,对于与软件一起使用,文件名应不带空格。(至少对于某些软件来说)我没有成功地为它调整第二个代码。在第一个代码中,在
ren“%%a”“%%s-%%a”
命令中消除破折号和文件名之间的空格。2.在第二个代码中,在
tokens=1*
之后添加
delims=-
,并通过
ren“%%a%%b”“%%b”命令中的破折号更改空格。我可以请你投票并选择这个答案吗?完成。然而,由于我的声望不到15,我的支持票不会改变分数。