# batch file to copy from usb- unknown drive letter



## c_robertson (Dec 22, 2009)

I have a usb that I copy files from to new computers. I do this all day and would like to make it quicker.
I will have an XP path and a Vista/7 path, but just worried about XP right now.
The problem is I don't know what drive letter the USB will be in. I thought there was a %thisdrive% switch of some time.

In addition it has spaces and I don't know how to replace spaces in the path. Here is an example;

copy f:\install\105 wallpaper\*.* c:\documents and settings\All users\Shared Docuemnts\Shared Pictures\

Summary;
Determine the USB drive letter?
bridge space issue?


----------



## TheOutcaste (Aug 8, 2007)

Welcome to TSG!

For paths with spaces, just surround them with double quotes.


```
Copy "F:\install\105 wallpaper\*.*" "C:\Documents and Settings\All Users\Shared Documents\Shared Pictures\"
```
You can also use the *AllUsersProfile* variable:

```
Copy "F:\install\105 wallpaper\*.*" "%AllUserProfile%\Shared Documents\Shared Pictures\"
```
If you assign the flash drive a letter, it will always have that letter when connected to that same system. That won't work for new systems though.

You can test each drive letter from D to Z until you find it, or you can use WMIC to list all removable drives and just test those. WMIC has to be installed first (it will install automatically when called) which takes a few seconds, and won't work on XP Home as it's not included, so checking each letter would probably be fastest. I included D so you could also check for a CD\DVD. You can add A and/or B if you want to include floppies
You can exit after finding it as this sample does, or continue checking (remove *&Goto:EOF*) if you might have more than one drive with the same folder to copy from.

```
For %%I In (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 "%%I:\Install\105 wallpaper" Copy "%%I:\install\105 wallpaper\*.*" "%AllUserProfile%\Shared Documents\Shared Pictures\"&Goto:EOF
```


```
For /F "tokens=1" %%I In ('wmic logicaldisk get DeviceID^,DriveType^|Findstr "2"') Do (
If Exist "%%I\Install\105 wallpaper" Copy "%%I\install\105 wallpaper\*.*" "%AllUserProfile%\Shared Documents\Shared Pictures\"
)
```
Jerry


----------



## Squashman (Apr 4, 2003)

I might be confusing this with something else, but I had written a batch file a couple months ago to query the registry to find a specific cd-writer because the computer had two of them. I think you should be able query the registry for this information.


----------



## Squashman (Apr 4, 2003)

Found the old post. This is what I did for someone a couple of months ago. Not sure if this will work for USB as well.


```
for /f "tokens=3 delims=\:" %%I in ('reg query hklm\system\mounteddevices^|findstr /C:"5C003F00"') do set CDROM=%%I:
```


----------



## TheOutcaste (Aug 8, 2007)

That's a great idea! You could get the ID string from one PC then check for that string, it would positively identify the device. You'd still have to verify that the device is connected, as the ID is always present in the registry for an installed device.

Found some problems with it when testing though that would make it more complicated, so unless you need to positively identify a particular flash drive, just checking the drive letter would be easier. Plus you'd have to edit the file if you switched to a difference device.
Very useful if you want to make sure you don't copy or delete data on the wrong flash drive because someone changed the drive letter, or it happens to get assigned the one your script uses.


 You need a longer search string. *5C003F00* is Unicode for *\?*, which on XP is present for all removable devices including CDs/DVDs.
 Different versions of Windows use different ID strings (but should be the same for all machines running that version), so you'd need to check what OS you were on, and have an ID string for each.
 Regedit sorts the display, so the Dos Devices line with the drive letter appears last. Reg Query reads the data from the registry unsorted, so a Volume ID could come after the drive letter. You'd have to check that the line also contains Dos Devices, and only set the variable when both conditions are true.
 I have two identical flash drives (M: and N:), here's the ID string from the Registry for XP Pro (XP MCE is identical) and Win 7.
I only have one real system with Win 7 installed, so can't confirm that a different machine will assign the same ID string.
I converted the Unicode Hex digits to text for readability.

```
XP:
M:=\??\STORAGE#RemovableMedia[COLOR=Blue][B]#[/B][/COLOR]7&1[COLOR=Red][B]bb594c5[/B][/COLOR]&0&RM[COLOR=Blue][B]#[/B][/COLOR]{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}
N:=\??\STORAGE#RemovableMedia[COLOR=Blue][B]#[/B][/COLOR]7&1[COLOR=Red][B]f309c80[/B][/COLOR]&0&RM[COLOR=Blue][B]#[/B][/COLOR]{53f5630d-b6bf-11d0-94f2-00a0c91efb8b}

Win 7:
M=_??_USBSTOR#Disk&Ven_Kingston&Prod_DataTraveler_20&Rev_100[COLOR=Blue][B]#[/B][/COLOR]0010000000000000000000[COLOR=Red][B]F5[/B][/COLOR]&0[COLOR=Blue][B]#[/B][/COLOR]{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
N:=_??_USBSTOR#Disk&Ven_Kingston&Prod_DataTraveler_20&Rev_100[COLOR=Blue][B]#[/B][/COLOR]0010000000000000000000[COLOR=Red][B]AD[/B][/COLOR]&0[COLOR=Blue][B]#[/B][/COLOR]{53f56307-b6bf-11d0-94f2-00a0c91efb8b}
```
The part that is unique to each device is between the # symbols.
On XP you could probably just use Findstr to look for the part that is unique. You have to actually, or the search string becomes too long.
On Win 7, there was only a 2 character (8 hex character) difference. You'd need to filter for *USBSTOR*, then either the *Ven* or *Prod* identifier (or both), then search for the unique ID part

For the curious, here's the Hex code from XP, what you get from the reg query command.

```
M:=5c003f003f005c00530054004f0052004100470045002300520065006d006f007600610062006c0065004d0065006400690061002300370026003100[COLOR=Red][B]6200620035003900340063003500[/B][/COLOR]26003000260052004d0023007b00350033006600350036003300300064002d0062003600620066002d0031003100640030002d0039003400660032002d003000300061003000630039003100650066006200380062007d00
N:=5c003f003f005c00530054004f0052004100470045002300520065006d006f007600610062006c0065004d0065006400690061002300370026003100[B][COLOR=Red]6600330030003900630038003000[/COLOR][/B]26003000260052004d0023007b00350033006600350036003300300064002d0062003600620066002d0031003100640030002d0039003400660032002d003000300061003000630039003100650066006200380062007d00
```
And this is what a DVD drive (IDE) and a Virtual CD\DVD drive (SCSI) are listed as:

```
\??\IDE#CdRom_NEC_DVD+-RW_ND-3530A___________________102B____#
\??\SCSI#CdRom&Ven_Generic&Prod_DVD-ROM&Rev_1.0#
```
Easy way to read these ID strings in English, export the *HKLM\System\MountedDevices* key from the registry, but change the *Save as Type:* box to *.txt* from *.reg*
Open in Notepad++
Use the column editor to remove the hex codes from the left of the ASCII representation
Join the lines
Remove the space added at the join (if any)
Replace *...* with *[email protected]@@.* (Prevents removing a text period)
Remove the periods
Replace *@@@* with *.*


----------



## c_robertson (Dec 22, 2009)

I found that if the batch file is ON the USB stick, all I have to do is use the "\path\path\filename.ext" or "\path\"*.* to acomplish what I need
The destination is all in " "
The problem with the quotes was the drive letter. If it was a variable, could I include it in the quotes?

What this does;
I use the batch file to install these wallpapers to the shared directory (doesn't change)
add/merge the registry settings for wallpaper and screensaver and tada!
I did this so my spouse could prepare new computer with our custom wallpaper/screensavers.

Thanks to everyone for your great replies. I'm sure I will use them in the future.


----------



## TheOutcaste (Aug 8, 2007)

Yes, you can put the drive letter in the quotes.

If the batch file is on the USB stick, you can use *%~dp0* to refer to the *d*rive and *p*ath to the folder the batch file is in. (Zero, not Oh)
So this would work in that case:

```
copy "%~dp0install\105 wallpaper\*.*" "C:\Documents and Settings\All Users\Shared Documents\Shared Pictures\"
```
Assuming the batch file is at *X:\batch.bat*
*%~dp0* would be *X:\* in that case, *X:* for the drive, and *\* for the path (meaning Root).
That could be used to autorun the file when the USB drive is plugged in (if it hasn't been disabled), though I know very little about setting up autorun.


----------

