Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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?

share|improve this question

migrated from stackoverflow.com Mar 3 '10 at 10:56

This question came from our site for professional and enthusiast programmers.

3  
Batch files are programming. –  Luke Feb 26 '10 at 20:16

3 Answers 3

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.

share|improve this answer

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

share|improve this answer
@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.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.