我正在尝试写一个批处理文件来删除分配给没有文件系统的分区的驱动器号。我不能使用wmi,因为它正在WinPE恢复环境中使用。
DISKPART> list volume
Volume ### Ltr Label Fs Type Size Status Info
---------- --- ----------- ----- ---------- ------- --------- --------
Volume 0 K DVD-ROM 0 B No Media
Volume 1 L DVD-ROM 0 B No Media
Volume 2 C Windows 7 NTFS Partition 80 GB Healthy System
Volume 3 D Partition 500 GB Healthy System
Volume 4 Partition 500 GB Healthy System
Volume 5 E Partition 500 GB Healthy System
DISKPART> exit
For loop = 0 to 5
If Type[loop]="Partition" then
If Ltr[loop]<>"" then
If Fs[loop]="" then
SELECT VOLUME loop
REMOVE LETTER Ltr[loop]
End If
End If
End If
Next这就是我目前所知道的..。
@echo off
For /F "Tokens=1,2,3,4,5,6*" %%I In ('echo.list volume^|diskpart.exe^|findstr /I /R /C:"Volume [0-9]"') Do (
echo %%I %%J %%K %%L %%M %%N
if "%%N"=="Partition" (
if NOT "%%K"=="" (
if "%%M"=="" (
echo mountvol %%K: /D
)
)
)
)上面的方法不起作用,因为输出是以空格分隔的,并且一些空列搞乱了解析。
另一次尝试,我认为这是可行的,但它可能会更好
@echo off
cd /d "%~dp0"
for /f "skip=8 tokens=*" %%A in ('echo.list volume && echo.exit^|%windir%\system32\diskpart.exe') do (
echo.%%A ^^| find /I " Partition" >nul && (
for /f "tokens=3 delims= " %%B in ("%%A") do (echo.mountvol %%B: /D)
)
)
pause
exit你知道为什么上面的代码在|(竖线)前需要2^吗?
@echo off
for /f "skip=9 tokens=*" %%A in ('echo.list volume^| diskpart') do (
echo."%%A"| find /I " Partition" >nul && (
for /f "tokens=3 delims= " %%B in ("%%A") do (echo.mountvol %%B: /D & mountvol %%B: /D)
)
)
pause
exit上面的方法现在似乎起作用了,我不得不在回声前后加上双引号。“%%A”,然后我去掉了管道前面的2^。
@echo off
setlocal enableDelayedExpansion
set "validDrives=;C;D;E;F;G;H;I;J;K;L;M;N;O;P;Q;R;S;T;U;V;W;X;Y;Z;"
for /f "skip=9 tokens=*" %%A in ('echo.list volume^| diskpart') do (
echo."%%A"| find /I " Partition" >nul && (
for /f "tokens=3 delims= " %%B in ("%%A") do (
if "!validDrives:;%%~B;=!" neq "!validDrives!" (echo.mountvol %%B: /D & mountvol %%B: /D)
)
)
)
pause
exit上面是我的工作脚本,我添加了一些代码来验证驱动器号。
如果任何人能就如何改善这一点提供建议,那么请!
谢谢
发布于 2016-03-26 14:12:28
我可能有一个更简单的解决方案。只需抓取整行,并使用批次子串提取正确的片段
@echo off
setlocal ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
for /f "delims=" %%i in ('^
echo list volume ^|^
diskpart ^|^
findstr Volume ^|^
findstr /v ^
/c:"Volume ### Ltr Label Fs Type Size Status Info"^
') do (
set "line=%%i"
set letter=!line:~15,1!
set fs=!line:~32,7!
if not " "=="!fs!" (
if not " "=="!letter!" (
call :removeVol !letter!
)
)
)
endlocal
exit /b
:removeVol
(
echo select volume %1
echo remove letter %1
) | diskpart
exit /bfor语句生成不带列标题或分隔符的卷的列表。
do语句执行子字符串操作。
确保您对diskpart命令具有管理员权限。我对您的WinRE环境有点好奇。我工作过的大多数应用程序都运行了一个最小的WMI实例,以及WSH,这将使代码更加简洁。
https://stackoverflow.com/questions/32024985
复制相似问题