2016-02-11 101 views
0

,所以我正在寫一個小的批處理文件,做一些有用的東西,但我有一個特定的麻煩,我使用這個命令:列出USB設備,選擇一個備份批處理文件

set /p choice="Press Enter to show available Backup USB Drives" if "%choice%"=="" wmic logicaldisk where drivetype=2 get deviceid, volumename,

其工作,並將其顯示可用的USB設備,但這些設備我想創建一個列表,以便用戶可以選擇作爲備份驅動器要使用的驅動器,任何的幫助深表感謝

+0

不幸的是,我沒有任何USB驅動器插件來查看該WMIC命令的輸出是什麼。你可以編輯你的問題來顯示命令的輸出。謝謝。 – Squashman

+0

的輸出是:說明:可移動磁盤設備ID:E:卷名稱:CORSAIR – Wolf

回答

0

這將顯示所有可用的驅動器和測試,如果答案是一個有效的驅動器(目前在列表中):

@echo off 

setlocal enabledelayedexpansion 

:: Creating a list with all USB drive 

for /f "delims=" %%a in ('wmic logicaldisk where drivetype^=2 get deviceid ^| find ":"') do set "$List=!$List! %%a" 

::Your Menu 

:menu 
set /p choice="Press Enter to show available Backup USB Drives" 
if "%choice%"=="" goto:USB 
goto:menu 

exit/b 

::Displaying available drive and asking to choose one 

:USB 
echo Avaiable Drive ==^> !$List! 
set /p "$Drive=Enter the letter of the Backup drive : " 
cls 

::Testing the availibility of the choosed drive in the List 

echo !$List! | find /i "%$Drive::=%:" && Echo drive OK || Echo drive do not exist && goto:USB 

代替Echo drive OK的你可以做一個Goto:next和使用!$Drive!作爲選用的車程。

+0

提供的腳本僅顯示2個設備,如果我有超過2個設備他們不會顯示,我如何修改它以顯示所有USB設備? – Wolf

-1

:-)我創建了一個用戶輸入的變量,而不是從實際列表中選擇,用戶只需鍵入驅動器盤符,例如:G:我意識到即使我創建了一個實際列表,用戶仍然需要輸入他想要使用的驅動器,爲什麼不使用鍵入的驅動器盤符作爲變量並將其添加到xcopy命令行,這裏是小實用程序的整個代碼,稍後我將添加一個小文件夾樹選擇一個備份源比手動輸入更好

`@echo off 
:: variables 
set backupcmd=xcopy /w /S /D /E /H /I /R /-Y 
:start 
echo ===================== 
echo Backup Utility 
echo ===================== 
echo. 
set /p sourcepath= Enter Folder or Drive path to be Copied: 
echo. 
ECHO ======================================================= 
echo Backup Source Path: %sourcepath% 
ECHO ======================================================= 
echo. 
set /p choice="Press Enter to show available Backup USB Drives" 
ECHO ======================================================= 
echo. 
if "%choice%"=="" wmic logicaldisk where drivetype=2 get deviceid,  
volumename, description 
echo. 
ECHO ======================================================= 
echo. 
ECHO ======================================================= 
set /p backuppath= Enter Backup Destination, Use DeviceID (e.g: E:): 
echo You selected %backuppath% as your Backup destination 
ECHO ======================================================= 
echo. 
echo ======================================================= 
echo. 
echo Backup Sourcepath Selected= %sourcepath% 
echo Backup Destination Selected= %backuppath% 
echo. 
echo======================================================== 
ECHO. 
set /p choice="If it looks good, Press Enter to Continue, or 0 to Start 
Over: " 
ECHO. 
if "%choice%"=="" goto Backup Files 
if "%choice%"=="0" goto start 
pause 

:Backup Files 
echo =========== Ready to Back up your Files =========== 
ECHO. 
%backupcmd% "%sourcepath%" "%backuppath%" 
ECHO. 
echo Success!! 
echo Finished at: 
For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set mydate=%%a-%%b-%%c) 
For /f "tokens=1-2 delims=/:" %%a in ('time /t') do (set mytime=%%a:%%b) 
echo %mytime% %mydate% 
start %backuppath% 
echo ===================================================== 
echo. 
goto start` 
+0

像這樣,你不能控制現有的USB驅動器。測試用戶是否輸入現有的。通過編程,這是一個非常糟糕的解決方案您有一個完整的工作解決方案。 – SachaDee