如何在弹出批量或VBS闪存驱动器 [英] How to Eject a Flash drive in Batch or Vbs

查看:252
本文介绍了如何在弹出批量或VBS闪存驱动器的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想我弹出闪存驱动器,当我键入退出到命令提示符下,我有一个code的外壳,但我没有实际的code将其弹出。

I'm trying to eject my flash drive when i type eject into a command prompt, i have a shell of a code, but i don't have the actual code to eject it.

UNLOCK

@echo off
echo '
set "psCommand=powershell -Command "$pword = read-host 'Enter Password' -AsSecureString ; ^
    $BSTR=[System.Runtime.InteropServices.Marshal]::SecureStringToBSTR($pword); ^[System.Runtime.InteropServices.Marshal]::PtrToStringAuto($BSTR)""
for /f "usebackq delims=" %%p in (`%psCommand%`) do set password=%%p

if %password%==Eject goto EJECT

:EJECT

^if EXIST "D:\Some file in the Flash drive" goto D-DRIVE

:D-DRIVE

^code that ejects the D drive

这一点后,我需要弹出code

after that bit i need the eject code

推荐答案

我要评论说,在魔力大约在同一时间上面做到了同样的事情。然而,经过测试,似乎脚本的diskpart 下马我自己的可移动驱动器$ P $自动重新获取一个盘符他们插在接下来的时间pvents他们我不得不去磁盘管理,并重新分配重新插入时,驱动器盘符。我敢肯定,这不是你想要的。脚本化下马与Win32_Volume 喜欢这个家伙确实有相同的令人遗憾的副作用

I was about to comment that same thing that mojo did above at about the same time. However, after testing, it seems that scripting diskpart to dismount my own removable drives prevents them from automatically re-acquiring a drive letter the next time they're plugged in. I had to go to Disk Management and re-assign a drive letter when re-plugging. I'm sure that's not what you want. Scripting a dismount with Win32_Volume like this guy did had the same unfortunate side effect.

hotplug.dll 喜欢玩这个家伙确实 WASN '吨太大的帮助或者与Windows报告该驱动器的类型不是一个可移动驱动器。

Playing with hotplug.dll like this guy did wasn't much help either, with Windows reporting that the type of drive wasn't a removable drive.

没有,不需要第三方实用工具是最可靠的,至少副作用-Y方法是从我的电脑-ish方面援引弹出动词。这里涉及一个批次/混合的JScript脚本的解决方案:

No, the most reliable, least side-effect-y method that doesn't require 3rd party utilities is to invoke the "Eject" verb from a "My Computer"-ish context. Here's a solution involving a batch / JScript hybrid script:

@if (@a==@b) @end   /* JScript ignores this multiline comment

:: dismount.bat
:: safely dismounts all removable drives

@echo off
setlocal

cscript /nologo /e:JScript "%~f0"

goto :EOF

:: end batch portion / begin JScript portion */

// DriveType=1 means removable drive for a WScript FSO object.
// See http://msdn.microsoft.com/en-us/library/ys4ctaz0%28v=vs.84%29.aspx

// NameSpace(17) = ssfDRIVES, or My Computer.
// See http://msdn.microsoft.com/en-us/library/windows/desktop/bb774096%28v=vs.85%29.aspx

var oSH = new ActiveXObject('Shell.Application'),
FSO = new ActiveXObject('Scripting.FileSystemObject'),
removableDriveType = 1,
ssfDRIVES = 17,
drives = new Enumerator(FSO.Drives);

while (!drives.atEnd()) {
    var x = drives.item();
    if (x.DriveType == removableDriveType) {
        oSH.NameSpace(ssfDRIVES).ParseName(x.DriveLetter + ':').InvokeVerb('Eject');
        while (x.IsReady)
            WSH.Sleep(50);
    }
    drives.moveNext();
}

此外,您还可以调用以类似的方式使用PowerShell的来自同一个命名空间弹出动词,但你会仍然需要从驱动器中被弹出之前退出启动睡眠来prevent的PS线程。我将它作为一个练习O.P.如果他prefers PowerShell中超过WScript的。

Also, you can invoke the "Eject" verb from the same namespace using PowerShell in a similar way, but you'll still need to Start-Sleep to prevent the ps thread from exiting before the drive has been ejected. I leave that as an exercise for the O.P. if he prefers PowerShell over WScript.

编辑:如果您要定义特定的驱动器在你的批处理脚本下马,像这样做:

If you want to define a specific drive to dismount in your batch script, do it like this:

@if (@a==@b) @end   /*

@echo off
setlocal

set "drive=F:"

cscript /nologo /e:JScript "%~f0" "%drive%"

goto :EOF

::  */

var oSH = new ActiveXObject('Shell.Application'),
    FSO = new ActiveXObject('Scripting.FileSystemObject'),
    drive = WSH.Arguments(0);

oSH.NameSpace(17).ParseName(drive).InvokeVerb('Eject');
while (FSO.GetDrive(drive).IsReady) WSH.Sleep(50);

这篇关于如何在弹出批量或VBS闪存驱动器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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