我从来没有写过一个批处理文件,现在我必须编写一个批处理文件来运行命令行并解析其输出。 (例如:CMD:Diskpart的名单量,输出:卷列表和自由空间,我想找到有最大可用空间的体积) 我的问题:批处理文件 - 获取和解析CMD输出字符串
- 我应该怎么写得到的输出?
- 我该如何解析它?
感谢你的一切,
我从来没有写过一个批处理文件,现在我必须编写一个批处理文件来运行命令行并解析其输出。 (例如:CMD:Diskpart的名单量,输出:卷列表和自由空间,我想找到有最大可用空间的体积) 我的问题:批处理文件 - 获取和解析CMD输出字符串
感谢你的一切,
DiskPart列表卷不会为您提供可用空间,但我认为这只是一个示例。 该批次将为您的系统提供最大容量的驱动器。 它使用足够的结构,让你用自己的要求去...... 你需要把下列批处理文件
@echo off
echo ****************************************************
echo *** Give maximum drive size on a given system ****
echo ****************************************************
echo *** This script shows various batch constructs. ****
echo *** Use at your own risk - no warranty given ****
echo *** that's fit for any intended purpose :-)) ****
echo *** or that it's error free ****
echo ****************************************************
echo.
REM All of our vars will be local - we do not want to pollute environment. For more info exec 'help setlocal' from cmdline
REM implied endlocal will be issued at the end, no need to insert it
setlocal
REM name of temp file we will use
set tmpFile=tmp.txt
REM power will store multiplier we are currently working in
set power=0
REM maximum found drive size
set maxSize=0
REM Enable delayed expansion - must be on, otherwise all vars will be expanded on input. For more info exec 'help setlocal' from cmdline
setlocal enabledelayedexpansion
REM enable extensions (on by default). Please see help setlocal
setlocal enableextensions
REM get input from user. For more info exec 'help set' from cmdline
set /P cmd=Give diskpart command you want to run [list volume]
REM set default command if user did not set anything
if NOT DEFINED cmd set cmd=list volume
REM set file to contain command we want to run
echo %cmd%>%tmpFile%
REM Skip 8 first lines, then read each line as token. For more info exec 'help for' from cmdline
REM use of backquote will enable us to use temp file with spaces in name
set ERRORLEVEL=0
for /f "tokens=* skip=8 usebackq" %%i in (`diskpart /s %tmpFile%`) do (
set line=%%i
REM read 5 chars from input token, starting at char 49, convert it to number and assign to env var. For more info exec 'help set' from cmdline
REM This also shows delayed expansion convention (!var!).
set /A tsize=!line:~49,5!*1
REM test for unequality (we want to run body only if given size is not 0). For more info exec 'help if' from cmdline
REM see also other operators used (LSS, GEQ
if !tsize! NEQ 0 (
REM it's not possible to do most obvious (multiplication) as this would overflow.
REM '(' is block character. Look how they are positioned, it has to be this way!
REM Mind also where you can put spaces!
REM 'set a=7' is different to 'set a=7 '
set unit=!line:~54,2!
REM Mind use of '
if '!unit!'==' B' (
if !power! LSS 1 (
set power=1
set maxSize=!tSize!
)
if !power!==1 (
if !tsize! GEQ !maxsize! (
set maxSize=!tsize!
set maxUnit=!unit!
set maxDrive=!line!
)
)
)
if !unit!==KB (
if !power! LSS 3 (
set power=3
set maxSize=!tSize!
)
if !power!==3 (
if !tsize! GEQ !maxsize! (
set maxSize=!tsize!
set maxUnit=!unit!
set maxDrive=!line!
)
)
)
if !unit!==MB (
if !power! LSS 6 (
set power=6
set maxSize=!tSize!
)
if !power!==6 (
if !tsize! GEQ !maxsize! (
set maxSize=!tsize!
set maxUnit=!unit!
set maxDrive=!line!
)
)
)
if !unit!==GB (
if !power! LSS 9 (
set power=9
set maxSize=!tSize!
)
if !power!==9 (
if !tsize! GEQ !maxsize! (
set maxSize=!tsize!
set maxUnit=!unit!
set maxDrive=!line!
)
)
)
)
)
REM cleanup
del %tmpFile% /Q
REM this prints empty line
echo.
echo Your max drive is: !maxSize! !maxUnit!
echo.
echo Drive details:
echo !maxDrive!
下面是简单的起始位。
echo list volume > %temp%\temp12345
diskpart /s %temp%\temp12345 > %temp%\diskpart.txt
批处理是一个噩梦,你有任何其他的选择吗?
编辑:要批量执行,您需要使用FOR
。
检查For /?
有关如何解析数据的示例。
感谢,我怎么能解析输出? – RRR 2012-01-30 11:37:30