此代碼最后準備使用可移動驅動器(例如插入的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驅動器 .