Computing.Net > Forums > Programming > Batch to search for flashdrive

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Batch to search for flashdrive

Reply to Message Icon

Name: mcel
Date: December 13, 2008 at 08:37:55 Pacific
OS: XP Sp3
CPU/Ram: centrine 4gb
Product: Hewlett-packard / 8510
Comment:

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

Does anyone know how to do this?



Sponsored Link
Ads by Google

Response Number 1
Name: BatchFreak
Date: December 13, 2008 at 09:46:05 Pacific
Reply:

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

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 2
Name: mcel
Date: December 13, 2008 at 10:21:07 Pacific
Reply:

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.


0

Response Number 3
Name: BatchFreak
Date: December 13, 2008 at 10:40:23 Pacific
Reply:

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 DONE

I only Batch if possible, 2000 more lines of code, oh well.


0

Response Number 4
Name: mcel
Date: December 13, 2008 at 11:45:52 Pacific
Reply:

Thanks, it worked :)


0

Response Number 5
Name: Judago
Date: December 14, 2008 at 02:27:25 Pacific
Reply:

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



0

Related Posts

See More



Response Number 6
Name: mcel
Date: December 27, 2008 at 05:09:16 Pacific
Reply:

Thanks


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

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


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Batch to search for flashdrive

shortcut to SEARCH - for files... www.computing.net/answers/programming/shortcut-to-search-for-files/16562.html

Batch to search and copy to new Dir www.computing.net/answers/programming/batch-to-search-and-copy-to-new-dir/16809.html

Scripts to search for a text in a view source www.computing.net/answers/programming/scripts-to-search-for-a-text-in-a-view-source/19961.html