Format USB drive
- From: "RDub" <rweinerattrcrentdotcom>
- Date: Thu, 28 Jun 2007 10:04:03 -0400
I have a VB6 application that needs to be able to move files on to USB stick
drives as fast and as efficiently as possible with as little user input as
possible.
One of the things that has to happen early on in the process is to make sure
there are no files on the USB stick before writing any of the new files. I
have found that fastest way to insure the disk is empty is to simply format
the drive. The last time I needed to format anything I was using a
Commodore 64 and a 1541 floppy drive. So it's been a while.
So far I have found three ways to do this:
1) Use the Win 32 API and call SHFormatDrive. Works great but pops up a
series of windows that the user has click his way through before the format
process starts. The poor soul who has to use this software could not
tolerate so many distractions in the environment he will be in when this is
happening.
2) Shell to the Command processor and run a format command there. IE
Shell("cmd /c format f: /q /v: ",vbNormalFocus). However this also opens a
command window and waits for the user to hit enter after displaying "Insert
new disk for drive F:
and press ENTER when ready..." prompt. This is far better than the first
method as it requires only one keypress instead of 4 or 5 mouse clicks.
3) The last method I discovered uses WScript Shell to create a Command
environment that I can push (ugh!) Keystrokes into. I was revolted the
moment I thought about using this method, but went ahead anyway. Here is
the code I wound up with.
strDrive = "f:" ' hard coded just for this example
Set objShell = CreateObject("WScript.Shell")
objShell.Run ("cmd") ' Start a Command Prompt
Call Sleep(500) ' The 1/2 sec delays were necessary to
prevent overrun problems
objShell.SendKeys "format " & strDrive & " /q /v: {Enter}"
Call Sleep(500)
objShell.SendKeys "{Enter}" ' The format actually starts after this
command is given
objShell.SendKeys "Exit{Enter}" ' Poof (Command Prompt Window Closes)
Set objShell = Nothing
The scary thing about this is that it actually worked without the operator
having to do anything. (three cheers for me) But you gotta admit this is
*** ugly code that is bound to break sooner or later.
Sooo....
Has anyone got another way to do this? I am offically out of Ideas
Ron W
.
- Follow-Ups:
- Re: Format USB drive
- From: Grand_Poobah
- Re: Format USB drive
- From: Ralph
- Re: Format USB drive
- From: MikeD
- Re: Format USB drive
- Prev by Date: Re: Storing variables with Random Files
- Next by Date: THANKS Re: Storing variables with Random Files
- Previous by thread: Error traping
- Next by thread: Re: Format USB drive
- Index(es):