Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Check if drive is present before running bat file

9,924 views
Skip to first unread message

WhiteTea77581

unread,
Jun 15, 2009, 9:31:11 AM6/15/09
to
I am trying to modify this to check first that J: drive exists.
(I am using TrueCrypt for a partition on a thumbdrive and the volume
may not have been mounted when this is run.)

Thanks,
Andy


:: Backup files from c: drive to thumbdrive j:
@echo off
if exist
c:
cd/
cd Backup
xcopy /d /y c:\Backup\lmtd_bks.zip j:\Backup
xcopy /d /y c:\Backup\wrdbckup.zip j:\Backup
xcopy /d /y c:\Job_Search\All_Resumes.zip i:\Job_Search
cd "C:\Documents and Settings\Administrator\Application Data\Mozilla
\Firefox\Profiles\gkpd58dg.default"
xcopy /d /y bookmarks.html j:\Backup

Andrew

unread,
Jun 15, 2009, 10:29:17 AM6/15/09
to
WhiteTea77581 schrieb:

> I am trying to modify this to check first that J: drive exists.
> (I am using TrueCrypt for a partition on a thumbdrive and the volume
> may not have been mounted when this is run.)


Hi,

Well, i do it like this. First i create a file on a stick like stick.dummy

Here�s the trick to "find" the driveletter of your stick

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:\stick.dummy SET LW=%%I

your batchfile would look like this then

:: Backup files from c: drive to thumbdrive j:
@echo off

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:\stick.dummy SET LW=%%I

c:
cd \Backup

xcopy /d /y c:\Backup\lmtd_bks.zip %LW%:\Backup
xcopy /d /y c:\Backup\wrdbckup.zip %LW%:\Backup

xcopy /d /y c:\Job_Search\All_Resumes.zip i:\Job_Search

cd "C:\Documents and Settings\Administrator\Application
Data\Mozilla\Firefox\Profiles\gkpd58dg.default"

xcopy /d /y bookmarks.html %LW%:\Backup

the only problem could be that your usbstick could be invisible if it
gets the same letter as a previously created networkdrive. Then you
would have to clear the assignment of that networkletter first (NET USE
(Networkletter:) /D

Andrew

WhiteTea77581

unread,
Jun 15, 2009, 2:55:57 PM6/15/09
to

When I tried you bat file, xcopy said it could not find the path.

I opened a cmd window and xcopy works in copying files from C: drive
to the thumbdrive.

Maybe there is a way to set an environmental variable that the bat
file could check for?
Or try to create a file on the thumbdrive and branch accordingly.

Andy

Ted Davis

unread,
Jun 15, 2009, 4:23:42 PM6/15/09
to
On Mon, 15 Jun 2009 06:31:11 -0700, WhiteTea77581 wrote:

> I am trying to modify this to check first that J: drive exists. (I am
> using TrueCrypt for a partition on a thumbdrive and the volume may not
> have been mounted when this is run.)

You posted from XP - if that is the OS of interest, then

if not exist j:\ goto :no_drive

with, of course, a lable :no_drive at the ehad of the code to execute if
the drive is not present, or at the end of the code to skip.

Note that the test fails if the drive is physically present, but has no
file system (an empty CD drive, for example) - it is not a test for the
drive; it is a test for a readable file system on the drive.

--
T.E.D. (tda...@mst.edu)

WhiteTea77581

unread,
Jun 15, 2009, 4:37:08 PM6/15/09
to

Thanks, I will try it.

Andy

Andrew

unread,
Jun 18, 2009, 12:03:52 PM6/18/09
to
WhiteTea77581 schrieb:

> On Jun 15, 8:29 am, Andrew <vnc_serv...@t-online.de> wrote:
>> WhiteTea77581 schrieb:
>> > I am trying to modify this to check first that J: drive exists.
>> > (I am using TrueCrypt for a partition on a thumbdrive and the volume
>> > may not have been mounted when this is run.)

> When I tried you bat file, xcopy said it could not find the path.

Those 2 Lines have to be one ! (without the >>
of course). Other than that i didn�t alter your batch.

>> 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:\stick.dummy SET LW=%%I

After reading your post again i�m not sure if you want to copy your
files onto your stick or on to that TrucryptVolume on that stick
If you want to copy it onto that TC Volume i see no way of looking for a
unmounted volume on a stick

regarding ...

If exist Drive:\ goto :whatever

i think this one is more bulletproof

If exist drive:\NUL goto :whatever


Andrew

Tim Meddick

unread,
Jun 19, 2009, 8:54:30 PM6/19/09
to
The rest of your batch file is okay but should start like this :


@echo off
if NOT EXIST j:\nul goto end

xcopy /d /y c:\Backup\lmtd_bks.zip j:\Backup

xcopy /d ........etc,

.............and end with this:

:end

...your 'cd' command is unnecessary at the beginning as you give the
whole paths in the first few 'xcopy' commands.

However, your next 'cd' command IS needed, as you change directory
*before* using the 'xcopy' command to copy a file [bookmarks.html] to
j:\backup

Be sure to include the line-label :end at the very end of your
script, then the line if NOT EXIST j:\nul goto end
will send the focus to this line and, being at the end, will exit if
it does not find dive j:

==

Cheers, Tim Meddick, Peckham, London. :-)


"WhiteTea77581" <andrewk...@peoplepc.com> wrote in message
news:6efd2f4a-b4c7-4b72...@x29g2000prf.googlegroups.com...

FileGod

unread,
Jun 19, 2009, 11:37:14 PM6/19/09
to
[Type your reply here]

>Thanks,
> Andy

I would use lables & use them pretty much like this:
if exist j: goto :found
goto :notfound
goto :end
:found


xcopy /d /y bookmarks.html j:\Backup

:notfound
:end

Sorry if the wordwrap is messed up, this is a fresh install...

http://www.filegod.netfirms.com

eitan...@gmail.com

unread,
Apr 3, 2013, 5:11:58 AM4/3/13
to
Hi all,
I tried to use this on my XP and it is not working for me:

@echo off
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:\updates.disk SET "LW=%%I" ELSE goto :nodrive
@echo ***************************
@echo * External drive is: %LW% *
@echo ***************************
pause
goto :end
:nodrive
@echo **************************
@echo * no external disk found *
@echo **************************
pause
goto :end
:end

if the file "updates.disk" is on my external drive, it works as expected, but if the file "updates.disk" is not there, I get:

***************************
* External drive is: *
***************************
Press any key to continue . . .

and the batch ends
The ELSE is not working...
any suggestions?
Thanks

Eitan

foxidrive

unread,
Apr 3, 2013, 5:16:41 AM4/3/13
to
Try this:

@echo off
set "LW="
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:\updates.disk SET "LW=%%I"
if defined LW (
echo ***************************
echo * External drive is: %LW% *
echo ***************************
) ELSE (
echo **************************
echo * no external disk found *
echo **************************
)
pause
goto :end
:end


--
foxi

eitan...@gmail.com

unread,
Apr 3, 2013, 6:21:17 AM4/3/13
to
Thanks foxidrive, it seams to work as expected

Mark

unread,
Apr 3, 2013, 2:24:30 PM4/3/13
to
I've been using this for years, especially on network drives.


REM Is Drive N: mapped?
set _ready=true
dir N: > "%temp%\tmp$$$.dir" 2>&1
find "The device is not ready" "%temp%\tmp$$$.dir" > nul
if %errorlevel% EQU 0 set _ready=false
find "The system cannot find the path specified" "%temp%\tmp$$$.dir" > nul
if %errorlevel% EQU 0 set _ready=false
if exist "%temp%\tmp$$$.dir" del "%temp%\tmp$$$.dir"

REM Continue if mapped
if "%_ready%"=="true" (
blah blah blah...
)

REM Complain if not mapped
if "%_ready%"=="false" (
blah blah blah...
)


--
<br>
<font size=1 color=blue face=Verdana>Mark</font>
<br>
<br>
<hr size="2" color=lightgray width=100%>
<br>

Stanley Daniel de Liver

unread,
Apr 4, 2013, 2:45:21 PM4/4/13
to
On Wed, 03 Apr 2013 19:24:30 +0100, Mark <no....@reply.here> wrote:

> I've been using this for years, especially on network drives.
>

Not that many, it requires NT or greater!

"if exist N:\nul" used to work in DOS/W98, but maybe not now (can't test
here, no local network)


>
> REM Is Drive N: mapped?
> set _ready=true
> dir N: > "%temp%\tmp$$$.dir" 2>&1
> find "The device is not ready" "%temp%\tmp$$$.dir" > nul
> if %errorlevel% EQU 0 set _ready=false
> find "The system cannot find the path specified" "%temp%\tmp$$$.dir" >
> nul
> if %errorlevel% EQU 0 set _ready=false
> if exist "%temp%\tmp$$$.dir" del "%temp%\tmp$$$.dir"
>
> REM Continue if mapped
> if "%_ready%"=="true" (
> blah blah blah...
> )
>
> REM Complain if not mapped
> if "%_ready%"=="false" (
> blah blah blah...
> )
>
>


--
It's a money /life balance.

foxidrive

unread,
Apr 4, 2013, 7:51:35 PM4/4/13
to
On 5/04/2013 5:45 AM, Stanley Daniel de Liver wrote:
> On Wed, 03 Apr 2013 19:24:30 +0100, Mark <no....@reply.here> wrote:
>
>> I've been using this for years, especially on network drives.
>>
> Not that many, it requires NT or greater!

NT has been around of more than 13 years. :)


> "if exist N:\nul" used to work in DOS/W98, but maybe not now (can't test
> here, no local network)

the folder\nul test is no longer reliable but if exist "folder\" is reliable.


--
foxi
0 new messages