2

I never wrote a batch file, and now I have to write a batch file that runs a command line and parse its output. (e.g: CMD: Diskpart List volume, Output: list of volumes and the free space, I want to find the volume with the maximum free space) My questions:

  1. what should I write to get the output?
  2. How can I parse it?

Thanks you all,

2 Answers 2

1

DiskPart list volumes will not give you free space, but I assume that was only an example. This batch will give you drive with maximum capacity on your system. It uses enough constructs to get you going with your own requirements... You will need to put the following into a batch file


@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!  
Sign up to request clarification or add additional context in comments.

Comments

1

Here's the easy beginning bit.

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

Doing this in batch is a nightmare, do you have any other options?

Edit: To do it in batch, you need to use FOR.

Check For /? for examples on how to parse data.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.