Tom's Guide | Tom's Hardware | Tom's Games
![]() |
![]() |
![]() |
I want to use a batch to copy files to a plugged in flashdrive. How can I get the drive letter of the flashdrive? (I use different computers so the driveletter keeps changing)
I thought maybe the batch file can recognize a specific folder/file on the flashdrive to identify it.
I tried putting in a wildcard * for the driveletter but that didn't work.
xcopy "file.doc" " *:\destination\" /i /sDoes anyone know how to do this?

This will tell you the drives you currently have.
A:
IF errorlevel==0 ECHO A:>>temp.txt
B:
IF errorlevel==0 ECHO B:>>temp.txt
C:
IF errorlevel==0 ECHO C:>>temp.txt
D:
IF errorlevel==0 ECHO D:>>temp.txt
E:
IF errorlevel==0 ECHO E:>>temp.txt
F:
IF errorlevel==0 ECHO F:>>temp.txt
G:
IF errorlevel==0 ECHO G:>>temp.txt
H:
IF errorlevel==0 ECHO H:>>temp.txt
I:
IF errorlevel==0 ECHO I:>>temp.txt
J:
IF errorlevel==0 ECHO J:>>temp.txt
CLS
ECHO Available Drives:
TYPE temp.txtI only Batch if possible, 2000 more lines of code, oh well.

Thanks for that, but I'm for a more automatic approach.
To automatically copy a file to the flashdrive which contains a certain folder or other characteristic and not to the other drives available.

A:
IF EXIST A:\"file" COPY "stuff to copy" "place to copy to"
B:
IF EXIST B:\"file" COPY "stuff to copy" "place to copy to"
C:
IF EXIST C:\"file" COPY "stuff to copy" "place to copy to"
D:
IF EXIST D:\"file" COPY "stuff to copy" "place to copy to"
E:
IF EXIST E:\"file" COPY "stuff to copy" "place to copy to"
F:
IF EXIST F:\"file" COPY "stuff to copy" "place to copy to"
G:
IF EXIST G:\"file" COPY "stuff to copy" "place to copy to"
H:
IF EXIST H:\"file" COPY "stuff to copy" "place to copy to"
I:
IF EXIST I:\"file" COPY "stuff to copy" "place to copy to"
J:
IF EXIST J:\"file" COPY "stuff to copy" "place to copy to"
CLS
ECHO DONEI only Batch if possible, 2000 more lines of code, oh well.

The batch below may be of some interest to you, I wrote it a little while ago for a similar request. I can't give any warranties, it seems to work fine for me, but who knows.......
@echo off
set hexval=""................................ !"#$.&...*+,-./0123456789:;.=.?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]._`abcdefghijklmnopqrstuvwxyz{.}~.................................................................................................................................""
:begin
SETLOCAL ENABLEDELAYEDEXPANSION
cls
echo.
echo.
echo.
echo.
echo.
echo: Scanning for usb flash drives.......
set dcnt=
for /f "delims=" %%a in ('reg query HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USBSTOR^|find /i "HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USBSTOR\"') do (
for /f "delims=" %%b in ('reg query "%%a"^|find /i "%%a\"') do (
set /a dcnt+=1
set chk=
for /f "tokens=1,2*" %%c in ('reg query "%%b"^|findstr /i "ParentIdPrefix FriendlyName"') do (
if /i "%%c"=="ParentIdPrefix" set id!dcnt!="%%e"&&set /a chk+=1
if /i "%%c"=="FriendlyName" set fname!dcnt!="%%e"&&set /a chk+=1
)
if !chk! lss 2 set /a dcnt-=1
)
)
set lcnt=
for /f "tokens=2,4 delims=\ " %%g in ('reg query HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices^|find ":"') do (
set /a lcnt+=1
set drvltt!lcnt!=%%g
set drvdat!lcnt!=%%h
)
for /l %%i in (1,1,!lcnt!) do (
set nn=%%i
set tdat=
call :delin
)
goto cnvrt
:delin
if not defined drvdat%nn% set drvdat%nn%=%tdat:~1%&&goto :eof
set tdat=%tdat%¼!drvdat%nn%:~0,2!
set drvdat%nn%=!drvdat%nn%:~2!
goto delin
:cnvrt
set hexval=!hexval:~2,-2!
set svr=!hexval!
for /l %%j in (1,1,!lcnt!) do (
set drvdat%%j=!drvdat%%j:00¼=!
set drvdat%%j=!drvdat%%j:¼00=!
set nn=%%j
set hexval=!svr!
for %%k in (0 1 2 3 4 5 6 7 8 9 a b c d e f) do (
for %%l in (0 1 2 3 4 5 6 7 8 9 a b c d e f) do (
set byte=%%k%%l
call :chge
)
)
)
for /l %%m in (1,1,!lcnt!) do set drvdat%%m=!drvdat%%m:¼=!
goto check
:chge
set thex=!byte!=!hexval:~0,1!
set thex="!thex:&=^&!"
set thex=!thex:~1,-1!
set drvdat%nn%=!drvdat%nn%:%thex%!
set hexval=!hexval:~1!
goto :eof:check
set fcnt=
for /l %%n in (1,1,!dcnt!) do (
for /l %%o in (1,1,!lcnt!) do (
echo "!drvdat%%o!"|find !id%%n!>nul
if not errorlevel 1 (
dir !drvltt%%o!\>nul 2^>^&1
if not errorlevel 1 (
set /a fcnt+=1
set itmed!fcnt!=!drvltt%%o! !fname%%n!
)
)
)
)
set srtcnt=1
for %%q in (a b c d e f g h i j k l m n o p q r s t u v w x y z) do (
for /l %%r in (1,1,!fcnt!) do (
set vrcnt=%%r
call :srtr %%q
)
)
goto tchup
:srtr
if /i "%1"=="!itmed%vrcnt%:~0,1!" (
set output!srtcnt!=!itmed%vrcnt%!
set /a srtcnt+=1
)
goto :eof:tchup
set /a fcnt+=1
set output!fcnt!=Rescan to detect newly connected devices.
set /a fcnt+=1
set output!fcnt!=Exit
cls
:loop
echo.
echo.
echo: Your options are.....
echo.
for /l %%p in (1,1,!fcnt!) do echo: %%p. !output%%p!
echo.
set /p dest= Please select a flash drive or option:
for /l %%p in (1,1,!fcnt!) do if "%%p"=="%dest%" goto finish
cls
echo Invalid choice - select again
echo.
goto loop
:finish
if /i "!output%dest%!"=="exit" pause&&goto :eof
if /i "!output%dest%!"=="Rescan to detect newly connected devices." endlocal&&goto begin
set dest=!output%dest%:~0,2!
endlocal & set dest=%dest%
set hexval=
echo You selected drive %dest%
pause

![]() |
![]() |
![]() |

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.
| Ads by Google |