3

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

ZackG
  • 177
  • 3
  • 3
  • 9
  • 2
    Try remarking out `@echo off` and that should help you figure out what line is resulting in "The system cannot find the path specified". – rojo Mar 01 '13 at 16:35
  • Why do you think the error is in this script, and not your "myotherscript"? – dbenham Mar 01 '13 at 18:59
  • Well, I've run "myotherscript" independently and had no issues. So unless there's something about the interaction between the two, I don't see how it could be. I'll add it to the original question. – ZackG Mar 01 '13 at 20:35
  • Ah, so you've got yet another script, `purge.bat`. Have you verified that the error doesn't proceed from *that* batch file? By the way, you are not using the full path when calling it. – Andriy M Mar 01 '13 at 20:46
  • I know its not coming from purge.bat. The other two I wrote myself, so there could be something wrong with them. purge.bat is part of a commercial software package, so I highly doubt its from that. (I'm not?) – ZackG Mar 02 '13 at 03:07
  • All right, the issue is probably not with the `purge.bat` (and it's certainly not with the `call purge` command itself because that would produce a different message if it could not find `purge.bat`). However, have you already tried using the method suggested by @rojo to find the cause of the issue? – Andriy M Mar 02 '13 at 18:02
  • Yes, it seems to have trouble after executing myotherscript the first time when it tries to chdir to the parallel directory. Though it doesn't print "chdir /d Z:\path\changing\to" like it did at the beginning of the script. – ZackG Mar 02 '13 at 18:32
  • The other script has `@echo off` too. You could comment it out as well to find out the offending bit more precisely. – Andriy M Mar 02 '13 at 21:01
  • I get that error message when my path includes relative path navigation items, eg "cd ..\xyz" – AnneTheAgile Apr 25 '13 at 22:03
  • Oh the shame! My error was due to my bug... is there a blush icon? – AnneTheAgile Apr 26 '13 at 14:30
  • Shouldn't that be `chdir /d %origdir%`? Pretty sure the `%` are required. – David Wohlferd Apr 05 '18 at 03:24

2 Answers2

1

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

dbeasy
  • 61
  • 3
1

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.

David Wohlferd
  • 7,110
  • 2
  • 29
  • 56
  • I don't know why it was reactivated either, and I can't confirm that this is the right answer (the need for it has long since passed) but I'm pretty sure it is, so I'll accept it. – ZackG Apr 06 '18 at 12:24