【问题标题】:Assign multiple drive letters to variables将多个驱动器号分配给变量
【发布时间】:2017-01-14 04:04:09
【问题描述】:

我正在编写一个批处理文件,一开始很简单的事情已经开始滚雪球成一个更大的项目。我学得越多,我就越想实施。所以我的脚本的基础是我希望它能够自动执行多个 SD 卡的格式化和文件复制过程,并能够在 2 种不同格式之间进行选择。公平警告:其中一些命令是来自this genius 的第 3 方软件(我的意思是恭敬)。到目前为止,这是我想出的:

@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

【问题讨论】:

  • 我不确定我是否明白你的问题是什么......你提供的大代码似乎没有必要,但你到底想用较小的 sn-p 实现什么,最重要的是:什么是你的问题吗?
  • 请阅读此帮助主题:minimal reproducible example!

标签: windows batch-file variables automation format


【解决方案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

【讨论】:

  • 谢谢。我什至没有想到这一点。到那时,大脑中的所有葡萄糖都用完了。我知道这一定很简单。再一次,我是脚本写作的新手,而且不太流利。再次感谢!!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-24
  • 1970-01-01
相关资源
最近更新 更多