Batch files and USB ports

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
HenryDara
Posts: 1
Joined: 29 Nov 2011 09:20

Batch files and USB ports

#1 Post by HenryDara » 29 Nov 2011 09:26

Is there a way to determine the presence of a USB stick, as a condition to perform a copy action to the stick. The trouble I am having is if the stick is not present, windows CE creates a folder with the name of the port automatically.

here is the code:

echo off
echo **********************************************
echo * Check for target directory on USB stick *
echo * and create it if it doesn't exist. *
echo **********************************************
IF NOT EXIST "\USB Storage\CFS\Config" MD "\USB Storage\CFS\Config"

echo **********************************************
echo * Copy files from Storage Card to USB *
echo **********************************************
cd \USB Storage\CFS\Config
IF ERRORLEVEL 0 copy "\Storage Card\CFS\Config\*.csv"

cd \
echo **********************************************
echo * Check for target directory on USB stick *
echo * and create it if it doesn't exist. *
echo **********************************************
IF NOT EXIST "\USB Storage\CFS\Recipe" MD "\USB Storage\CFS\Recipe"

echo **********************************************
echo * Copy files from Storage Card to USB *
echo **********************************************
cd \USB Storage\CFS\Recipe
IF ERRORLEVEL 0 copy "\Storage Card\CFS\Recipe\*.csv"

cd \
echo **********************************************
echo * Check for target directory on USB stick *
echo * and create it if it doesn't exist. *
echo **********************************************
IF NOT EXIST "\USB Storage\CFS\Service" MD "\USB Storage\CFS\Service"

echo **********************************************
echo * Copy files from Storage Card to USB *
echo **********************************************
cd \USB Storage\CFS\Service
IF ERRORLEVEL 0 copy "\Storage Card\CFS\Service\*.csv"

cd \
echo on
exit

Ed Dyreen
Expert
Posts: 1569
Joined: 16 May 2011 08:21
Location: Flanders(Belgium)
Contact:

Re: Batch files and USB ports

#2 Post by Ed Dyreen » 05 Dec 2011 16:33

'
similar question and solution here viewtopic.php?f=3&t=2584&start=0

I have been playing with "mountvol.EXE"

Code: Select all

   \\?\Volume{908a0d13-d21b-11e0-ba1a-806d6172696f}\
       F:\

   \\?\Volume{174cf938-e1c4-11e0-a5eb-005056c00008}\
       X:\

   \\?\Volume{e306a64d-e1c8-11e0-a5ee-005056c00008}\
       G:\
Why can't you simply if exist "$drive:\", I don't understand :?:

orange_batch
Expert
Posts: 442
Joined: 01 Aug 2010 17:13
Location: Canadian Pacific
Contact:

Re: Batch files and USB ports

#3 Post by orange_batch » 06 Dec 2011 00:20

If your USB drive is always on the same drive letter, check for the existence of the drive letter. If you need to confirm that it is the USB stick and not something else, if you have a persistent unique file on it, you could simply check for that file on that drive letter.

But if you need to make sure it is an existing USB drive, you can identify USB drive letters...

1. WMI(C) can probably identify USB drives, so a Google search to get started...

2. http://msdn.microsoft.com/en-us/library ... 85%29.aspx "...detect which drive letter is associated with a logical disk partition?"

3. Voila, my condensed WMI vbscript for USB drives. (Modify as desired, such as "Found "& can be removed.)

usb_drives.vbs

Code: Select all

set a=getobject("winmgmts:")
set b=a.execquery("select deviceid from win32_diskdrive where interfacetype='USB'")
for each c in b
set d=a.execquery("associators of {win32_diskdrive.deviceid='"&c.deviceid&"'} where assocclass=win32_diskdrivetodiskpartition")
for each e in d
set f=a.execquery("associators of {win32_diskpartition.deviceid='"&e.deviceid&"'} where assocclass=win32_logicaldisktopartition")
for each g in f
wscript.echo "Found "&g.deviceid
next
next
next

batch:

Code: Select all

cscript usb_drives.vbs //nologo

As usual, can be used like for /f "delims=" %%a in ('cscript usb_drives.vbs //nologo') do ...

Post Reply