遍历目录可以用for命令的/r参数,你只需要知道顶层目录名就行了.
@echo off
pushd h:\
for /r . %%i in (*.txt) do @ren "%%i" "%%~ni.bak"
popd
当然,你也可以不用for来遍历,改用dir或者xcopy来打印查找的文件到一临时文件,然后根据文件内容进行操作,这种方法比较灵活,可以指定各种文件属性,不过缺点是会产生临时文件,如果不产生临时文件用for+dir或for+xcopy的话效率就不怎么样了。
dir:
pushd f:\
dir /s *.txt >tmp1.txt
popd
xcopy:
pushd h:\
xcopy /l /s /h /f *.txt %tmp%\ >tmp1.txt
popd
得到tmp1.txt后再用for根据tmp1中的文件列表进行操作。