windows - 为变量分配多个驱动器号

标签 windows batch-file variables automation format

我正在编写一个批处理文件,开始时很简单的事情已经开始像滚雪球一样变成一个更大的项目。我学得越多,我就越想实现。所以我的脚本的基础是我希望它能够自动执行多个 SD 卡的格式化和文件复制过程以及在 2 种不同格式之间进行选择的能力。公平警告:其中一些命令是来自 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

最佳答案

既然你说插入的永远不会超过两个,你可以使用另一个 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
)
)
)

参见 http://ss64.com/nt/if.html有关 IF 语句的信息

关于windows - 为变量分配多个驱动器号,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41614780/

相关文章:

java - LWJGL 自动本地选择器

windows - 如何使用 Windows 批处理脚本将空 ASCII 字符 (nul) 写入文件?

batch-file - 批量,用延迟扩展参数替换延迟扩展字符串

Delphi访问冲突分配局部变量

variables - Jekyll:在定义其他变量时使用变量

.net - 防止单个可执行文件的多个进程实例

windows - 添加注销脚本

windows - 安装适用于 64 位操作系统的 windows azure sdk 时出错 - Windows 7

windows - 从传递给批处理文件的参数中删除封闭的双引号

java - 用变量替换实例