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

0
 
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
0
 
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.
0
Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

 
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

0
 
SteveGTRCommented:
What does this line display?

echo %testfor% drive is %drive%
0
 
dbrs_helpdeskAuthor Commented:
it is to test for the drive that it finds.  If you have a better solution then great.
0
 
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.
0
 
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.
0
 
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.
0
 
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.
0
 
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

0
 
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.
0
 
dbrs_helpdeskAuthor Commented:
I don't mind using the above code you have entered.  Where do I put my xcopy command?
0
 
dbrs_helpdeskAuthor Commented:
Thank you
0
 
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.
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.