Windows 下批量修改文件夹及子文件夹下文件的扩展名

方法1.

@echo off
set DestPath=D:\Music_bak
for /r %DestPath% %%i in (.) do (
echo %%i
cd %%i
ren *.mp3 *.m3p
)
pause

rename 命令
RENAME [drive:][path]filename1 filename2.
REN [drive:][path]filename1 filename2.
filename2 不能带有路径所以修改子文件夹下的文件后缀名时先 cd到子文件夹下

方法2.

@echo off
set DestPath=D:\Music_bak
cd %DestPath%
for /f "tokens=*" %%i in ('dir /A:-D /S/B') do (
echo %%~pi
move "%%i" "%%~pi\%%~ni.mp3"
)
pause

如果你的文件命中含有空格 需要加上”tokens=” 表示提取一整列,比如:文件路径是D:\Music_bak\I love you.mp3, 如果不加”tokens=” 你获取的 %%i 就会是D:\Music_bak\I

dir /A:-D /S/B
dir 是列出当前目录的文件
/A:-D 表示只列出文件,D(Directory)是列出目录,-D(not Directory)列出文件
/S 表示不仅列出当前目录的文件,也包括子目录(subdirectories)的文件
/B 表示只显示最简单的信息,也就是只显示长文件名

%%~pi 是只取到路径
%%~ni 是只取文件名

在控制台输入 for /? 回车会看到所有信息,下面是关于%i的一小段:
In addition, substitution of FOR variable references has been enhanced.
You can now use the following optional syntax:

%~I         - expands %I removing any surrounding quotes (")
%~fI        - expands %I to a fully qualified path name
%~dI        - expands %I to a drive letter only
%~pI        - expands %I to a path only
%~nI        - expands %I to a file name only
%~xI        - expands %I to a file extension only
%~sI        - expanded path contains short names only
%~aI        - expands %I to file attributes of file
%~tI        - expands %I to date/time of file
%~zI        - expands %I to size of file
%~$PATH:I   - searches the directories listed in the PATH
               environment variable and expands %I to the
               fully qualified name of the first one found.
               If the environment variable name is not
               defined or the file is not found by the
               search, then this modifier expands to the
               empty string

The modifiers can be combined to get compound results:

%~dpI       - expands %I to a drive letter and path only
%~nxI       - expands %I to a file name and extension only
%~fsI       - expands %I to a full path name with short names only
%~dp$PATH:I - searches the directories listed in the PATH
               environment variable for %I and expands to the
               drive letter and path of the first one found.
%~ftzaI     - expands %I to a DIR like output line

In the above examples %I and PATH can be replaced by other valid
values. The %~ syntax is terminated by a valid FOR variable name.
Picking upper case variable names like %I makes it more readable and
avoids confusion with the modifiers, which are not case sensitive.

Windows 下批量修改文件夹及子文件夹下文件的扩展名_第1张图片
Windows 下批量修改文件夹及子文件夹下文件的扩展名_第2张图片

Windows 下批量修改文件夹及子文件夹下文件的扩展名_第3张图片
Windows 下批量修改文件夹及子文件夹下文件的扩展名_第4张图片

你可能感兴趣的:(bat脚本)