use batch file to format flash drive then copy files to it

I have a mapped network drive that I want the entire contents of copied to a flash drive.
I'm using:
@echo off

rem copy individual files
xcopy "Z:\*.*" E:\ /d /r /y

rem copy document trees
xcopy "Z:\" E:\ /d /i /s /r /y

XCOPY reference

Open in new window

and it seems to be working ok for the copying of files and folders, but I want to format the drive first each time I run the script.

it's a 16GB drive that is currently NTFS.  Is there a way to automate the format so no user intervention is required? (ie. pressing enter)

If possible, I'd also like it to output success or failure to a text file on the desktop...?
LVL 8
Reece DoddsAsked:
Who is Participating?
 
BillDLConnect With a Mentor Commented:
My worry would be that you could end up formatting the wrong drive if the Flash Drive was not inserted.  Personally what I would do would be to CREATE a Volume Label each time the Flash Drive was formatted, and first test if the E: Drive actually IS the Flash Drive BEFORE proceeding to format.  Something like this (based on andresdelfino's batch file) typed off the top of my head for WinXP but untested:

@echo off
set FlashDrive=E:
set MyVolLabel=MYFLASH
for /f "tokens=6 delims= " %%A in ('vol %FlashDrive% ^|find /i "Volume in Drive "') do set CurrentVolLabel=%%A
if %errorlevel% neq 0 echo Some Error In Finding Volume Label Occurred & pause & goto :EOF
if %CurrentVolLabel% neq %MyVolLabel% echo Volume Labels Do Not Match & pause & goto :EOF
echo.
echo Formatting Drive %FlashDrive% as NTFS with Volume Label: %MyVolLabel% ...
echo.
echo format /q /fs:NTFS /x /v:%MyVolLabel% %FlashDrive%
echo.
echo Place success and failure actions here
echo.
pause

References:
http://ss64.com/nt/format.html
http://ss64.com/nt/vol.html

Checking the Volume Label of the drive is far from the most definitive test, but in the circumstances it would probably suffice as a simple check to avoid a catastrophe.
0
 
Lee W, MVPTechnology and Business Process AdvisorCommented:
TRY:

FORMAT /Q /FS:NTFS /X /U E:
(Assuming E: is your flash drive).
0
 
andresdelfinoConnect With a Mentor Commented:
leww wrote "/U" instead of "/V".

This version also creates the output you wanted on the desktop. Just save this as a batch file, and run it double clicking it.

@echo off

set drive=E:

format /q /fs:NTFS /x /v %drive%

if %errorlevel% equ 0 (
	set output=SUCCESS
) else (
	set output=FAILURE
)

echo %output% > %homedrive%\%homepath%\Desktop\log.txt

Open in new window


Please note that this will only work on an English environment, since "Desktop", as you can see, is hardcoded in English.
0
Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

 
Lee W, MVPTechnology and Business Process AdvisorCommented:
The /u should be "unconditional" format. Problem is, it's not clear if the switch still exists.

The /v labels the drive - there's no need for that.
0
 
andresdelfinoCommented:
You need /V because otherwise format will prompt you for a label.
0
 
Reece DoddsAuthor Commented:
Sorry for the lack of action on this question.  I will test these scripts over the next few days and let you know which one suited and if I need any tweaks.
Thanks
0
 
Reece DoddsAuthor Commented:
This batch script works perfectly.  Thanks
0
 
BillDLCommented:
Thank you reecem27
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.