2

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:

  • Typing all commands directly to the console works. Only when using batch files, the error occurs.
  • Directly before calling subst X: /D the drive mapping is fine. Directly after calling it, the drive mapping has indeed been removed. Yet why do I get an error?
Juergen
  • 956
  • 1
  • 9
  • 24

1 Answers1

4

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.

Stephan
  • 53,940
  • 10
  • 58
  • 91
  • Better explain than me, you can take the accpeted anser check. – tukan May 15 '19 at 14:41
  • @tukan it's up to Juergen to accept the answer that helped him most. Imagine, we could just "take" the checkmark - LOL - What a mess would that be... – Stephan May 15 '19 at 14:44
  • hehe, true that would be one hell of a mess :). – tukan May 15 '19 at 14:53
  • @Stephan That workaround is a great idea and suits my needs very well! I'll go ahead and accept your answer. – Juergen May 16 '19 at 05:19
  • EDIT: I tried it out and it just generates a second error message. However, the explanation is clear. – Juergen May 16 '19 at 06:05
  • out of curiosity: what error message? (I'm German too, so just copy/paste it) – Stephan May 16 '19 at 06:59
  • Das System kann den angegebenen Pfad nicht finden. – Juergen May 16 '19 at 07:38
  • hm - right - when `cleanup` is executed from `X:` (without `popd`). With `popd` there is no error. Also no error when executed like `X:\>D:cleanup` (with or without `popd`). It looks like my assumptions weren't correct. Let me think about it... – Stephan May 16 '19 at 08:31