我正在编写一个批处理文件,从简单开始的事情开始滚雪球进入一个更大的项目。我学的越多,我就越想实施。所以我的脚本的基础是我希望它能够自动化多个SD卡的格式化和文件复制过程,并能够在两种不同的格式之间进行选择。公平警告:其中一些命令是来自this genius的第三方软件(我的意思是尊重)。这是我到目前为止所提出的:
@echo off
REM Set the variables below for the file path for the bin file and the drive letters for the sd cards.
SET BIN_PATH=
SET SD1=
SET SD2=
:start
Title Insert SD Cards
cls
echo.
echo.
echo.
echo.
echo Insert SD cards into the readers.
echo.
echo.
echo.
echo.
pause
:ListDrives
Title Drives present?
restartsrdev %SD1%:
restartsrdev %SD2%:
cls
sleep 1
ECHO.
ECHO.
ECHO Are drives %SD1% and/or %SD2% listed below?
ECHO.
ECHO.
wmic logicaldisk get description,name
ECHO.
ECHO.
ECHO (Y) YES
ECHO (N) NO
set choice=
set /p choice=
if not '%choice%'=='' set choice=%choice:~0,99%
if '%choice%'=='Y' goto FormatChoice
if '%choice%'=='y' goto FormatChoice
if '%choice%'=='N' goto Reseat
if '%choice%'=='n' goto Reseat
ECHO "%choice%" is not valid, try again
Pause.
GoTo ListDrives
:FormatChoice
Title FS Preferance
cls
echo.
echo.
echo Format to EXT4 or FAT32?
echo.
echo.
echo.
ECHO (1) FAT32
ECHO (2) EXT4
set choice=
set /p choice=
if not '%choice%'=='' set choice=%choice:~0,99%
if '%choice%'=='1' goto FormatFAT32
if '%choice%'=='2' goto FormatEXT4
ECHO "%choice%" is not valid, try again
Pause.
GoTo FormatChoice
:FormatEXT4
Title Formatting SD Cards to EXT4
cls
echo.
mke2fs -t ext4 -L Label %SD1%:
echo.
echo.
mke2fs -t ext4 -L Label %SD2%:
echo.
echo.
start "Copying BIN to %SD1%:" cmd /c Robocopy %BIN_PATH% %SD1%:\ /e
start "Copying BIN to %SD2%:" cmd /c Robocopy %BIN_PATH% %SD2%:\ /e
echo.
echo.
pause
removedrive %SD1%: -l -47 -e
echo.
removedrive %SD2%: -l -47 -e
GoTo Choose
:FormatFAT32
Title Formatting SD Cards to FAT32
cls
echo.
echo.
echo.
format %SD1%: /fs:FAT32 /V:"" /Q /X
echo.
echo.
echo.
format %SD2%: /fs:FAT32 /V:"" /Q /X
echo.
echo.
echo.
start "Copying BIN to %SD1%:" cmd /c Robocopy %BIN_PATH% %SD1%:\ /e
start "Copying BIN to %SD2%:" cmd /c Robocopy %BIN_PATH% %SD2%:\ /e
echo.
cls
echo.
:Choose
cls
Title Transfer Complete
echo.
echo.
echo.
echo.
echo Please remove the SD cards from the readers.
echo.
echo Want to do it again?
ECHO.
ECHO (1) Format again
ECHO (2) Exit
ECHO.
set choice=
set /p choice=
if not '%choice%'=='' set choice=%choice:~0,99%
if '%choice%'=='1' goto start
if '%choice%'=='2' goto Exit
ECHO "%choice%" is not valid, try again
ECHO.
pause.
goto choose
:Exit
Exit
:Reseat
Title Reseat SD Cards
cls
echo.
echo.
echo.
echo.
echo Reseat the SD cards in the readers or
echo turn the USB hub off/on
echo.
echo.
echo.
pause
GoTo ListDrives
我的问题是我希望能够从这部分代码中分配发现的驱动器号:
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
SET SD1=%%i
SET SD2=%%i
)
)
ECHO Are drives %SD1% and/or %SD2% listed below?
pause
到我的变量SD1和SD2。一次插入的SD卡读卡器永远不会超过两个,所以这不是问题。 任何形式的见解将不胜感激。我已经做了很多研究,如果这是一个简单的解决方案,我道歉,但是我在这里撞墙了。
更新: 以下是最终剧本的结果,以防万一有人感兴趣......
@echo off
:start
Title Insert SD Cards
cls
echo.
echo.
echo.
echo.
echo Insert SD cards into the readers.
echo.
echo.
echo.
echo.
pause
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
IF NOT DEFINED SD1 (
set SD1=%%i
) ELSE (
set SD2=%%i
)
)
)
cls
:ListDrives
Title Drives present?
cls
sleep 1
ECHO.
ECHO.
ECHO Are drives %SD1% and/or %SD2% listed below?
ECHO.
ECHO.
wmic logicaldisk get description,name
ECHO.
ECHO.
ECHO (Y) YES
ECHO (N) NO
set choice=
set /p choice=
if not '%choice%'=='' set choice=%choice:~0,99%
if '%choice%'=='Y' goto FormatChoice
if '%choice%'=='y' goto FormatChoice
if '%choice%'=='N' goto Reseat
if '%choice%'=='n' goto Reseat
ECHO "%choice%" is not valid, try again
Pause.
GoTo ListDrives
:FormatChoice
Title FS Preferance
cls
echo.
echo.
echo Format to EXT4 or FAT32?
echo.
echo.
echo.
ECHO (1) FAT32
ECHO (2) EXT4
set choice=
set /p choice=
if not '%choice%'=='' set choice=%choice:~0,99%
if '%choice%'=='1' goto FormatFAT32
if '%choice%'=='2' goto FormatEXT4
ECHO "%choice%" is not valid, try again
Pause.
GoTo FormatChoice
:FormatEXT4
Title Formatting SD Cards to EXT4
FOR /F "tokens=1" %%I in (BIN_PATH.TXT) do SET BIN_PATH=%%I
FOR /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
IF NOT DEFINED SD1 (
set SD1=%%i
) ELSE (
set SD2=%%i
)
)
)
cls
echo.
mke2fs -t ext4 -L Label %SD1%
echo.
echo.
mke2fs -t ext4 -L Label %SD2%
echo.
echo.
start "Copying BIN to %SD1%" cmd /c Robocopy %BIN_PATH% %SD1%\ /e
start "Copying BIN to %SD2%" cmd /c Robocopy %BIN_PATH% %SD2%\ /e
echo.
echo.
pause
removedrive %SD1% -L -47 -e -i
echo.
removedrive %SD2% -L -47 -e -i
GoTo Choose
:FormatFAT32
FOR /F "tokens=1" %%I in (BIN_PATH.TXT) do SET BIN_PATH=%%I
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
IF NOT DEFINED SD1 (
set SD1=%%i
) ELSE (
set SD2=%%i
)
)
)
Title Formatting SD Cards to FAT32
cls
echo.
echo.
echo.
format %SD1% /fs:FAT32 /V:"" /Q /X
echo.
echo.
echo.
format %SD2% /fs:FAT32 /V:"" /Q /X
echo.
echo.
echo.
start "Copying BIN to %SD1%" cmd /c Robocopy %BIN_PATH% %SD1%\ /e
start "Copying BIN to %SD2%" cmd /c Robocopy %BIN_PATH% %SD2%\ /e
echo.
cls
:Choose
cls
Title Transfer Complete
echo.
echo.
echo.
echo.
echo Please remove the SD cards from the readers.
echo.
echo Want to do it again?
ECHO.
ECHO (1) Format again
ECHO (2) Exit
ECHO.
set choice=
set /p choice=
if not '%choice%'=='' set choice=%choice:~0,99%
if '%choice%'=='1' goto start
if '%choice%'=='2' goto Exit
ECHO "%choice%" is not valid, try again
ECHO.
pause.
goto choose
:Exit
Exit
:Reseat
Title Reseat SD Cards
cls
echo.
echo.
echo.
echo.
echo Reseat the SD cards in the readers or
echo turn the USB hub off/on
echo.
echo.
echo.
pause
GoTo ListDrives
答案 0 :(得分:1)
既然你说永远不会有两个以上的插件,你可以使用另一个IF语句
set SD1=
set SD2=
for /F "usebackq tokens=1,2,3,4 " %%i in (`wmic logicaldisk get caption^,description^,drivetype 2^>NUL`) do (
if %%l equ 2 (
IF NOT DEFINED SD1 (
set SD1=%%i
) ELSE (
set SD2=%%i
)
)
)
有关IF语句的信息,请参阅http://ss64.com/nt/if.html