Batch script fails with "The system cannot find the path specified"

ZackG picture ZackG · Mar 1, 2013 · Viewed 12.7k times · Source

I have written the following batch script, which runs another batch script on a directory, or, with addition of a flag, on a directory tree and then on an equivalent directory or directory tree on a different drive (Z:). No matter which option I choose, it outputs the error "The system cannot find the path specified." 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. I've run it without @echo off to try understand where its failing, without success. The directory which it's trying to change into does exist.

@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

Here is "myotherscript" Yes, purge does exist.

@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

Any help would be appreciated. Thanks

Answer

dbeasy picture dbeasy · Jul 3, 2013

For me I got the "The system cannot find the path specified" due to a missing exe that seemed way later in the script. It seems that the pipes in DOS don't always output data in the order of execution. I was used to UNIX where the output from each "echo" command in a script goes in order, so I had added debug output in the .bat file to try to tell me what lines had executed.

The problem is, the error about the file not found was happening in the output log (and screen) way earlier than the echo commands would indicate. So I don't know if the WinXP cmd shell was going a few steps ahead, or it was parsing for the exe to call during startup of the called bat file or what.

It turned out it was in fact a bad path to the .exe I was running from a call'd bat script, but the echo debug statements made me think I was in a way earlier part of the script. Once I added the right path before the exe it all worked