Windows – How to partition and format multiple disks using a batch script

batch-filewindows

I am trying to format 'n' number of disks using a batch script. My script goes like this.

diskpart /s "abc.txt"

where abc.txt is:

sel disk 1
create part primary
format FS=NTFS  label=label2 quick compress

My Problem here is I want to 'loop' the commands in abc.txt for the number of disks that exists. But I cannot send an argument like %1 to abc.txt file as it is a .txt file. and my diskpart /s can only take a .txt file as an argument. how to overcome this… could anybody please help?

Best Answer

The following might work, kinda:

echo list disk > list.txt
for /f "usebackq tokens=1,2" %%a in (`diskpart /s list.txt ^| findstr /r /c:"Disk [0-9]"`) do (
   echo sel %%a %%b>abc.txt
   echo create part primary>>abc.txt
   echo format FS=NTFS  label=label2 quick compress>>abc.txt
   diskpart /s abc.txt
)
del list.txt abc.txt

This will first get the list of disks from diskpart and subsequently use them with your script file (which will be dynamically created in the loop).

I won't try it here, since I like my disks the way they are currently.