windows – 使用DISKPART删除驱动器号的BAT / CMD文件
发布时间:2020-12-14 02:47:11 所属栏目:Windows 来源:网络整理
导读:我正在尝试编写一个批处理文件来删除分配给没有文件系统的分区的驱动器号.我不能使用wmi,因为它在WinPE恢复环境中使用. DISKPART list volume Volume ### Ltr Label Fs Type Size Status Info ---------- --- ----------- ----- ---------- ------- --------
我正在尝试编写一个批处理文件来删除分配给没有文件系统的分区的驱动器号.我不能使用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%system32diskpart.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 以上是我的工作脚本,我添加了一些代码来验证驱动器号. 如果有人可以提供如何改善这一点的建议那么请做! 谢谢 解决方法
我可能有一个更简单的解决方案.只需抓住整行并使用批次子字符串来拉出正确的部分
@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 /b for语句生成没有列标题或分隔符的卷列表. do语句执行子字符串操作. 确保您具有diskpart命令的管理员权限.我对你的WinRE环境有点好奇.我工作过的大部分都运行了一个最小的WMI实例,以及WSH,这将使这个代码更清晰. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- windows – 使用IE9嵌入WebBrowser控件时覆盖IE设置
- 为什么使用其他用户通过PowerShell创建的每周任务失败,错误
- windows ping命令
- .net-3.5 – Windows工作流动态,用户创建的工作流程
- Windows下安装Redis
- Windows C线程参数传递
- 如何判断win32 c应用程序在CTRL-ALT-DEL之后是否失去焦点?
- windows – 使用ShellExecuteEx启动外部应用程序并等待它初
- .net – PresentationCore.dll中出现’System.StackOverflo
- .Net Core 3.0 关于Windows Form和WPF的支持