列出USB设备并选择一个作为备份批处理文件

时间:2016-02-11 19:13:03

标签: list batch-file usb usb-drive

所以我正在编写一个小批处理文件,它可以执行一些有用的操作,但是我遇到了一个具体的问题,我正在使用这个命令:

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

可以工作,它显示可用的usb设备,但是从那些设备我想创建一个列表,以便用户可以选择使用哪个驱动器作为备份驱动器,非常感谢任何帮助

2 个答案:

答案 0 :(得分: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!作为选择的驱动器。

答案 1 :(得分:-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`