windowsbatch-filesubst

Calling SUBST from Windows batch file


I have problem calling SUBST from a Windows batch file. I want to use one batch to create a virtual drive mapping and a second batch to unmount the drive. However unmounting gives the error message:

The system cannot find the path specified.

Here are my batch files:

prepare.bat

pushd .
subst X: .
X:

cleanup.bat

popd
subst X: /D

Execution give the following outputs:

d:\>prepare.bat

d:\>pushd .

d:\>subst X: .

d:\>X:

X:\>cleanup.bat

X:\>popd

d:\>subst X: /D
Das System kann den angegebenen Pfad nicht finden.

d:\>

Can anyone explain to me what is going wrong here and how to fix it?

EDIT:

Some strange things to notice:


Solution

  • The reason for that behaviour is the way a batch script is executed. It reads a line, executes it and reads the next line to execute until there are no more lines.

    When you unmount the drive, you destroy the (virtual) path to the script, so "reading the next line" fails - the script is "gone". Even if the unmount is the very last line of the script, the interpreter doesn't know until it tries to read the next line - and fails, because the script is not available any more.

    So your error message doesn't come from anything inside your script, but from the interpreter itself, trying to read the next line of the script.

    If you enter the command at the command prompt, there is no "next line" expected, so no error occurs.

    Edit

    to avoid the errormessage:

    subst x: /d & goto :eof
    

    The line is read and parsed in one go, and as this line exits the batch explicitely, the interpreter doesn't try to read the next line.