Batch file formatting, Diskpart formatting

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Message
Author
mini-rooter
Posts: 9
Joined: 14 Feb 2012 18:58

Batch file formatting, Diskpart formatting

#1 Post by mini-rooter » 14 Feb 2012 19:11

Hi i am hoping to set up my flash drive with a batch file.

Only i know what it does so if someone gets ahold of my drive they will accidentally activate it.

I want a batch file to format my entire flash drive. without popups or questions and fast.

i am relatively new to batching but have tried %CD% to get the relative drive letter of my drive as i do not want to assign it a permanent letter...

i have also tried "%~dp0"

in something like this

@echo off

cls

CD %CD%

%CD%

FORMAT %CD% /Q /fs:ntfs

but its not effective and i have tried hundreds of cominations on how to do this and countless hours of research, although i am aware the batch i have provided is bad and extrememly uneefective and proably not even close to what i need.

but i need a batch to find what the drive letter is and to format all the contents with out requesting permission.

your help is appreciated

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file formatting, Diskpart formatting

#2 Post by foxidrive » 14 Feb 2012 19:18

Are you aware that format doesn't destroy the data and it can be unerased?

mini-rooter
Posts: 9
Joined: 14 Feb 2012 18:58

Re: Batch file formatting, Diskpart formatting

#3 Post by mini-rooter » 14 Feb 2012 19:29

yes, but i just need this one batch to get rid of everything on the drive, without assigning it a letter and without asking for confirmation this would absolutely make my year!

Liviu
Expert
Posts: 470
Joined: 13 Jan 2012 21:24

Re: Batch file formatting, Diskpart formatting

#4 Post by Liviu » 14 Feb 2012 20:23

mini-rooter wrote:Only i know what it does so if someone gets ahold of my drive they will accidentally activate it.
...
but i need a batch to find what the drive letter is and to format all the contents with out requesting permission.
Just curious, how is the drive supposed to know that it's "you" using it vs. "someone" else who'd trigger a self destruct?
mini-rooter wrote:i just need this one batch to get rid of everything on the drive, without assigning it a letter and without asking for confirmation
It's Windows (not you, or the drive) who controls drive letter assignments for removable media. As for the "no confirmation" part... well, scratch that thought ;-)

mini-rooter
Posts: 9
Joined: 14 Feb 2012 18:58

Re: Batch file formatting, Diskpart formatting

#5 Post by mini-rooter » 15 Feb 2012 05:25

im just going to have this batch, or .cmd or even a .exe where i can give it an icon and label it as a game or data... I know better and wont click it but someone else looking around on my drive wont. Yes i don't want to give it a letter from windows, im just having trouble with the relative drive letter. as %CD% wont work with the format option or i am doing it wrong. is there any way i can use %CD% as a variable and use that variable with the format option,

like, %CD% would = F:\ right

SET DRIVE "%CD%"

FORMAT %DRIVE% /Q /fs:ntfs


or would it not work like that? i just want to format the drive with a batch and relative letters retained. once again your help is appreciated.

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file formatting, Diskpart formatting

#6 Post by foxidrive » 15 Feb 2012 05:35

place a file in the root of the USB drive called xyz.bin and you can use this to get the drive letter:

@echo off
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if exist "%%a:\xyz.bin" do set drv=%%a:

Then use a format command of your choice. You can use a third party command line format program which can eliminate any prompts easily, maybe like fat32format.

Usage Fat32Format X:
Erase all data on disk X:, format it for FAT32
It is also possible to specify a cluster size for the disk, e.g
Fat32Format -c1 X: - use 1 sector per cluster ( max size 137GB for 512 bytes per sect)
Fat32Format -c2 X: - use 2 sectors per cluster ( max size 274GB for 512 bytes per sect )
Fat32Format -c4 X: - use 4 sectors per cluster ( max size 549GB ... )
Fat32Format -c8 X: - use 8 sectors per cluster ( max size 1TB ... )
Fat32Format -c16 X: - use 16 sectors per cluster
Fat32Format -c32 X: - use 32 sectors per cluster
Fat32Format -c64 X: - use 64 sectors per cluster
Version 1.01, see http://www.ridgecrop.demon.co.uk/fat32format.htm
This software is covered by the GPL
Use with care - Ridgecrop are not liable for data lost using this tool

mini-rooter
Posts: 9
Joined: 14 Feb 2012 18:58

Re: Batch file formatting, Diskpart formatting

#7 Post by mini-rooter » 15 Feb 2012 16:46

could you possibly elaborate on the %%a option? i dont quite understand, ive inserted the %%a (a-z) and it displays the drive letter perfectly, but how do i use the drive letter in batch?

like format %%a:/q?

how can i use the drive letter? thanks

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file formatting, Diskpart formatting

#8 Post by foxidrive » 15 Feb 2012 19:13

mini-rooter wrote:could you possibly elaborate on the %%a option?

like format %%a:/q?



In the way I presented it you would use the environment variable %drv%

@echo off
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if exist "%%a:\xyz.bin" do set drv=%%a:
format %drv% /switches_here

mini-rooter
Posts: 9
Joined: 14 Feb 2012 18:58

Re: Batch file formatting, Diskpart formatting

#9 Post by mini-rooter » 16 Feb 2012 18:27

i have tried it like that,


@echo off

for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if exist "%%a:\xyz.bin" do set drv=%%a:

pause

cd (comes up E: drive)

pause

%drv% (nothing)

pause

format %drv% /q (required paramater missing and closes promptly)

i put pauses in there so i could see where it went wrong when it didnt work... what did i do wrong?

LOL i forgot to put the xyz.bin in the root of the drive, but now that i have it says that "do" is not an operable command and still the format option says there is a parameter missing...

@echo off

for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if exist "%%a:\xyz.bin" do set drv=%%a: (do is not a proper command?"

pause

format %drv% /q (parameter missing...)

pause

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file formatting, Diskpart formatting

#10 Post by foxidrive » 16 Feb 2012 21:01

I had one too many do statements. Try this: (still untested)

Code: Select all

@echo off
for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if exist "%%a:\xyz.bin" set drv=%%a:
format %drv% /switches_here

mini-rooter
Posts: 9
Joined: 14 Feb 2012 18:58

Re: Batch file formatting, Diskpart formatting

#11 Post by mini-rooter » 16 Feb 2012 21:07

OMGWTFBBQ!!! Thats AWESOME!!!

thank you som much foxidrive and everyone else for all your help it works! ITS ALIVE

thatnk you so much this is exactly what i needed,

your help goes far beyond appreciated it is fantastic thank you so much

the drive letter was a pain in the ass, now all i have to do is format it without popups... THANK YOU SO MUCH!!!!!!!!

mini-rooter
Posts: 9
Joined: 14 Feb 2012 18:58

Re: Batch file formatting, Diskpart formatting

#12 Post by mini-rooter » 16 Feb 2012 22:06

is there a command in batch that presses enter for you?

something like

format %drv% /q /x /fs:ntfs

keypress enter

exit

i have no idea but am hoping there is a way to make the batch file do what you want if you know the next question, such as do you want to format press enter to continue, or even run the cmd minimised or invisable

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Batch file formatting, Diskpart formatting

#13 Post by foxidrive » 16 Feb 2012 22:35

Some programs can accept redirected input. If format still does this then the following example would enter Y, press enter, and enter Q

This is untested and just an example of the technique

(
echo Y
echo.
echo Q
)>file.input

Myexe %drv% /u /q /x /fs:ntfs <file.input

del file.input


Sometimes this will work instead.

type file.input | Myexe %drv% /u /q /x /fs:ntfs

Squashman
Expert
Posts: 4488
Joined: 23 Dec 2011 13:59

Re: Batch file formatting, Diskpart formatting

#14 Post by Squashman » 17 Feb 2012 07:11

I believe this may work as well.

Code: Select all

echo Y | format %drv% /q /x /fs:ntfs


There were other undocumented switches with the FORMAT command as well.

I remember doing this years ago and just doing some Google Searching I found:
/Y
/U
/Force
/AutoTest

These all my be for older version of the FORMAT command. I would research and test them on something external.

EDIT: Looks like /Y is what you would need to use according to this site.
http://www.robvanderwoude.com/format.php

mini-rooter
Posts: 9
Joined: 14 Feb 2012 18:58

Re: Batch file formatting, Diskpart formatting

#15 Post by mini-rooter » 19 Feb 2012 15:21

thanks for all of the help guys i have the answer...

---------------

@echo off


for %%a in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do if exist "%%a:\xyz.bin" set drv=%%a:


echo y| format %drv% /q /x /v:Empty /fs:ntfs
cls

exit
------------

(caution, i was testing this out on friday, with 2 flash drives in, both had xyz.bin in the root as i was making copies of the data, my drive which i didnt want to format was E: and the one i was formatting was F:, the computer chose E: and i lost some data... nothing i didn't have backed up but be careful! on a double click this will format what drive its in or what drive it finds with a xyz.bin file)

(for people who arnt savvy with batch files, in between the dotted lines, paste that into a text document and save as something.BAT, make another text file and save it as xyz.bin there doesnt have to be any text in xyz.bin. once thats done... on double click of the bat file your drive will be formatted.)

once again thanks to all those who helped :)

Post Reply