gpt4 book ai didi

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

转载 作者:可可西里 更新时间:2023-11-01 10:33:17 25 4
gpt4 key购买 nike

我正在编写一个批处理文件,开始时很简单的事情已经开始像滚雪球一样变成一个更大的项目。我学得越多,我就越想实现。所以我的脚本的基础是我希望它能够自动执行多个 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com