I recently needed to create and format a disk partition from a batch file.  To make this more fun, I needed to run the batch file from a USB disk which mounted as the D drive, but I needed the new partition to be the D drive when finished.

in the interest of full disclosure, there are other ways to do this, but I wanted to do it from a batch file because I was doing other things from batch files at the time – so this method fit in.

I started this by doing running diskpart and learning what could do and how to do it.  To start I knew that I needed to create disk 0 partition 2 and format it with NTFS.  The commands in diskpart are:

select disk 0
create partition primary noerr
select part 2
format fs=ntfs label=Data quick

These command create and partition disk 0 partition 2 and format the disk.  But what I found is that the USB disk was the D drive and the new partition was the F driver, not what I want.   But if before creating the partition, I changed the USB disk to be the F drive solving the problem:

select volume 1
assign letter=F

And just to be sure, before formatting the disk, I could set the new partition to be the D drive with:

assign letter=D

Now I have a way to use diskpart to do the work, but what about from a batch file?  For that, all that is needed is a script, or text file, with all of the diskpart commands and tell disk part to run the script:

diskpart /s CreateDDrive.txt

and the contents of CreateDDrive.txt is:

select volume 1
assign letter=F
select disk 0
create partition primary noerr
select part 2
assign letter=D
format fs=ntfs label=Data quick

And that is how I did this from a batch file.

 

Copyright © 2014 – Bruce Eitman
All Rights Reserved