感谢rojo下面的批处理文件将检查非法字符,用短划线替换它们,搜索子目录并需要一个位置参数,但是当它运行时它会带你到你指定的目录在论证中。我想留在驱动器/文件夹中运行批处理文件。 (例如:rename.bat c:\ test,这将带你到批处理文件运行后的c:\ test)
@@echo off
IF "%~1"=="" goto Continue
pushd %1
setlocal enabledelayedexpansion
for /r %%I in (*) do (
set "file=%%~nxI"
if "!file:~0,1!"=="~" (
set "file=-!file:~1!"
)
for %%d in (# %%) do (
if not "!file!"=="!file:%%d=!" (
set "file=!file:%%d=-!"
)
)
if not "!file!"=="%%~nxI" (
echo %%~fI -^> !file!
ren "%%~fI" "!file!"
)
)
Exit /B
:Continue
@echo You need drive and directory at end or this batch file
答案 0 :(得分:1)
在FOR /R
循环结束时,将POPD
添加到"撤消"目录更改由相应的PUSHD
命令执行。
setlocal enabledelayedexpansion
pushd %1
for /r %%I in (*) do (
...
)
REM Revert back to the original directory.
POPD
ENDLOCAL
Exit /B
:Continue