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.

修改之前的文件列表
修改之前子文件夹文件列表

运行结果
运行结果

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值