xcopy files to usb drive if you do not know what the drive letter is

Hello experts,

I am trying to do an xcopy of files on a server then copy them to a location.  I have the xcopy command working properly.  My issue is trying to find out what the removable media drive will be.  It could be any drive letter  I have posted my code below.  It works but it always copies and makes a folder on my desktop called drive.  I need this code to search for the removable drive letter and then copy the files to that drive letter.
@echo off
set testfor=REP
set drive=unknown
 
for %%a in (E,F,G,H,I,J) do call :testdrive %%a
goto continue
 
:testdrive
set test=%1
REM use Dir to get drive label.  Sets drive variable
for /f "tokens=5,*" %%x in ('dir %test%:\*.* 2^>NUL ^| find /i "volume in drive"') do if /i "%%y"=="%testfor%" set drive=%test%:
goto end
 
:continue
echo %testfor% drive is %drive%
xcopy "X:\Emergency Planning\test\*.*" "%drive\" /C/E/H/R/K/D/M/Y
:end

Open in new window

dbrs_helpdeskAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SteveGTRCommented:
That looks like a good method. Your xcopy line is missing a percent:

xcopy "X:\Emergency Planning\test\*.*" "%drive%\" /C/E/H/R/K/D/M/Y
dbrs_helpdeskAuthor Commented:
I have added that percentage that I have missed, thank you.  However, it now creates a folder on my desktop called unknown with nothing inside it.  When I pause my little program I get ''xcopy' is not a recognized as an internal or external command, operable program or batch file.  I need these files copied to my removable drive.
dbrs_helpdeskAuthor Commented:
To add to this, I have corrected another thing and now I get REP drive unknown.

 
 
 
@echo off
set testfor=REP
set drive=unknown
 
for %%a in (E,F,G,H,I,J) do call :testdrive %%a
goto continue
 
:testdrive
set test=%1
REM use Dir to get drive label.  Sets drive variable
for /f "tokens=5,*" %%x in ('dir %test%:\*.* 2^>NUL ^| find /i "volume in drive"') do if /i "%%y"=="%testfor%" set drive=%test%:
goto end
 
:continue
echo %testfor% drive is %drive%
xcopy "X:\Emergency Planning\test\*.*" "%drive%\" /C/E/H/R/K/D/M/Y
:end

Open in new window

Acronis True Image 2019 just released!

Create a reliable backup. Make sure you always have dependable copies of your data so you can restore your entire system or individual files.

SteveGTRCommented:
What does this line display?

echo %testfor% drive is %drive%
dbrs_helpdeskAuthor Commented:
it is to test for the drive that it finds.  If you have a better solution then great.
SteveGTRCommented:
Can you modify this line for debugging purposes and report back with the output?

for /f "tokens=5,*" %%x in ('dir %test%:\*.* 2^>NUL ^| find /i "volume in drive"') do echo if /i "%%y"=="%testfor%" set drive=%test%:

Notice that I placed an echo prior to the if statement.
SteveGTRCommented:
>> it is to test for the drive that it finds.  If you have a better solution then great.

I said display not what it does. I think you answered that already. I believe your for statement isn't working correctly. Please do what prior message did and report back.
dbrs_helpdeskAuthor Commented:
It now says if /i "jumpdrive"=="rep set drive=e: REP drive is unknown.  It then copies and unknown folder to my desktop with the proper information in it but the folder should be called test and it should be on my removable media drive called jumpdrive.
SteveGTRCommented:
It looks like the for statement is not formatted correctly. Looks like you want to search for REP, but you are picking up jumpdrive.

Can you post the output of the dir command for the removable drive?

Please cut and paste the output rather than retyping it back in.
SteveGTRCommented:
If you don't mind using WMIC this should work for you:
@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%

Open in new window

dbrs_helpdeskAuthor Commented:
The code is below
if /i "JUMPDRIVE"=="REP" set drive=E:
REP drive is unknown

JUMPDRIVE is the name of my usb key.
dbrs_helpdeskAuthor Commented:
I don't mind using the above code you have entered.  Where do I put my xcopy command?
SteveGTRCommented:
You'd put it after the FOUND label:
@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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dbrs_helpdeskAuthor Commented:
Thank you
antOnyk1ngCommented:
Although this is solved partly it doesn't solve the part of the original idea of being able to identify a removable drive by its name as in this eg he was calling his removable drive JUMPDRIVE and then assigning a drive letter (or determining it) and copying files to that drive.

This is exactly what I would like to do. I want to have various removable drive's with unique names and BAT files I can execute that would Xcopy the files as required by the BAT file to that specific removable drive.

Can someone help me.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft DOS

From novice to tech pro — start learning today.