批处理文件移动到参数中的位置

Batch file moves to location in argument

感谢 rojo 下面的批处理文件将检查非法字符,将它们替换为破折号,搜索子目录并需要一个位置参数,但是当它是 运行 时它会将您带到目录您在参数中指定。我想留在 drive/folder 中,批处理文件来自 运行。 (例如:rename.bat c:\test,这将在批处理文件 运行s 后带你到 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 

在您的 FOR /R 循环结束时,将 POPD 添加到 "undo" 相应 PUSHD 命令执行的目录更改。

setlocal enabledelayedexpansion
pushd %1

for /r %%I in (*) do (
...
)

REM Revert back to the original directory.
POPD
ENDLOCAL

Exit /B
:Continue