Link to home
Start Free TrialLog in
Avatar of dbrs_helpdesk
dbrs_helpdesk

asked on

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

Avatar of SteveGTR
SteveGTR
Flag of United States of America image

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
Avatar of dbrs_helpdesk
dbrs_helpdesk

ASKER

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

What does this line display?

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

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

JUMPDRIVE is the name of my usb key.
I don't mind using the above code you have entered.  Where do I put my xcopy command?
ASKER CERTIFIED SOLUTION
Avatar of SteveGTR
SteveGTR
Flag of United States of America image

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
Thank you
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.