0

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
aschipfl
  • 33,626
  • 12
  • 54
  • 99

1 Answers1

0

(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
ooLi
  • 63
  • 6
  • Thanks for reply. Above commands will give me only list of disks and partitions, i agree but i want batch file also ask me which partition number you want to format so that i can put my partition number like 1 or 2 or 3 for system, windows, Recovery partitions format purpose... – Lifeline Feb 09 '22 at 20:08
  • What exactly is the problem? You can select the partition the same way like the disk. Except that you have to select a disk before you can select a partition. The plain diskpart commands would be "select disk 0", "select partition 1" "format ...." – ooLi Feb 09 '22 at 20:21
  • your commands displaying list of disk and partitions. It's not asking me which disk and which partition you want to format. I don't want to put disk no and partition no in batch file. I want cmd commands will ask me for disk selection and partition selection.. like this " set /p Disk=Please enter the disk number:" . Actually i am not expert in cmd commands so i here for some person read my question and exactly make batch file as i need... In my above question i can put disk number but i don t know how to put partition number... i want like this " Pls enter partition number "... Thanks – Lifeline Feb 10 '22 at 09:27