简体   繁体   中英

How to answer yes to a (Y/N) [N] prompt to delete a mapped network drive in a batch script on Windows?

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.

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM