批处理文件 - 获取并解析CMD输出字符串

时间:2012-01-30 10:49:35

标签: batch-file

我从未写过批处理文件,现在我必须编写一个运行命令行并解析其输出的批处理文件。 (例如:CMD:Diskpart List卷,输出:卷列表和可用空间,我想找到具有最大可用空间的卷) 我的问题:

  1. 我应该写什么来获得输出?
  2. 我该如何解析它?
  3. 谢谢大家,

2 个答案:

答案 0 :(得分:1)

这是一个简单的开始位。

echo list volume > %temp%\temp12345
diskpart /s %temp%\temp12345 > %temp%\diskpart.txt

批量执行此操作是一场噩梦,您还有其他选择吗?

编辑:要批量执行此操作,您需要使用FOR

检查For /?以获取有关如何解析数据的示例。

答案 1 :(得分:1)

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!  
相关问题