Diskpart batch file with user input selecting disk and partition number

913 Views Asked by At

I am trying to creating batch file. How to use user input in selecting partition no? I want to format some partitions only using diskpart. I already learn how to select disk number but I want to learn select partition number also. Sometimes partition number changed. So how can I user input for selecting partition number?

I am doing below now. But I want at time of selecting partition, it should ask me to select partition number also, but how?

@echo off

cd /d"%~dp0"
rem == List Disk and List Partitions ======================
echo >diskpart.txt List Disk
diskpart /s diskpart.txt

set /p Disk=Please enter the disk number:
if "%Disk%"=="" goto :eof
echo > diskpart.txt Select disk %disk%
echo >> diskpart.txt list partition
diskpart /s diskpart.txt

echo ============= Please check your Disk and all Partition numbers before continue =============

pause

rem == List Disk Again for Final Confirmation ======================
echo >diskpart.txt List Disk
diskpart /s diskpart.txt

set /p Disk=Please enter the disk number:
if "%Disk%"=="" goto :eof
echo > diskpart.txt Select disk %disk%

rem == 1. Format System partition ======================
echo >> diskpart.txt select partition 1
echo >> diskpart.txt format quick fs=fat32 label="System"

rem == 2. Format Windows partition ========================
echo >> diskpart.txt select partition 3
echo >> diskpart.txt format quick fs=ntfs label="Windows"

rem == 3. Format Recovery tools partition ================
echo >> diskpart.txt select partition 4
echo >> diskpart.txt format quick fs=ntfs label="Recovery"

echo >> diskpart.txt list partition

diskpart /s diskpart.txt

pause
1

There are 1 best solutions below

3
On

(Since I'm a new to stackoverflow I cannot use comment for clarification.)

at time of selecting partition, it should ask me to select partition Is the first "partition" is ment to be "disk" ?

As I understood you present the user the diskpart output of list disk and want him to select the disk AND the partition at the same time?

Fetch all availabel disks and keep them in a list an than call diskpart for each disk and do list partition. That way the user sees all disks with all their partitions and can enter disk and partition number at once.

setlocal ENABLEDELAYEDEXPANSION
set DiskList=
for /F "skip=9 tokens=1,2,*" %%A in ('^(echo.list disk^) ^| diskpart') do (
    set DiskList=!DiskList! %%B
)

for %%a in (%DiskList%) do (
    echo.
    echo.Disk %%a
    for /F "skip=12 delims=" %%A in ('^(echo.select disk %%a ^^^& echo.list partition ^) ^| diskpart') do (
        if /i not "%%A" == "DISKPART> " echo.    %%A
    )
)

Additionally you may keep track of alls disk and partition numbers like in %DiskList% and verify that the user input makes sense.

With this diskpart-for-loops you could also use detail disk and detail volume after the disk selection to use those informations for further verification.

Another way to select disk and partition at once is to use select volume. This requires the partition to have a drive letter assigned which does not apply to the "System" (boot) partition.

EDIT You almost found the answer youself. You have already listed the disks and asked the user for a disk number. Do the same for the partition number. The only difference is that you need to select a disk before you can select a partition.

echo >diskpart.txt List Disk
diskpart /s diskpart.txt
set /p Disk=Please enter the disk number:

rem - listing partition needs a selected disk
echo >diskpart.txt select %Disk%
echo >diskpart.txt list partition
diskpart /s diskpart.txt
set /p Part=Please enter the partition number:

echo >diskpart.txt select %Disk%
echo >diskpart.txt select %Part%
rem - do whatever you want to do, e.g. like
echo >diskpart.txt format ... 
diskpart /s diskpart.txt