2

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?

1
  • 5
    Batch files are programming.
    – Luke
    Feb 26, 2010 at 20:16

4 Answers 4

2

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.

3
  • Marks answer seems to be able to do it with one diskpart call, do you know if that works as well?
    – BNT
    Apr 11, 2018 at 7:49
  • @BNT: If you know the IDs beforehand ...
    – Joey
    Apr 11, 2018 at 9:17
  • thanks for your reply, i assume you mean that i need to know the IDs because the select command doesnt feature a noerr switch like i described in my more specialized question?
    – BNT
    Apr 11, 2018 at 12:06
1
@echo off
setlocal enabledelayedexpansion
cls
:: Eg. To enable choice prompt, change to set _CHOICE.ENABLE=
::     To disable choice prompt, change to set _CHOICE.ENABLE=::
::     Do the same for set _DISKPART.ENABLE=::
set _CHOICE.ENABLE=
set _DISKPART.ENABLE=::

set _TMP.FILE=%~dp0%~n0.DPS

for /f "skip=1 tokens=1" %%a in ('wmic diskdrive where "MediaType='Fixed Hard Disk Media'" get index') do (set _DISK.AVAIL=!_DISK.AVAIL! %%a)
set _DISK.AVAIL=!_DISK.AVAIL:~1!

for %%a in (!_DISK.AVAIL!) do (set "_DISKPART.SCRIPT=!_DISKPART.SCRIPT!select-disk-%%a create-partition-primary format-fs=ntfs-label="LABEL%%a"-quick-compress ")

:_FN.VAR2FILE
for /f "tokens=1*" %%a in ("!_DISKPART.SCRIPT!") do (
   set _DISKPART.SCRIPT.FORMAT=%%a
   set _DISKPART.SCRIPT.FORMAT=!_DISKPART.SCRIPT.FORMAT:-= !
   if not exist "%_TMP.FILE%" (echo !_DISKPART.SCRIPT.FORMAT! > %_TMP.FILE%) else (echo !_DISKPART.SCRIPT.FORMAT! >> %_TMP.FILE%)
   set _DISKPART.SCRIPT=%%b
)
if defined _DISKPART.SCRIPT (goto :_FN.VAR2FILE)

echo Generated Script^:
type %_TMP.FILE% | more
%_CHOICE.ENABLE% CHOICE /C TC /M "[T]erminate, or [C]ontinue"
%_CHOICE.ENABLE% if errorlevel 2 (call :_FN.DISKPART.RUN)
goto :_FN.FINISH
goto :eof

:_FN.DISKPART.RUN
if "!_DISKPART.ENABLE!"=="::" (echo Command is disabled, please see the header for information on how to enable it.)
%_DISKPART.ENABLE% diskpart /s %_TMP.FILE%
goto :eof

:_FN.FINISH
if exist "%_TMP.FILE%" (del /q %_TMP.FILE%)
echo All Done.
goto :eof

An elegant solution.

1
  • does this result in only one call to diskpart with all drives in the parameter file abc.txt?
    – BNT
    Apr 11, 2018 at 7:47
1

If you are always repeating the same disks then you could just put them in as static. I've done this for USB keys.

SELECT DISK 6
CLEAN
CREATE PART PRI 
SELECT PART 1 
ACTIVE 
ASSIGN
FORMAT FS=fat32 quick label="WINPE"

SELECT DISK 7
CLEAN
CREATE PART PRI 
SELECT PART 1 
ACTIVE 
ASSIGN
FORMAT FS=fat32 quick label="WINPE"
EXIT
1
  • does selecting multiple disks from the same abc.txt actually work? This would be superior to calling diskpart for each disk seperately
    – BNT
    Apr 11, 2018 at 7:45
0

You could simply loop and automatically re-create the "abc.txt" file in each iteration.

You must log in to answer this question.