Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

DiskPart automated

858 views
Skip to first unread message

Docfxit

unread,
Nov 7, 2015, 1:50:06 PM11/7/15
to
I would like to have a batch file that would format a USB flash drive for me.

I found this bat file. It doesn't list the drives so I can select the USB.

@echo off
setlocal enableextensions disabledelayedexpansion

:ask
rem select disk to format
call :showDiskTable
set /p "diskNumber=Type number of disk to format: " || goto :processCancelled

rem test disk selection
( echo select disk %diskNumber%
echo list disk
) | diskpart | findstr /b /c:"*" >nul || (
echo(
echo WRONG SELECTION
echo(
goto :ask
)

rem confirm disk selection
cls
call :showDiskTable
set "answer=%random%%random%"
set /p "confirm=If you are sure you want to format disk %diskNumber%, type %answer%:"
if not "%confirm%"=="%answer%" goto :processCancelled

rem Create script file
set "scriptFile=%temp%\%~nx0.%random%%random%%random%.tmp"
> "%scriptFile%" (
echo SELECT DISK %diskNumber%
echo CLEAN
echo CREATE PARTITION PRIMARY
echo FORMAT FS=fat32 quick label="Backup"
echo ACTIVE
)

rem execute script file
type "%scriptFile%"
rem diskpart /s "%scriptFile%"

rem cleanup and exit
del /q "%scriptFile%"

echo(
echo DONE
echo(

exit /b 0

:showDiskTable
echo(
echo =====================================================
echo list disk | diskpart | findstr /b /c:" "
echo =====================================================
echo(
goto :eof

:processCancelled
echo(
echo PROCESS CANCELLED
echo(
exit /b 1

Thanks,

Docfxit

Kody Brown

unread,
Nov 8, 2015, 7:55:13 PM11/8/15
to
As far as I know you cannot pipe commands into diskpart. It can execute instructions from a file though, which is in the batch file you found/posted. However, that batch file looks like it might need some help in other areas too.

The showDiskTable function should look something like this instead:

:showDiskTable
set "tmpfile=%TEMP%\tmp_%RANDOM%_%RANDOM%"

(
echo LIST DISK
echo EXIT
) > "%tmpfile%"

echo =====================================================
call DISKPART.exe /s "%tmpfile%" | findstr /B /C:" "
echo =====================================================

if exist "%tmpfile%" del /F /Q "%tmpfile%"
set "tmpfile="

goto :eof

Todd Vargo

unread,
Nov 8, 2015, 8:12:54 PM11/8/15
to
You found the wrong batch file. You said you wanted to format a USB
flash drive. For that use FORMAT, not diskpart.

The following batch does what you described on the first line of your
post. Fill in drive letters G thru X and run it to verify that it does
what you want.

Note, the format command is disabled in the last line and will only
display the format command to screen. To enable it, you must remove the
ECHO:DEMO: from the line and save.

For help with format, type FORMAT/? at prompt. If you still need help,
post again with more details about the problem.


@echo off
setlocal enableextensions enabledelayedexpansion

set "dlist="
set "drive="
set "answer="
for %%a in (D E F - Y Z) do if exist %%a: set "dlist=!dlist! %%a"
echo List of available drives... %dlist%

set /p "drive=Type drive letter to format and press enter: "
echo.

for %%a in (%dlist%) do if /i "%%a" equ "%drive%" set "drive=%%a"
for %%a in (%dlist%) do if /i "%%a" equ "%drive%" goto :confirm

echo Error: Invalid drive letter %drive%: selected.
pause
echo.
%0
goto :eof

:confirm
echo You chose drive %drive%:.
set /p "answer=Are you sure you want to reformat this drive? Y/N "
echo.
if /i "%answer%" equ "y" goto :format
goto :eof

:format
echo bye-bye drive %drive%:
ECHO:DEMO: format %drive%: /Q


--
Todd Vargo
(Post questions to group only. Remove "z" to email personal messages)

Docfxit

unread,
Nov 9, 2015, 12:51:53 AM11/9/15
to
Todd Vargo

Thank you for the format script. When I described what I wanted I miss spoke.
I did want more than just a format.
USB bootable drives are very touchy. They sometimes work with just deleting the files. They sometimes work with just a format. And sometimes they need to be cleaned, re-partitioned, formatted and set active.
The bat file I posted is attempting to accomplish all required to make it successful.

Thanks,
Docfxit

Docfxit

unread,
Nov 9, 2015, 12:58:31 AM11/9/15
to
Kody Brown

Thank you very much for the code. I have incorporated your code in this version (work-in-process) of my script.

I am stuck on line 67
call DISKPART.exe /s "%tmpfile%" >DiskpartList.txt | findstr /B /C:" " >nul || (

It's processing:
echo (
echo WRONG SELECTION
echo )
goto :ask
which is just after line 67
I don't understand what it is doing.

This is my latest version of the script:
@echo off
setlocal enableextensions disabledelayedexpansion

:ask
rem select disk to format
call :showDiskTable
type "%CD%\DiskpartList.txt"
set /p diskNumber="Enter the disk number: " || goto :processCancelled

rem test disk selection
call :selectDrive

rem confirm disk selection
cls
call :showDiskTable
set "answer=%random%"
set /p confirm="If you are sure you want to format disk %diskNumber%, type %answer%: "
if not "%confirm%"=="%answer%" goto :processCancelled

rem Create script file
set "DiskPartScriptFile=%temp%\DiskPartScriptFile%~nx0.%random%%random%%random%.tmp"
> "%DiskPartScriptFile%" (
echo SELECT DISK %diskNumber%
echo ::CLEAN
echo ::CREATE PARTITION PRIMARY
echo ::FORMAT FS=fat32 quick label="Backup"
echo ::ACTIVE
)

rem execute script file
type "%DiskPartScriptFile%"
rem diskpart /s "%DiskPartScriptFile%"

rem cleanup and exit
::del /q "%DiskPartScriptFile%"
::del /q "DiskpartList.txt"
echo(
echo DONE
echo(

exit /b 0

:showDiskTable
set "tmpfile=%TEMP%\Diskpartcmd.tmp"
(
echo LIST DISK
echo EXIT
) > "%tmpfile%"
echo ============================
call DISKPART.exe /s "%tmpfile%" >DiskpartList.txt | findstr /B /C:" " >nul
echo ============================

if exist "%tmpfile%" del /F /Q "%tmpfile%"
set "tmpfile="
goto :eof

:selectDrive
set "tmpfile=%TEMP%\Diskpartcmd.tmp"
(
echo select disk %diskNumber%
echo list disk
echo EXIT
) > "%tmpfile%"
echo ============================
call DISKPART.exe /s "%tmpfile%" >DiskpartList.txt | findstr /B /C:" " >nul || (
echo (
echo WRONG SELECTION
echo )
goto :ask
echo ============================

if exist "%tmpfile%" del /F /Q "%tmpfile%"
set "tmpfile="
goto :eof

:: | findstr /b /c:" "
:processCancelled
(
PROCESS CANCELLED
)
exit /b 1

Thank you,
Docfxit

Kody Brown

unread,
Nov 9, 2015, 12:20:18 PM11/9/15
to
The issue appears to be with the following line:

call DISKPART.exe /s "%tmpfile%" >DiskpartList.txt | findstr /B /C:" " >nul

You are running a couple different commands on this line, but the first command doesn't send anything to the second.

Here it is explained:

call DISKPART.exe /s "%tmpfile%" >DiskpartList.txt

is saving the output of the diskpart command to the DiskpartList.txt file. This is called redirection; you are redirecting stdout from the DISKPART command to a file.

The result of that is piped (|) to the findstr command. The problem is that the output of the first redirection is nothing.

So,

I haven't tested this, but it seems you need to just re-order that line..

First, process the file,

call DISKPART.exe /s "%tmpfile%"

then pipe (|) its output (the list of disk drives) to the findstr command (to only show the lines you're interested in),

findstr /B /C:" "

then, redirect (>) the output of that command to your file,

> DiskpartList.txt


Here is what it will it would look like on one line.

call DISKPART.exe /s "%tmpfile%" | findstr /B /C:" " > DiskpartList.txt


I hope this helps to understand what is going on. In looking at your current batch file, the :selectDrive function will also need to be changed in the same way as :showDiskTable.


Docfxit

unread,
Nov 9, 2015, 3:11:26 PM11/9/15
to
Thank you very much Kody.

That was a great help.

I am now having a problem with the "%tmpfile%" not being written out.
It's on line 52

:showDiskTable
set "tmpfile=%TEMP%\Diskpartcmd.tmp"
(
echo LIST DISK
echo EXIT
) >"%tmpfile%"

Expands to: set "tmpfile=R:\Temp\Diskpartcmd.tmp"
The drive and folder is writable.

I can't figure out why it won't write the file.

Thank you,

Docfxit

Docfxit

unread,
Nov 9, 2015, 4:48:25 PM11/9/15
to
I was able to get that working.
I'm now having trouble with this code:
call DISKPART.exe /s "%tmpfile%" | findstr /B /C:"*" > DiskpartList.txt >nul || (
echo (
echo WRONG SELECTION
echo )
goto :ask
)

This is what is in the %tmpfile%
select disk 3
list disk
EXIT

This is what is in DiskPartList.txt
Disk ### Status Size Free Dyn Gpt
-------- ------------- ------- ------- --- ---
Disk 0 Online 447 GB 1024 KB
Disk 1 Online 14 GB 0 B *
Disk 2 Online 4596 MB 6144 KB
Disk 3 Online 57 GB 0 B

It is displaying WRONG SELECTION
and it shouldn't

Thanks,
Docfxit

Docfxit

unread,
Nov 9, 2015, 5:36:42 PM11/9/15
to
On Monday, November 9, 2015 at 1:48:25 PM UTC-8, Docfxit wrote:
> On Monday, November 9, 2015 at 12:11:26 PM UTC-8, Docfxit wrote:
> > Thank you very much Kody.
> >
> > That was a great help.
> >
> > I am now having a problem with the "%tmpfile%" not being written out.
> > It's on line 52
> >
> > :showDiskTable
> > set "tmpfile=%TEMP%\Diskpartcmd.tmp"
> > (
> > echo LIST DISK
> > echo EXIT
> > ) >"%tmpfile%"
> >
> > Expands to: set "tmpfile=R:\Temp\Diskpartcmd.tmp"
> > The drive and folder is writable.
> >
> > I can't figure out why it won't write the file.
> >
> > Thank you,
> >
> > Docfxit
Please ignore the above post

This is the correct code I am having trouble with:

call DISKPART.exe /s "%tmpfile%" | findstr /C:"Disk 3" > DiskpartList.txt >nul || (

Kody Brown

unread,
Nov 10, 2015, 10:46:23 AM11/10/15
to
I'm not certain, but it looks like you're using that as an if-else, right? You're trying to display 'wrong selection' if Disk 3 was not in the output..?

As far as I understand it, you won't get the errorlevel returned from a redirection. And, I don't know why you are redirecting the file redirection to nul as in `... findstr .. > DiskpartList.txt >nul ...`, because that will most likely fail.

Have you tried removing the `>nul` statement? It will probably never fail in that case, because redirecting the output of findstr will always work.


In order to get it to work, you'll may have to separate those commands on different lines, if I am understanding what it is you're trying to do.. Regardless, it would sure make your code easier to understand.

Maybe something like this:

REM list disks, filter and save to DiskpartList.txt
call DISKPART.exe /s "%tmpfile%" | findstr /C:"Disk 3" > DiskpartList.txt

REM now, check if `Disk 3` is really in the output/saved file
findstr /C:"Disk 3" "DiskpartList.txt"
if %errorlevel% NEQ 0 (
echo WRONG SELECTION..
)

Docfxit

unread,
Nov 11, 2015, 1:46:48 PM11/11/15
to
That worked really great.

Thank you very much,

Docfxit

Petr Laznovsky

unread,
Nov 14, 2015, 7:28:33 AM11/14/15
to
Dne 9.11.2015 v 1:55 Kody Brown napsal(a):
> As far as I know you cannot pipe commands into diskpart.

You can:

echo select disk 1| diskpart.exe


0 new messages