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

时间:2015-02-05 16:24:18

标签: batch-file arguments required

感谢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 

1 个答案:

答案 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