Solved

xcopy files to usb drive by a given drive name (and not knowing what the drive letter is)

Posted on 2010-09-16
20
1,265 Views
Last Modified: 2012-05-10
I would like to expand on a question asked a few years ago "xcopy files to usb drive if you do not know what the drive letter is"

Which resulted in some great code that resolved it.

@echo off
 
setlocal
 
for /f "tokens=1-3" %%a in ('wmic logicaldisk get caption^, description') do if "%%b %%c"=="Removable Disk" set drive=%%a&goto FOUND
 
echo Couldn't detect removable drive
goto :EOF
 
:FOUND
 
echo Removable drive letter is %drive%
 
xcopy "X:\Emergency Planning\test\*.*" "%drive%\" /C/E/H/R/K/D/M/Y

Open in new window


This code works fine if there is just one USB device but when more than one it finds the first removable media drive and fails if it is not the drive with the media card in place.

I would like to xcopy files to a specific USB drive (with a given Name and that drive only) when he BAT file is executed.

Is this possible to do?
0
Comment
Question by:antOnyk1ng
  • 7
  • 6
  • 5
  • +2
20 Comments
 

Expert Comment

by:rotatingluficer
ID: 33690200
give a parameter and work with the parameter..
0
 
LVL 14

Expert Comment

by:ddsh79
ID: 33690312
"%%b %%c"=="Removable Disk" this is coming as the drive letter %%b is variable for first word "Removable" and %%c for "Disk"

I think you can change it to your required drive label

%%a is holding the drive letter.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33690499
You can use

WMIC logicaldisk where VolumeName="YourLabel" get Caption, VolumeName

and parse that for the drive letter if you know the label?

Steve
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33690523
So change this line to:

for /f "tokens=1" %%a in (WMIC logicaldisk where VolumeName="YourLabel" get Caption ^| find ":"') do set drive=%%a&goto FOUND

Steve
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33690597
When you say "with a given Name and that drive only" do you mean you know the drive letter, or the volume label, of the desired target disk?

~bp
0
 

Author Comment

by:antOnyk1ng
ID: 33690922
dragon-it Thanks I tried your code above by copying it into my BAT file... but not working for me.

When I say "with a given Name and that drive only" I mean I would give the drive a unique label so that I could construct the Bat file to only copy to that USB Drive if it exists and not to any other USB Drive.

I am deliberately trying to ignore the Dive Letter given that there could be 2 or more USB drives plugged in and the drive letter could be different each time.
0
 
LVL 51

Accepted Solution

by:
Bill Prew earned 500 total points
ID: 33691275
Give this a try, just change the MyUSB at the top to be the volume label you are looking for.

~bp
@echo off
setlocal
 
set TargetVol=MyUSB
set Drive=
  
for /F "skip=2 tokens=2-3 delims=," %%A in ('wmic logicaldisk get caption^, volumename^, volumeserialnumber /format:csv') do (
  if /I "%%B" EQU "%TargetVol%" set Drive=%%A
)
 
if not defined Drive (
  echo Couldn't detect removable drive.
  goto :EOF
)
 
echo Removable drive letter is %Drive%
xcopy "X:\Emergency Planning\test\*.*" "%Drive%\" /C/E/H/R/K/D/M/Y

Open in new window

0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33691276
it should return just the drive letter of the label you specify.  I see though I have missed a ' before the word WMIC when I copy pasted into the EE window here -- had tried it on my own machine and it worked for me.

Please let me know if is OK now.

for /f "tokens=1" %%a in ('WMIC logicaldisk where VolumeName="YourLabel" get Caption ^| find ":"') do set drive=%%a&goto FOUND

Steve
0
 

Author Comment

by:antOnyk1ng
ID: 33691509
Steve I noticed the ' before and tried it... still not working for me.

Bill yours works perfectly...

set TargetVol=MyUSB

I changed MyUSB to reflect mine and it worked. I plugged in 4 USB sticks and it found "MyUSB" and xcopy worked.

I forced changed the drive from what it was to another driver letter and again it found it and xcopy copied the files. And finally I changed the volume name to something else and it didn't work... which is good.

Job done... I'll set this up on the machine and check it out in the "real world" but I expect it to work.

Thanks very much guys.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33691560
Actually it seems the = needs escaping, is fine from cmd line but from FOR of course need to add a ^ before the =.  Try this:

@echo off
set drive=
for /f "tokens=1" %%a in ('WMIC logicaldisk where VolumeName^="2GIG" get caption') do set drive=%%a

if "%drive%"=="" (
  echo no drive found.  Logical drives are:
  WMIC logicaldisk get caption, volumename, description, size, freespace
  pause
  exit /b
)

echo Found drive at %drive%
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:antOnyk1ng
ID: 33691768
Steve i changed 2GIG to my drive label (BACKUP) and got

no drive found.  Logical drives are:
Caption  Description              FreeSpace     Size          VolumeName
A:       3 1/2 Inch Floppy Drive
C:       Local Fixed Disk         114691207168  250055688192
D:       CD-ROM Disc
E:       Local Fixed Disk         128598720512  155441307648  Data
F:       Local Fixed Disk         58687074304   250056704000  Games
G:       Local Fixed Disk         66100654080   119611985920  MP3
H:       Local Fixed Disk         15596093440   45016924160   Store
J:       CD-ROM Disc
W:       Removable Disk           4938366976    16001302528   BACKUP

Press any key to continue . . .
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33691926
Fair enough.  Sorry mind was on other things after initial post but Bill has sorted you out anyway.

WMIC logicaldisk where VolumeName="2GIG" get caption

returns the drive letter for me, as does DriveType="2" to pickup USB drives but Bill's post just works as you say!

Steve
0
 

Author Comment

by:antOnyk1ng
ID: 33696177
Steve thanks for the help I'm sure you'd get it to work but yeah Bill's works great.
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33697119
No probs, didn't have time to run it through properly today.
0
 

Author Comment

by:antOnyk1ng
ID: 33698138
FYI I had a question asked about USb Drive to USB Drive so I did this.

Which worked great but I am sure someone could make it a little better.

Finds and xcopies from one USB Drive called backup_1 to another USB Drive called backup_2

Antony

@echo off
setlocal

set TargetVol=backup_1
set Drive=

for /F "skip=2 tokens=2-3 delims=," %%A in ('wmic logicaldisk get caption^, volumename^, volumeserialnumber /format:csv') do (
  if /I "%%B" EQU "%TargetVol%" set Drive=%%A
)

if not defined Drive (
  echo Couldn't detect removable drive.
  goto :EOF
)

echo Removable drive letter is %Drive%


set TargetVol=backup_2
set Drive2=

for /F "skip=2 tokens=2-3 delims=," %%B in ('wmic logicaldisk get caption^, volumename^, volumeserialnumber /format:csv') do (
  if /I "%%C" EQU "%TargetVol%" set Drive2=%%B
)

if not defined Drive (
  echo Couldn't detect removable drive.
  goto :EOF
)



echo Removable drive letter is %Drive%

xcopy  "%Drive%\"\*.* "%Drive2%\" /D /s /e /f /h /i /r /k /c /y %1 %2

Open in new window

0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33700143
Actually that's not bad.  Here's a slightly different approach that I might suggest.

You also might want to validate that the two input parms were passed in right at the top.

~bp
@echo off
setlocal
 
REM Define volume labels to use in copy
set SourceVol=backup_1
set TargetVol=backup_2
set SourceDrive=
set TargetDrive=
 
REM Search logical drives on system looking for source and target volumes
for /F "skip=2 tokens=2-3 delims=," %%A in ('wmic logicaldisk get caption^, volumename^, volumeserialnumber /format:csv') do (
  if /I "%%B" EQU "%SourceVol%" set SourceDrive=%%A
  if /I "%%B" EQU "%TargetVol%" set TargetDrive=%%A
)
 
REM Make sure we found both source and target
if not defined SourceDrive (
  echo Couldn't locate removable drive for source [%SourceVol%].
  goto :EOF
)
if not defined TargetDrive (
  echo Couldn't locate removable drive for source [%TargetVol%].
  goto :EOF
)
 
REM Show source and target drive letters
echo Source removable drive letter is: %SourceDrive%
echo Target removable drive letter is: %TargetDrive%
 
REM Do the copy
xcopy  "%SourceDrive%\"\*.* "%TargetDrive%\" /D /s /e /f /h /i /r /k /c /y %1 %2

Open in new window

0
 

Author Comment

by:antOnyk1ng
ID: 33700269
Bill Thanks that looks a little neater... I just did it on the fly and it worked so I was stoked... I'll replace it with yours next time I'm there.

Antony
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33700291
Yes, yours was certainly effective, which is the most important.  I just like to share (and see) different techniques, figure we all learn from each other.  But you did good, so understand my "adjustments" were purely meant as supportive constructive alternative approaches, not "you should have done it this way".

~bp
0
 
LVL 43

Expert Comment

by:Steve Knight
ID: 33700390
Bill - if only all people round here were that way inclined, really p's me off when someone preaches "this is THE way"!  As we all know there is always a million different ways of tackling a problem, some of which are more reliable than others..... but as long as it works ...

Just been dealing with a production line process that was "simplified" for the opertaors by having them scan a bar code of the item and then another bar code describing the process.  Lots of error checking on the first bar code and code to deal with the three possible values for the second bar code..... BUT if the bar code scanner blips twice on the first bar code it goes in the second field too and crashes the program, oops noboody thought there should be an "ELSE" clause for this field so the program carries on regardless with gibberish data.

I have been guilty of writing quick and dirty batch files and programs over the years for single use or for me to run and at the other end of the spectrum ones that check themselves to the nth degree, log events or email results and errors etc. and frankly it depends how long you have to write / perfect it and the target user etc. but then have had scripts that have failed to work properly due to people moving SMTP servers etc. which ironically break the script due to the lack of monitoring but at least we knew it had failed due to the lack of a log file...

Anyway enough ramblings sorry, always interested in your scripts Bill for ideas myself.

Steve
0
 
LVL 51

Expert Comment

by:Bill Prew
ID: 33700459
Thanks Steve, I appreciate the kind words and support.  There are certainly many people here that I too learn much from on a daily basis, yourself included.

I often find with BAT scripts (as well as AWK scripts) that there are often two schools of thought.  One that tries to do the job in the least number of lines or instructions, and the other that takes a more obvious and deliberate approach.  Generally I tend to fall in the latter, having had to maintain my own and other peoples code over the years, I typically favor readability and understandability.  But I'm also a bit of a performance guy, so will occasionally try to tune out extra code and processing time.  Always interesting to see the different approaches, and most enjoyable for me as well when people recognize that there typically isn't one "right" way (which coincidentally they often think is their way).  I certainly enjoy that dialog with the likes of you, the two Q's, AT, leew, etc...

Hope the family is well, enjoy the weekend, I'm off for a bit of R&R myself this weekend after putting in huge hours in the last couple of weeks to get our student portal off the ground here at the university.  Ready for a little break...

~bp
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

More or less everybody in the IT market understands the basics of Networking, however when we start talking about Storage Networks, things get a bit dizzier, and this is where I would like to help.
Ever notice how you can't use a new drive in Windows without having Windows assigning a Disk Signature?  Ever have a signature collision problem (especially with Virtual Machines?)  This article is intended to help you understand what's going on and…
This video Micro Tutorial explains how to clone a hard drive using a commercial software product for Windows systems called Casper from Future Systems Solutions (FSS). Cloning makes an exact, complete copy of one hard disk drive (HDD) onto another d…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

15 Experts available now in Live!

Get 1:1 Help Now