Link to home
Start Free TrialLog in
Avatar of antOnyk1ng
antOnyk1ng

asked on

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

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?
Avatar of rotatingluficer
rotatingluficer

give a parameter and work with the parameter..
"%%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.
Avatar of Steve Knight
You can use

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

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

Steve
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
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
Avatar of antOnyk1ng

ASKER

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.
ASKER CERTIFIED SOLUTION
Avatar of Bill Prew
Bill Prew

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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
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.
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%
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 . . .
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
Steve thanks for the help I'm sure you'd get it to work but yeah Bill's works great.
No probs, didn't have time to run it through properly today.
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

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

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
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
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
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