Batch to Detect flashdrive in Vista

Score
0
Vote Up
December 27, 2008 at 09:16:20 Pacific
Specs: XP Sp3, centrino 4gb

I recently posted a question about detecting a plugged flashdrive by means of a specific file that is located on the flashdrive. After some more research I found this line of code:

:: Test folder exists
@echo off
for %%p 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 if exist %%p:\Testfolder\ set w=%%p
echo Your drive is: %w%
PAUSE

This works perfectly on windows XP but when I run it on Vista I get an error message every time it finds a drive with no media inside it (for example an empty cardreader).
The popup window shows the following:

Exception Processing Message 0xc0000013 Parameters.....and so on.

Every time one shows up they halt the running batch file until action is taken. Is it possible to automatically ignore these errors or to suppress them?


Reply ↓  Report •


#1
Vote Down
Score
0
Vote Up
December 28, 2008 at 04:15:04 Pacific

::flashdisk.bat
@echo off & setlocal enabledelayedexpansion

for /f "tokens=1,2 delims=\ " %%a in ('fsutil fsinfo drives^|more/e/t0') do (
if "%%b"=="" (set drive=%%a) else (set drive=%%b)
fsutil fsinfo drivetype !drive!|find "Removable Drive">nul 2>&0 &&echo Your drive is: !drive!
)

this one works on XP, i dont have vista machine to try the code


Reply ↓  Report •

#2
Vote Down
Score
0
Vote Up
December 28, 2008 at 04:48:10 Pacific

Thanks reno. But it does not work on Vista, it just gives back a blank where the driveletter should be.

Also when I have multiple flashdrives inserted at the same time, it tells me the driveletters of all of them. Not only the drive with the specific file to recognize it. Any solutions?


Reply ↓  Report •

#3
Vote Down
Score
0
Vote Up
December 28, 2008 at 05:42:23 Pacific

dont know if this works on vista.

type this line in command prompt:
wmic logicaldisk get deviceid,description|find /i "removable"


Reply ↓  Report •

#4
Vote Down
Score
0
Vote Up
December 28, 2008 at 08:37:12 Pacific

I found the problem. I use a different OS language than English for my vista PC. That is why it cannot find "removable". It does work if I put “verwisselbare” (which means removable) in the script. Thanks

However I still get an errormessage when the batch tries to access a drive with no media inside it. XP just ignores it when there is no media. Is there a way the let Vista ignore the drives with no media inside?


Reply ↓  Report •

Related Posts

#5
Vote Down
Score
0
Vote Up
December 28, 2008 at 09:54:22 Pacific

The drive is specified as "Scandisk Cruzer USB Device" maybe it is possible to search for that and then extract a driveletter from it.

Reply ↓  Report •

#6
Vote Down
Score
0
Vote Up
December 28, 2008 at 17:29:16 Pacific

I would try to make a couple of small changes to what you already have

try changing


%%p:\Testfolder\

to

"%%p:\Testfolder\*"


Failing that I posted a batch that finds info like you mentioned above in your last thread, does it by any chance work in vista?

If so it may be possible to modify it to suit your needs by removing the menu style and simple selecting the drive that matches your text string. Just be aware it probably won't work on locked down systems because it uses reg.exe.

As you may tell I also don't have a vista machine to test on.....


Reply ↓  Report •

#7
Vote Down
Score
0
Vote Up
December 29, 2008 at 00:48:45 Pacific

Indeed, I have been playing around with that code you gave me. But Vista has a slightly different reg buildup. There is no "parentidprefix" in the keys, only the friendly name is available.

I tried modifying the code to search for a specific friendlyname, and couple it a driveletter but I'm afraid the buildup is a bit above my level of understanding :P


Reply ↓  Report •

#8
Vote Down
Score
0
Vote Up
December 29, 2008 at 01:25:16 Pacific

The parentidprefix was how I found the drive letter so without it something else must be used, unless the drive letter is available in other ways in vista.

I got the drive letter by partially converting the hex data from the values \dosdevices\x: at
HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices to ascii, where x is the drive letter. That hex data contained the parentidprefix in xp which I matched against the parentidprefix from HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USBSTOR. so my guess would be to see if any unique identifiers are contained in the hex data in vista. Just be aware that every second byte was 00 in xp, so every second byte may need to be removed, then converted to ascii.


Reply ↓  Report •

#9
Vote Down
Score
0
Vote Up
December 29, 2008 at 02:49:02 Pacific

My flashdrive is called HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Enum\USBSTOR\Disk&Ven_SanDisk&Prod_Cruzer&Rev_8.02\0876431B074195EE&0
in both Vista and XP.

The sequences: 0876431B074195EE&0 and Disk&Ven_SanDisk&Prod_Cruzer&Rev_8.02 seem to be constant and can also be found in the hex code in mounted devices(only to be found in vista), in this case it is J:

"\\DosDevices\\J:"=hex:5f,00,3f,00,3f,00,5f,00,55,00,53,00,42,00,53,00,54,00,\
4f,00,52,00,23,00,44,00,69,00,73,00,6b,00,26,00,56,00,65,00,6e,00,5f,00,53,\ 00,61,00,6e,00,44,00,69,00,73,00,6b,00,26,00,50,00,72,00,6f,00,64,00,5f,00,\
43,00,72,00,75,00,7a,00,65,00,72,00,26,00,52,00,65,00,76,00,5f,00,38,00,2e,\
00,30,00,32,00,23,00,30,00,38,00,37,00,36,00,34,00,33,00,31,00,42,00,30,00,\ 37,00,34,00,31,00,39,00,35,00,45,00,45,00,26,00,30,00,23,00,7b,00,35,00,33,\
00,66,00,35,00,36,00,33,00,30,00,37,00,2d,00,62,00,36,00,62,00,66,00,2d,00,\ 31,00,31,00,64,00,30,00,2d,00,39,00,34,00,66,00,32,00,2d,00,30,00,30,00,61,\
00,30,00,63,00,39,00,31,00,65,00,66,00,62,00,38,00,62,00,7d,00

Is it possible to use that as an unique ID?


Reply ↓  Report •

#10
Vote Down
Score
0
Vote Up
December 29, 2008 at 03:35:22 Pacific

[edit: my tabs were being converted to space - if you already copied the script copy it again
+ Minor change ``]

Although the tab appear to be fine in the post every time i copy it there seems to be a problem. Please check the for loop directly below the line "set lcnt=" have delimiters of <backslash><tab><space>.

Try this, but please understand I can't test on vista so you may have to give me feedback if there are any problems....

My intention with this is for it to work on both xp and vista.......

Sorry, I think I may be a little absorbed in this, your information should be enough as I think the "0876431B074195EE&0" will be the one to use because the first one may be a device id and may be the same one two identical devices.


@echo off&setlocal
set hexval=""................................ !"#$.&...*+,-./0123456789:;.=.?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]._`abcdefghijklmnopqrstuvwxyz{.}~.................................................................................................................................""
:begin
SETLOCAL ENABLEDELAYEDEXPANSION
cls
echo.
echo.
echo.
echo.
echo.
echo: Scanning for usb flash drives.......
set dcnt=
ver|find "n 5.1">nul&&goto xptype
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 flshtmp="%%b"
set flshtmp=!flshtmp:%%a\=!
set /a dcnt+=1
set chk=
for /f "tokens=1,2*" %%c in ('reg query "%%b"^|find /i "FriendlyName"') do (
set fname!dcnt!="%%e"&&set /a chk+=1
set id!dcnt!=!flshtmp!
set flshtmp=
)
if !chk! neq 1 set /a dcnt-=1
)
)
goto merge

:xptype
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
)
)

:merge
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



Reply ↓  Report •

#11
Vote Down
Score
0
Vote Up
December 29, 2008 at 16:23:33 Pacific

I just realised what is wrong with the original batch, hopefully dir won't invoke the same messages on vista. Regardless I would still like to get the one above working in vista...

:: Test folder exists
@echo off
for %%p 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 (
dir %%p:\ 2>nul 1>&2&&if exist %%p:\Testfolder\ set w=%%p
)
echo Your drive is: %w%
PAUSE


Reply ↓  Report •

#12
Vote Down
Score
0
Vote Up
December 31, 2008 at 04:19:58 Pacific

The dir version works great on vista! :D.
No errors at all!

The large "scanning for flashdrive" version still has a few bugs in it because now it doesn't work in either XP or vista.

Reply ↓  Report •

#13
Vote Down
Score
0
Vote Up
December 31, 2008 at 06:10:12 Pacific

I edited the large flash drive scanner a couple of times the same day I posted it so you may have it without the little fix a put in it. I can assure you it does still work in xp, the problem comes from coping it and pasting it. Copy it into a text file then manually edit the line shown below:

for /f "tokens=2,4 delims=\ " %%g in ('reg query HKEY_LOCAL_MACHINE\SYSTEM\MountedDevices^|find ":"') do (

For some reason the tab does not copy into a text file properly. What you will need to do is manually edit the script as follows. Delete everthing between = (equals) and " (double quote) now add in a \ (backslash) followed buy a tab then a space, the order is very important! It should now work under xp, who knows it may even work in vista.

It's a real pain but if it won't copy properly, then what can we do?

Glade to here that the other version works.

Can you try the edit and post back on weather it works on vista or not?


Reply ↓  Report •

#14
Vote Down
Score
0
Vote Up
December 31, 2008 at 08:03:08 Pacific

Judago, I'm glad to tell you that the large script is Vista proof! :D It worked perfectly. Nice job.

Happy new year! Cheers!


Reply ↓  Report •

#15
Vote Down
Score
0
Vote Up
December 31, 2008 at 15:38:19 Pacific

Happy new year!

Reply ↓  Report •

Reply to Message Icon Start New Discussion
« Cannot access File filename character remova... »

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

Ask the Community!
Describe your Problem
Example: Hard Drive Not Detected on My PC