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

Posted on 2012-03-15
Last Modified: 2012-05-10
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...?
Question by:Reece Dodds
  • 2
  • 2
  • 2
  • +1
LVL 95

Expert Comment

by:Lee W, MVP
ID: 37727359

(Assuming E: is your flash drive).

Assisted Solution

andresdelfino earned 250 total points
ID: 37774792
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.
LVL 95

Expert Comment

by:Lee W, MVP
ID: 37775382
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.

Expert Comment

ID: 37776127
You need /V because otherwise format will prompt you for a label.
Comprehensive Backup Solutions for Microsoft

Acronis protects the complete Microsoft technology stack: Windows Server, Windows PC, laptop and Surface data; Microsoft business applications; Microsoft Hyper-V; Azure VMs; Microsoft Windows Server 2016; Microsoft Exchange 2016 and SQL Server 2016.

LVL 38

Accepted Solution

BillDL earned 250 total points
ID: 37776673
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 Formatting Drive %FlashDrive% as NTFS with Volume Label: %MyVolLabel% ...
echo format /q /fs:NTFS /x /v:%MyVolLabel% %FlashDrive%
echo Place success and failure actions here


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.

Author Comment

by:Reece Dodds
ID: 37854058
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.

Author Closing Comment

by:Reece Dodds
ID: 37944688
This batch script works perfectly.  Thanks
LVL 38

Expert Comment

ID: 37952082
Thank you reecem27

Featured Post

Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware attacks.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Decrypting the Zepto Virus 21 612
DOS - Automated Tree Structure Creation 10 85
Windows 7 Share with XP 22 132
how to find then copy to anotehr file 3 45
Can I legally transfer my OEM version of Windows to another PC?  (AKA - Can I put a new systemboard in my OEM PC?) Few of us are both IT and legal experts but we all have our own views of Microsoft's licensing rules and how they apply.  There are…
Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

911 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

19 Experts available now in Live!

Get 1:1 Help Now