Search code examples
batch-filechdir

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


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


Solution

  • I'm not sure why this (very old) question got reactivated. But since it has, let's see if we can close this out.

    There seem to be two problems here. First:

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

    This looks like a simple typo on this line:

    chdir /d origdir
    

    Without the '%' marks, this is trying to change to a directory literally named origdir, rather than the original directory the script was run from, which would be:

    chdir /d %origdir%
    

    Second problem is:

    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.

    At a guess, this is due to this line:

    if X%2==X/R
    

    "IF" is case sensitive. If you tried to run this using /r, it wouldn't see the request for recursion and would always execute single.