Jump to content

Need to format all disks conected to the system in WinPE environment


Recommended Posts

Posted (edited)

Hi,

I need to format all the disks connected to the system at a time using a single C++ program.For that i am trying to use diskpart . There is a call to system as

system("diskpart /s Script.txt > Log.txt");

I have written Script.txt for formatting a single disk which is as follows

select disk 0

clean

create partition primary

assign letter =d:

exit

After that give command

format d: /FS:NTFS

This would do the work if only one disk is present.If multiple disks are present in the system,what changes i have to do in the script plus program logic.Need to know how to get the disk connected.( can we use list disk here)

Thanks in advance...

Edited by Amits

Posted
Hi,

I need to format all the disks connected to the system at a time using a single C++ program.For that i am trying to use diskpart . There is a call to system as

system("diskpart /s Script.txt > Log.txt");

I have written Script.txt for formatting a single disk which is as follows

select disk 0

clean

create partition primary

assign letter =d:

exit

After that give command

format d: /FS:NTFS

This would do the work if only one disk is present.If multiple disks are present in the system,what changes i have to do in the script plus program logic.Need to know how to get the disk connected.( can we use list disk here)

Thanks in advance...

I actually had a similar problem and because you can't really query what's on the computer you can't just say select disk 1, 2, 3, etc because you can't select a disk that isn't present without getting an error and select doesn't have the NOERR switch which sucks and breaks the diskpart script. So, I wrote an AutoIT script that makes the user select the disk they'll want to install XP on, then it queries all the other disks using the diskpart.au3, and based on some customizing, it writes a diskpart.ini file. Here is my AutoIT script that I re-customizes for your situtation, hopefully you can use it. If you don't know AutoIT or where to get the support files go ask at www.autoitscript.com this is as much help as I can provide... Also you could make it smarter by having AutoIT query the drive letters in use and then build the $drvltrs variable based on that so you don't try and assign a driver letter that's in use by a CD/DVD drive. Good luck.

#include-once
#include <GUIConstants.au3>
#include <Constants.au3>
#include <Misc.au3>
#include "diskpart.au3"
;#include "array.au3"

Global $diskCount2, $myDisks2, $ii
Dim $myVolumes2[20][5]
Global $drvltrs = "c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z"
$drvltrs = StringSplit($drvltrs, ",")

$ii = Ask_TargetDisk()

$file = FileOpen("c:\windows\system32\diskpart.ini", 2)

For $lc = 0 To $diskCount2-1

FileWrite($file, "select disk " & $myVolumes2[$lc][0] & @CRLF)
FileWrite($file, "clean" & @CRLF)
FileWrite($file, "create partition primary noerr" & @CRLF)
FileWrite($file, "select partition 1" & @CRLF)
FileWrite($file, 'format FS=ntfs label="' & $drvltrs[$lc+1] & '_drive" quick override noerr' & @CRLF)
FileWrite($file, "assign letter=" & $drvltrs[$lc+1] & " noerr" & @CRLF)

Next

FileWrite($file, "exit")
FileClose($file)

Run(@COMSPEC & ' /c notepad.exe "c:\windows\system32\diskpart.ini"', "c:\windows\system32")

Exit











Func Ask_TargetDisk()

Local $GUI = GUICreate("Select Target Disk", 690, 320, -1, -1, BitOR($WS_CAPTION,$WS_BORDER,$WS_CLIPSIBLINGS))

Local $cboTargetDisk = GUICtrlCreateCombo("", 8, 8, 673, 25, BitOR($CBS_DROPDOWNLIST, $CBS_AUTOHSCROLL))
GUICtrlSetFont(-1, 10, 400, 0, "Lucida Console")
GUICtrlSetData(-1, "")

Local $txtDiskDetails = GUICtrlCreateEdit("", 8, 32, 673, 236, BitOR($ES_AUTOVSCROLL,$ES_AUTOHSCROLL,$ES_READONLY,$ES_WANTRETURN,$WS_HSCROLL,$WS_VSCROLL))
GUICtrlSetFont(-1, 10, 400, 0, "Courier New")

Local $btnContinue = GUICtrlCreateButton("Continue", 608, 272, 75, 40 )

Local $diskList, $diskDetails, $diskListArray, $diskWithFocus = 1
If Not Ask_TargetDisk_GetSystemDiskConfiguration( $diskList, $diskDetails ) > 0 Then
GUIDelete( $GUI )
Return _Iif( @error, SetError( 1, 2, -1 ), SetError( 1, 3, -1 ))
EndIf

$diskListArray = StringSplit( $diskList, "|" )
If $diskListArray[0] > 0 Then
GUICtrlSetData( $cboTargetDisk, $diskList, $diskListArray[$diskWithFocus] )
GUICtrlSetData( $txtDiskDetails, $diskDetails[$diskWithFocus - 1] )
GUICtrlSetState( $cboTargetDisk, $GUI_FOCUS )
Else
GUIDelete( $GUI )
Return SetError( 1, 3, -1 )
EndIf

GUISetState( @SW_SHOW )

Local $rc = 0, $ec = 0, $xc = 0
While 1

Local $msg = GUIGetMsg()
Switch $msg

Case $btnContinue
Local $m = StringRegExp( GUICtrlRead( $cboTargetDisk ), "(?i)^[ ]*Disk (\d+)", 1 )
If @error Then
$rc = -1
$ec = 1
$xc = 0
ExitLoop
Else
$rc = Int($m[0])
$ec = 0
$xc = 0
ExitLoop
EndIf

Case $cboTargetDisk
Local $cboText = GUICtrlRead( $cboTargetDisk )
For $i = 1 To $diskListArray[0]
If StringLeft( $cboText, 10 ) = StringLeft( $diskListArray[$i], 10 ) Then
If $diskWithFocus = $i Then
ExitLoop
Else
$diskWithFocus = $i
GUICtrlSetData( $txtDiskDetails, $diskDetails[$diskWithFocus - 1] )
EndIf
EndIf
Next

EndSwitch
WEnd

GUISetState( @SW_HIDE )
GUIDelete( $GUI )
Return SetError( $ec, $xc, $rc )
EndFunc


Func Ask_TargetDisk_GetSystemDiskConfiguration( ByRef $cboDisks, ByRef $cboDetails )

Local $myDisks, $diskCount, $diskDetails
Global $myVolumes, $volumeCount
Local $myPartitions, $partitionCount
Local $pid, $version

Local $tmpList = "", $tmpDetails[63] = [""], $emptyDetailArray[1] = [""]

$cboDisks = ""
$cboDetails = $emptyDetailArray

ProgressOn( "Reading Disk Configuration", "Please wait...", "", -1, -1 )
ProgressSet( 0, "Starting diskpart utility..." )
_DiskpartStartConsole( $pid, $version )
If @error Then
ProgressOff()
Return SetError( 1, 2, 0 )
EndIf

ProgressSet( 10, "Getting list of disks in system...", "Diskpart v" & $version )
$diskCount = _DiskpartListDisks( $pid, $myDisks )
$diskCount2 = $diskCount
ReDim $myVolumes2[$diskCount2][5]

If @error Then
ProgressOff()
_DiskpartCloseConsole( $pid )
Return SetError( 1, 2, 0 )
EndIf

For $d = 0 To $diskCount - 1

ProgressSet( 20 + $d, "Reading disk " & $myDisks[$d][0] & " configuration..." )

_DiskpartSelectDisk( $pid, $myDisks[$d][0] )
If @error Then
ProgressOff()
_DiskpartCloseConsole( $pid )
Return SetError( 1, 2, 0 )
EndIf

$volumeCount = _DiskpartDetailDisk( $pid, $diskDetails, $myVolumes )

If @error Then
ProgressOff()
_DiskpartCloseConsole( $pid )
Return SetError( 1, 2, 0 )
EndIf

$myVolumes2[$d][0] = $myDisks[$d][0]
$myVolumes2[$d][1] = $myVolumes[0][1]

$tmpList &= _Iif( $d > 0, "|", "" )
$tmpList &= "Disk " & $myDisks[$d][0] & " (ID: " & $diskDetails[1] & ", Type: " & $diskDetails[2] & ", " & $diskDetails[0] & ")"
$tmpDetails[$d] = "Details for disk " & $myDisks[$d][0] & " (ID: " & $diskDetails[1] & ", Type: " & $diskDetails[2] & ", " & $diskDetails[0] & ")" & @CRLF

If $volumeCount > 0 Then

For $v = 0 To $volumeCount - 1

ProgressSet( 20 + $d + $v, "Reading disk " & $myDisks[$d][0] & ", volume " & $myVolumes[$v][0] & " configuration..." )

$tmpDetails[$d] &= @CRLF & StringFormat( " %-10s %-3s %-11s %-5s %-10s %7s %-9s %-8s", "Volume ###", "Ltr", "Label", "Fs", "Type", "Size", "Status", "Info" ) & @CRLF
$tmpDetails[$d] &= " ---------- --- ----------- ----- ---------- ------- --------- --------" & @CRLF
$tmpDetails[$d] &= StringFormat( " %-10s %-3s %-11s %-5s %-10s %7s %-9s %-8s", _
"Volume " & $myVolumes[$v][0], $myVolumes[$v][1], $myVolumes[$v][2], _
$myVolumes[$v][3], $myVolumes[$v][4], $myVolumes[$v][5], _
$myVolumes[$v][6], $myVolumes[$v][7] ) & @CRLF

$partitionCount = _DiskpartListPartitionsByVolume( $pid, $myVolumes[$v][0], $myPartitions )
$myVolumes2[$d][2] = $partitionCount

If @error Then
ProgressOff()
_DiskpartCloseConsole( $pid )
Return SetError( 1, 2, 0 )
EndIf

If $partitionCount > 0 Then
$tmpDetails[$d] &= StringFormat( @CRLF & @TAB & " %-13s %-16s %7s %7s", "Partition ###", "Type", "Size", "Offset" ) & @CRLF
$tmpDetails[$d] &= @TAB & " ------------- ---------------- ------- -------" & @CRLF

For $p = 0 To $partitionCount - 1
$myVolumes2[$d][3] = $myVolumes2[$d][3] & $myPartitions[$p][0] & ","
$tmpDetails[$d] &= StringFormat( @TAB & " %-13s %-16s %7s %7s", _
"Partition " & $myPartitions[$p][0], $myPartitions[$p][1], _
$myPartitions[$p][2], $myPartitions[$p][3] ) & @CRLF
Next
If $myVolumes2[$d][4] = "" Then $myVolumes2[$d][4] = StringTrimRight($myVolumes2[$d][3], 1)

Else
$tmpDetails[$d] &= @CRLF & @TAB & "No partitions are associated with this volume" & @CRLF
EndIf
Next
Else
$tmpDetails[$d] &= @TAB & "This disk does not contain any recognized volumes or partitions"
EndIf
Next

ProgressSet( 100, "Closing diskpart console...." )
_DiskpartCloseConsole( $pid )
ProgressOff()

If $diskCount > 0 Then
ReDim $tmpDetails[ $diskCount ]
$cboDetails = $tmpDetails
$cboDisks = $tmpList
EndIf

Return SetError( 0, 0, $diskCount )
EndFunc

You'll end up with an output like this:

select disk 0

clean

create partition primary noerr

select partition 1

format FS=ntfs label="c_drive" quick override noerr

assign letter=c noerr

select disk 1

clean

create partition primary noerr

select partition 1

format FS=ntfs label="d_drive" quick override noerr

assign letter=d noerr

select disk 2

clean

create partition primary noerr

select partition 1

format FS=ntfs label="e_drive" quick override noerr

assign letter=e noerr

select disk 3

clean

create partition primary noerr

select partition 1

format FS=ntfs label="f_drive" quick override noerr

assign letter=f noerr

exit

Posted (edited)

I have found a simple solution for this....

We can try like this..

#include <iostream>

#include <conio.h>

#include <stdio.h>

#include <fstream>

#include <string>

using namespace std;

void main()

{

int counter=0;

string str1;

int i= system("diskpart /s Script.txt > Log.txt");

if(i==0)

{

ifstream fin("Log.txt");

string str;

while (fin>>str)

{

if (str =="Disk" )

counter++;

}

cout<<"Number of disks present in the system are "<<(counter-1)<<endl;

fin.close();

}

ofstream fout("format.txt");

if(fout)

{

for(int j=0;j<(counter-1);j++)

{

fout<<"SELECT Disk "<<j<<endl;

fout<<"CLEAN"<<endl;

fout<<"CREATE PARTITION PRIMARY"<<endl;

fout<<"ASSIGN LETTER ="<<char(j+100)<<":"<<endl;

fout<<"EXIT"<<endl;

fout.close();

system("diskpart /s format.txt ");

str1 = "format "+char(j+100)+" /FS:NTFS";

system(str1.c_str());

}

}

getch();

}

Edited by Amits

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...