如何检查当前驱动器是否在本地磁盘(cmd)上? [英] How to check if current drive is on a local disk (cmd)?

查看:180
本文介绍了如何检查当前驱动器是否在本地磁盘(cmd)上?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

要获取当前批处理所在的驱动器,很容易使用

To get the drive the current batch resides in is easy using

set batchdrive=%~d0

但是如何检查%batchdrive%是否在本地驱动器上,而不是在(映射的)网络共享上?

But how is it possible to check if %batchdrive% is on a local drive and not on a (mapped) network share?

检查%SYSTEMDRIVE%或固定列表"C:""D:" ...不可靠.

Checking for %SYSTEMDRIVE% or a fixed list "C:" "D:" ... is not reliable.

推荐答案

首先要检查的是batchdrive是否是未映射的网络共享(如果您在cmd.exe之外启动批处理文件,例如通过双击或通过系统调用):

The first thing to check is if batchdrive is an unmapped network share (this happens if you start the batch file outside of cmd.exe, for example via double click or via a system call):

if "%batchdrive%" == "\\" set nshare=1

第二件事是检查batchdrive是否在网络共享列表中.这些显示为net use,其输出类似于

The second thing is to check if batchdrive is in the list of network shares. These are shown with net use, which output is similar to

status        local   remote              network
-------------------------------------------------------------------
OK            D:      \\computer1\share1  Microsoft Windows Network
OK            E:      \\computer1\share2  Microsoft Windows Network
disconnected  F:      \\computer2\share   Microsoft Windows Network
Command executed successfully. 

因此,我们使用findstr /r /c:" [A-Z]: "过滤所有看起来像磁盘驱动器的行的输出,并通过for /f "tokens=2"获取输出的第二部分.

Therefore we filter the output for all lines that looks like a disk drive with findstr /r /c:" [A-Z]: " and took the second part of the output via for /f "tokens=2".

已完全删除(适用于WinXP及更高版本):

Complete snipped (working on WinXP and above):

if "%~d0" == "\\" (
   set nshare=1
) else (
   set nshare=0
   for /f "tokens=2" %%a in ('net use ^| findstr /r /c:" [A-Z]: "') do (
       if "%%a" == "%~d0" set nshare=1
   )
)

对可能出现的问题的评论:-)

Comments for possible issues requested :-)

这篇关于如何检查当前驱动器是否在本地磁盘(cmd)上?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆