MS Batch: Check if drive is in use

☆樱花仙子☆ 提交于 2020-02-24 09:07:26

问题


I need to check if a drive (Z:) is in use (e.g. in use by an application, opened).

My Batch File looks like this:

Mount Z:
wait 15 minutes
check if drive Z: is in use

IF NOT: unmount Z:
ELSE: wait 15 minutes
repeat..

Is there any Command for this? Thanks!


回答1:


Use the IF EXIST or IF NOT EXIST combination:

:FindDrive
if exist Z:\nul goto Mounted
timeout /T 5
goto FindDrive
:Mounted

NUL is the a 'virtual' file that exists in every folder. So if c:\anypath\nul exists, the drive exists.




回答2:


@echo off
echo --------------------------------------------------
echo Checking if Y:\10.210.12.8\Rubicon Drive exist? Please wait...
echo IMPORTANT: DO NOT TOUCH THE COMPUTER DURING THIS PROCESS
echo --------------------------------------------------
Rem : Bill Kingsley

SET LOG=c:\Temp\logs

echo **********[%Date% - %Time%****************>%LOG%\Mapdrive.log

  IF EXIST Y:\  (
        echo. 
    echo The drive is already mapped. 
    echo ...Check if the mapp drive is accessible?....
        echo.
    dir y: >>%LOG%\Mapdrive.log
    if ERRORLEVEL 1 GOTO MAPDRIVE     
        GOTO SKIPPED
    ) ELSE (
      echo The drive has not yet been mapped.
      goto MAPDRIVE   
      )
goto end


:SKIPPED
echo.
Echo ******Mapped Drive Y is working Fine********
Echo.
Goto end

:MAPDRIVE
Net use Y: /d
net use Y: \\172.31.161.100\eBBS 
      dir Y: >> %LOG%\Mapdrive.log
  if errorlevel 0 goto SKIPPED
  if errorlevel 1 goto MAPDRIVE  
Goto end

:end 
echo ----------------------------------------
echo Maintenance check complete. check log in %LOG%
echo ----------------------------------------
rem exit



回答3:


If you have your power options set correctly It will stop the patter from spinning if it is not in use.

In windows 7, in your start search for power options. In your current power plan hit change plan options. then hit change advanced power options and go to hard drive.

http://blog.laptopmag.com/windows-advanced-power-options-explained



来源:https://stackoverflow.com/questions/11495908/ms-batch-check-if-drive-is-in-use

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!