question

balajisivapragasam-5111 avatar image
0 Votes"
balajisivapragasam-5111 asked TeemoTang-MSFT commented

Bat file skip Disk 0 and format all other Hard disk in the pc

Hi Experts,

i am trying to format all the disk present in the pc excluding disk 0 in Winpe. so i am using the below given batch file script

@echo off

setlocal enabledelayedexpansion
setlocal enableextensions

:: Set an initial value for HDDcount to keep track of how many HDDs we have found.

set HDDCount=0
For /f "Skip=3 usebackq tokens=1,2,3" %%A in ( ^
"PowerShell Get-PhysicalDisk ^| Select MediaType, DeviceID, FriendlyName") Do (
if %%A==HDD set /A HDDcount=HDDcount+1
if %%A==HDD call:HDD %%B %%C !HDDcount!)
:: All disks have been processed. Exit the batch file.

EXIT

:HDD

:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
:: We arrive at the HDD routine if Get-PhysicalDisk indicates the disk is an HDD. :
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::

:: If this is the first HDD that we have encountered, then we will set the disk label to
:: "Data". For each additional HDD, we will label them as "Data1", "Data2", "Data3", etc.

set /A ID=%3 - 1
if /I %ID% EQU 0 (
set DiskLabel=Data
) else (
set DiskLabel=Data%ID%
)

(
Echo select disk %2
Echo clean
Echo convert gpt
Echo create partition primary
Echo format quick fs=NTFS label="%DiskLabel%"
Echo assign
Echo exit
)|Diskpart

exit /b

but this batch file formats the Disk 0 also how we can stop the batch file from formatting the DIsk 0.

your help is much appreciated.

Thanks
Balaji S

windows-10-setup
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

If you resolved it using our solution, please click "Accept Answer" on a reply to help other community members find the helpful reply quickly.
If you resolve it using your own solution, please share your experience and solution here. It will be very beneficial for other community members who have similar questions.

0 Votes 0 ·

1 Answer

MotoX80 avatar image
0 Votes"
MotoX80 answered

If you can run Powershell, then I would recommend that you do the entire script in Powershell. Add your diskpart commands to this. (I don't want to format my drives.)


  $AllHDD  = Get-PhysicalDisk | Where-Object MediaType -eq 'HDD'
  $count = 0 
  ForEach ($HDD in $AllHDD) {
        
     # Build diskpart commands based on which disk it is 
     if ($count -eq 0) {
         "This is the first HDD"
         $hdd.Size                                 # show how big it is
         $Label = 'Data'
         $cmds = @( "list disk",
                     "select disk $count"
                     "exit"
                  )
    
     } else {
         "Process subsequent HDD's here."
         $hdd.Size                                # show how big it is
         $Label = "Data$count"           # Example of how to concatenate a variable to a string 
         $cmds = @( "list disk",
                     "select disk $count"
                     "exit"
                  )
    
     }
    
     # Now send those commands to diskpart.
     ""
     "Here are the commands to be executed:"
     $cmds
     "" 
     $cmds | diskpart.exe 
    
     $count++                                      # increment count       
  }


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.