0

Im trying to execute part of a batch file only if a specific partition is at least a certain size. I found an example of diskpart being used in a batch file here: https://stackoverflow.com/questions/18477576/get-the-amount-of-partitions-on-a-disk-0-using-batch-file

But when I try to modify it to get what I need out of it It stops working entirely. I've scaled back to just this:

@ECHO OFF
ECHO select disk 0 > temp.scr
ECHO list partition >> temp.scr
diskpart /s temp.scr | findstr /r "Partition.[0-9]" > temp.txt
pause

based on how I THOUGHT the original worked, shouldn't this be outputting the string found with a regex to temp.txt? temp.exe is empty no matter what I try.

For what it's worth, the end goal is to have a batch file loaded into a winPE image, which is then run automatically with startnet.cmd. This batch file would look at each partition on the disk, find the relevant one (so larger than ~200 gb in this case) and then format that partition, then run some more batch files which I already have made and working.

  • You need to run the bat file you create as an administrator; it appears that a user process calling an elevated task is unable to capture the output of the diskpart command. – CoveGeek Jun 10 '16 at 13:36
0

This batch file will grab all the partition records and parse them into a series of temporary environement variables within the For command

@Echo Off
SETLOCAL ENABLEEXTENSIONS ENABLEDELAYEDEXPANSION
(
ECHO list disk
ECHO select disk 0
ECHO list partition
) > %~dp0temp.scr
For /F "usebackq tokens=2,3,4,5,6,7" %%P IN (`diskpart /s %~dp0temp.scr ^| findstr /r "Partition.[0-9]"`) DO (
    If /i "%%S" EQU "KB" (
        REM Skip drives sized in KiloBytes
    ) Else If /i "%%S" EQU "MB" (
        REM Skip drives sized in MegaBytes
    ) Else (
        REM GB/TB
        If /i "%%S" EQU "GB" Set /A intSize=%%R * 1
        If /i "%%S" EQU "TB" Set /A intSize=%%R * 1024
        If !intSize! GEQ 200 (
            Echo Partition %%P  Type %%Q    Size !intSize! GB   Offset %%T %%U
            Call :s_Work_Partition %%P
        )
    )
)
ENDLOCAL
pause
Goto :EOF
:s_Work_Partition
REM First parameter is disk partition number
If "%~1" EQU "" Goto :EOF
::  %~1 contains the partition number from disk 0
::  it can be used to run any disk command within this command block.


Goto :EOF

Within the body of the For command you have complete freedom to use the token data from the DiskPart process. You must run this batch file as an administrator or it will not be able to work properly.

Because the DiskPart command uses the KB/MB/GB/TB suffixes to the disk size, the suffix is checked against known values to eliminate disks that are too small, but also be able to handle disks that are between 1 and 199 TB which are converted to 1024 - 203776 GB for comparison.

Additional commands can be placed in the code black at the bottom to further automate the process.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.