0

I would like to automatically delete a mapped network drive from a batch file on Windows after perfoming several commands.

This is my script:

myscript.bat:

net use X: \\myserver\myfolder
pushd X:
dir
net use X: /delete

after listing the files the following message is prompted:

Is it OK to continue disconnecting and force them closed? (Y/N) [N]:

and the script waits for manual user input. I have tried

echo y | net use X: /delete

but the following message is displayed

No valid response was provided.

and again the script waits for manual user input.

j.xavier.atero
  • 506
  • 2
  • 10
  • 25
  • 2
    `popd` before you try to disconnect. It's prompting because the drive is the one you're currently using. If you stop using it first by changing away from it, the prompt won't appear. It's prompting because you're trying to cut off the branch you're standing on, and it doesn't want you to fall to your death. Move off the branch back onto the main tree before you cut the branch. :-) Or just don't do the `pushd X:` at all, and use `dir X:` instead of `dir`. Or (as a final option) don't `net use` the drive at all, and use `dir \\myserver\myfolder` directly. – Ken White Aug 03 '19 at 00:13
  • @Ken White thanks for the answer. I used ```dir``` to simplify the example my actual command is ```for %%i in (*.svg) do "c:\Program Files\Inkscape\inkscape" -d 85.33 -z %%i -e %%~ni.png``` – j.xavier.atero Aug 03 '19 at 00:26
  • @Ken White ```popd``` worked! Thank you! – j.xavier.atero Aug 03 '19 at 00:28
  • Isn't there a `/yes` option to force deletion/disconnection without prompt (just as an alternative)? – aschipfl Aug 03 '19 at 17:08
  • @aschipfl yes, I mentioned it as a comment in Ken White's answer – j.xavier.atero Aug 03 '19 at 17:34
  • @Compo I am quite new and I am not very familiar with the policies of the site. I have asked in meta for advice, you can read it here https://meta.stackoverflow.com/questions/388109 Thanks again! – j.xavier.atero Aug 03 '19 at 19:05
  • 1
    @j.xavier.atero, I applaud your efforts in trying to get clarification on this, but remember that your simplified use of `dir` instead of the `for` command is completely unrelated to the solution I posted as an answer. I have posted a comment to that effect in the meta question you have linked to. – Compo Aug 03 '19 at 19:42

2 Answers2

3

You've mapped a share to a drive (X:), and then changed to that drive. When you try to unmap it, Windows is warning you that you're trying to unmap the current drive. It's like standing on a tree branch with a chain saw, turning to face the tree trunk, and starting to saw off the branch you're standing on, and Windows is telling you to confirm that you really want to do it and fall to your death. :-) Stop trying to cut off the branch you're standing on. Just popd back to where you started before you try to unmap the drive, and you won't be prompted.

net use X: \\myserver\myfolder
pushd X:
dir
popd
net use X: /delete
Ken White
  • 123,280
  • 14
  • 225
  • 444
  • 1
    I just saw that https://stackoverflow.com/q/13284106/10850340 answers the prompted question with ```net use X: /delete /y```. But in my case your solution makes more sense. – j.xavier.atero Aug 03 '19 at 00:43
2

This is untested, but you shouldn't need to use net use at all, PushD alone should temporarily map the drive, if it isn't ready mapped, and PopD would unmap the temporary mapping.

Given your question code, all you should need is:

@PushD "\\myserver\myfolder" 2>NUL || Exit /B
@Dir
@PopD

Should you be using the For command, mentioned in the comments, instead of the Dir command, the solution is exactly the same:

@PushD "\\myserver\myfolder" 2>NUL || Exit /B
@For %%A In (*.svg)Do @"%ProgramFiles%\Inkscape\inkscape.exe" -d 85.33 -z "%%A" -e "%%~nA.png"
@PopD

In both cases, if there are no further commands to perform, you could omit the @PopD line.

Compo
  • 36,585
  • 5
  • 27
  • 39
  • you are right, it works! this approach it's a better fit for my specific problem and I will use it. I can't select this answer as the accepted one because of the way that I worded my question initially, but many thanks!! – j.xavier.atero Aug 03 '19 at 12:15