Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7184049
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T18:07:15+00:00 2026-05-28T18:07:15+00:00

I never wrote a batch file, and now I have to write a batch

  • 0

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,

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T18:07:16+00:00Added an answer on May 28, 2026 at 6:07 pm

    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!  
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Right now i have a batch job I wrote that calls another file and
I have never wrote a line of cfmodule myself. However, now is the time
I'd like to write a batch file that logs data. Each time it runs,
Background I have a Spring batch program that reads a file (example file I
I wrote a short script that never terminates. This script continuously generates output that
I am trying to write a small batch file for a task I do
I wrote a test app that should never stop. It issues t.wait() ( t
I have an msi installer that needs to call a few batch files to
I am to develop an application on windows. I have never done that before
I have a Pig job which analyzes log files and write summary output to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.