此代码最后准备使用可移动驱动器(例如插入的USB驱动器):
$drives = [System.IO.DriveInfo]::GetDrives()
$r = $drives | Where-Object { $_.DriveType -eq 'Removable' -and $_.IsReady }
if ($r) {
return @($r)[-1]
}
throw "No removable drives found."
这种方式不需要预先设置固定卷名 . 我们可以使用不同的USB驱动器而无需知道/设置其名称 .
UPDATE 要完成任务的拖放部分,您可以执行此操作 .
创建PowerShell脚本(例如,使用记事本)C:\ TEMP_110628_041140 \ Copy-ToRemovableDrive.ps1(路径由您决定):
param($Source)
$drives = [System.IO.DriveInfo]::GetDrives()
$r = $drives | Where-Object { $_.DriveType -eq 'Removable' -and $_.IsReady }
if (!$r) {
throw "No removable drives found."
}
$drive = @($r)[-1]
Copy-Item -LiteralPath $Source -Destination $drive.Name -Force -Recurse
创建文件Copy-ToRemovableDrive.bat(例如在桌面上),它使用PowerShell脚本:
powershell -file C:\TEMP\_110628_041140\Copy-ToRemovableDrive.ps1 %1
现在,您可以插入USB驱动器并将文件拖到桌面上的 Copy-ToRemovableDrive.bat 图标 . 这应该将拖动的文件复制到刚刚插入的USB驱动器 .