batch-file - 批处理脚本失败并显示 "The system cannot find the path specified"

标签 batch-file chdir

我编写了以下批处理脚本,它在一个目录上运行另一个批处理脚本,或者通过添加标志在目录树上运行,然后在不同驱动器 (Z:) 上的等效目录或目录树上运行。无论我选择哪个选项,它都会输出错误“系统找不到指定的路径”。如果我只在一个目录上执行它,它确实会执行它应该执行的操作,即使它会给出错误。它不能在目录树上成功运行。我在没有关闭 @echo 的情况下运行它,试图了解它失败的地方,但没有成功。它试图更改的目录确实存在。

@echo off
set origdir=%CD%
if X%~f1==X (
echo Please input a directory.
goto done
)

chdir /d %~f1
for %%X in (myotherscript.bat) do (set FOUND=%%~$PATH:X)
if not defined FOUND (
    echo myotherscript is not in your PATH
    )
if X%2==X/R (
goto recursive
) else ( goto single )

 :recursive     
for /d /r %%G in (.) do call myotherscript
echo Z:%~p1
chdir /d "Z:%~p1"
for /d /r %%G in (.) do call myotherscript
goto ended

:single
call myotherscript
echo Z:%~p1
chdir /d "Z:%~p1"
call myotherscript
goto ended

:ended
chdir /d origdir
goto done

:done
pause

这是“myotherscript”是的,purge 确实存在。

@echo off
if exist "D:\path\to\purge.bat" (
    call purge
    for %%f in (*.log.*) do call :renameit "%%f"
    for %%f in (*.drw.*) do call :renameit "%%f"
    for %%f in (*.asm.*) do call :renameit "%%f"
    for %%f in (*.prt.*) do call :renameit "%%f"
    goto done ) else (
    echo Purge does not exist.
    goto done )

:renameit
ren %1 *.1

:done

如有任何帮助,我们将不胜感激。 谢谢

最佳答案

我不确定为什么这个(非常老的)问题被重新激活。但既然已经发生了,让我们看看是否可以结束它。

这里似乎有两个问题。第一:

it outputs the error "The system cannot find the path specified."

这行看起来像是一个简单的错字:

chdir /d origdir

如果没有“%”标记,这将尝试更改为字面上名为 origdir 的目录,而不是运行脚本的原始目录,即:

chdir /d %origdir%

第二个问题是:

It does do what it's supposed to if I do it on just one directory, even though it gives the error. It doesn't work successfully on a directory tree.

猜测,这是由于这一行:

if X%2==X/R

“IF”区分大小写。如果您尝试使用 /r 运行它,它不会看到递归请求,并且将始终执行 single

关于batch-file - 批处理脚本失败并显示 "The system cannot find the path specified",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15162358/

相关文章:

windows - 将子目录的所有内容复制到父目录中的批处理脚本?

windows - 赢bat文件: How to add leading zeros to a variable in a for loop?

c - 使用 chdir() 从终端更改目录

c - C 中的 Unix Shell - 更改目录

python - os.chdir() 导致 python 多线程出现意外行为

windows - 批处理文件以 °C 获取 CPU 温度并设置为变量

.net - 识别是否安装了dot.net 4.5,如果没有则安装

windows - 从相对路径启动可执行文件

c - fchdir 是如何工作的?

C 调用 chdir 后损坏的双链表