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

check for drive letter before copying files.

3 views
Skip to first unread message

shrpshtr

unread,
Apr 12, 2007, 1:23:04 AM4/12/07
to
hey all,

script newbie here and i am banging my head against my keyboard trying
to figure this one out. i am sure it is easy to all of the guru's
here. what i am trying to do is check to see if an assigned drive
letter (i: in this case) exists before i allow a script to copy data
to it. if it doesn't i want the script to continue as planned, if it
doesn't i want the script to prompt the end user to insert his/her usb
drive. i know there has to be a way to do it so any help would be
greatly appreciated. thanks in advance...

here is what i have so far.

Dim objfso, msg, i

drv = "i"
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.DriveExists(drv) Then
End If
Set i = objfso.GetDrive(drv)
If i.IsReady Then
Else
msg = "Please plug USB drive into laptop..."
End If
Else
msg = "Please plug USB drive into laptop..."
End If
Set objShell = CreateObject("Wscript.Shell")
objshell.Popup msg, AUTO_ANSWER, "Insert USB Drive", OK_Button

shrpshtr

unread,
Apr 12, 2007, 9:43:58 AM4/12/07
to

Sorry for the rush I am trying to finish this up and can't seem to pry
myself away from the computer. PLEEAAASSSEEE HELP! tia.

GRAND_POOBAH

unread,
Apr 12, 2007, 9:51:44 AM4/12/07
to
The only thing I can see is the extra "End If" noted.

GP

--->


> hey all,
>
> script newbie here and i am banging my head against my keyboard trying
> to figure this one out. i am sure it is easy to all of the guru's
> here. what i am trying to do is check to see if an assigned drive
> letter (i: in this case) exists before i allow a script to copy data
> to it. if it doesn't i want the script to continue as planned, if it
> doesn't i want the script to prompt the end user to insert his/her usb
> drive. i know there has to be a way to do it so any help would be
> greatly appreciated. thanks in advance...
>
> here is what i have so far.
>
>
>
> Dim objfso, msg, i
>
> drv = "i"
> Set objfso = CreateObject("Scripting.FileSystemObject")
> If objfso.DriveExists(drv) Then

> End If <------------------------------------------------------------ HERE

shrpshtr

unread,
Apr 12, 2007, 10:04:12 AM4/12/07
to
On Apr 12, 9:51 am, GRAND_POOBAH
> > objshell.Popup msg, AUTO_ANSWER, "Insert USB Drive", OK_Button- Hide quoted text -
>
> - Show quoted text -

Thanks for the response. What I was trying to accomplish is for the
script to continue without any notification if the the drive exists.
I only want to be prompted if the drive letter doesn't exist. If I
remove that END IF it prompts me with a blank echo/msgbox and I have
to click ok. I don't want any user interaction unless the drive
doesn't exist. Any other ideas? Thanks ...

GRAND_POOBAH

unread,
Apr 12, 2007, 10:19:16 AM4/12/07
to

--->

Your logic might go like this (this can be simplified with multiple
logic statements):

IF (drive exists) Then
IF (drive is ready) Then
' Do nothing - EXIT
ELSE
' wait until drive is ready
ENDIF
ELSE
' Tell OP to plug the drive in; and ...
' wait until drive is ready
ENDIF

GP

shrpshtr

unread,
Apr 12, 2007, 10:38:55 AM4/12/07
to
On Apr 12, 10:19 am, GRAND_POOBAH
> GP- Hide quoted text -

>
> - Show quoted text -

Thanks for your continued help. Here is the edited script with the
logic you mentioned. It looks good but I get an invalid "exit"
statement. We're almost there, thanks!

Dim objfso, msg, i
drv = "i"
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.DriveExists(drv) Then

Set i = objfso.GetDrive(drv)
If i.IsReady Then

EXIT

GRAND_POOBAH

unread,
Apr 12, 2007, 10:47:51 AM4/12/07
to

--->

Sorry - the EXIT was pseudocode - what you need to do there is either
delete the "EXIT" and let the logic take over, or add some sort of jump
or Return out of the entire routine.

GP

shrpshtr

unread,
Apr 12, 2007, 11:04:30 AM4/12/07
to
On Apr 12, 10:19 am, GRAND_POOBAH
<iss_boss.del...@delete.sbcglobal.net> wrote:
> GP- Hide quoted text -

>
> - Show quoted text -

I thought I already responded but it isn't showing my last post so
here goes again. I apologize in advance if this comes up as a
duplicate. Following the logic you listed here is the script the way
I have it now. I am getting and invalid EXIT error when I try to run
it. Thanks for your continued efforts.

Dim objfso, msg, i
drv = "i"
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.DriveExists(drv) Then

Set i = objfso.GetDrive(drv)
If i.IsReady Then

EXIT

GRAND_POOBAH

unread,
Apr 12, 2007, 11:23:44 AM4/12/07
to
Sorry - the EXIT was pseudocode - what you need to do there is either
delete the "EXIT" and let the logic take over, or add some sort of jump
or Return out of the entire routine.

GP

--->

Jay

unread,
Apr 12, 2007, 5:41:40 PM4/12/07
to
You could also use WScript.quit if you just want it to quit out of the
script.

Jason

"GRAND_POOBAH" <iss_bos...@delete.sbcglobal.net> wrote in message
news:udmCBGRf...@TK2MSFTNGP03.phx.gbl...

shrpshtr

unread,
Apr 12, 2007, 9:24:03 PM4/12/07
to
On Apr 12, 5:41 pm, "Jay" <jgrei...@schoolcraft.edu> wrote:
> You could also use WScript.quit if you just want it to quit out of the
> script.
>
> Jason
>
> "GRAND_POOBAH" <iss_boss.del...@delete.sbcglobal.net> wrote in message

GP,

If I let the logic handle it I get a blank popup/msgbox message that I
have to click ok on. I want to skip past the rest of that part of the
routine and let the script finish. I am not familiar enough with VB
script to know what code to use for that functionality. Thanks again
for all of your help thus far.

Jay,

I don't want to quit the script but that info you provided will be put
to good use in other scripts. Thanks. Any other responses will be
greatly appreciated.

Ryan

Michael Bednarek

unread,
Apr 13, 2007, 1:39:58 AM4/13/07
to
On 12 Apr 2007 08:04:30 -0700, shrpshtr wrote in
microsoft.public.scripting.vbscript:

[snip]


>I thought I already responded but it isn't showing my last post so
>here goes again. I apologize in advance if this comes up as a
>duplicate. Following the logic you listed here is the script the way
>I have it now. I am getting and invalid EXIT error when I try to run
>it. Thanks for your continued efforts.
>
>Dim objfso, msg, i
>drv = "i"
>Set objfso = CreateObject("Scripting.FileSystemObject")
>If objfso.DriveExists(drv) Then
> Set i = objfso.GetDrive(drv)
> If i.IsReady Then
> EXIT
> Else
> msg = "Please plug USB drive into laptop..."
> End If
> Else
> msg = "Please plug USB drive into laptop..."
> End If
> Set objShell = CreateObject("Wscript.Shell")
> objshell.Popup msg, AUTO_ANSWER, "Insert USB Drive", OK_Button

Either, make the line "EXIT" a comment (by preceding it with a single
quote), or remove it altogether, or simplify the logic:

If Not objfso.DriveExists(drv) Or Not objfso.GetDrive(drv).IsReady


Set objShell = CreateObject("Wscript.Shell")

objshell.Popup "Please plug USB drive into laptop...", _


AUTO_ANSWER, "Insert USB Drive", OK_Button

End If

--
Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

shrpshtr

unread,
Apr 13, 2007, 10:32:52 AM4/13/07
to
> Michael Bednarek http://mbednarek.com/ "POST NO BILLS"- Hide quoted text -

>
> - Show quoted text -

Thanks for the response. I copied your script here and tried to get
it to work with no luck. I also tried editing it where I thought the
problems were, still no luck. Like I mentioned above, I get a blank
popup type message when the drive is present with the script I was
running (pasted above). I need to eliminate that positive
notification and I only want to be notified if the drive DOESN'T
exist. Please help. This thing can't be this hard as I am sure I am
making it much more difficult some how. Thanks again for all of the
responses. Here is a copy of the last thing I tried.

Dim objsfo, drv, i


drv = "i"
Set objfso = CreateObject("Scripting.FileSystemObject")

If Not objfso.DriveExists(drv) Or Not objfso.GetDrive(drv).IsReady

Then
Set i = objfso.GetDrive(drv)

'End If


Set objShell = CreateObject("Wscript.Shell")

nate...@gmail.com

unread,
Apr 13, 2007, 1:59:27 PM4/13/07
to

'first thing check the drive status, don't continue until it
'becomes available.

Do Until DriveStatus = True
DriveStatus = CheckDrive("i")
Loop

'some code down here to do some more stuff


'This function at the end of your code
Function CheckDrive(sDriveLetter)


Set objfso = CreateObject("Scripting.FileSystemObject")

If objfso.DriveExists(sDriveLetter) Then
Set i = objfso.GetDrive(sDriveLetter
If Not i.IsReady Then Wscript.Echo "Please plug USB drive into
laptop..."
CheckDrive = False
Else
Wscript.Echo "Please plug USB drive into laptop..."
CheckDrive = False
End If

CheckDrive = True
End Function


Hope this helps. As always watch for line wrapping.

-Nate
http://www.naterice.com

shrpshtr

unread,
Apr 16, 2007, 4:37:51 PM4/16/07
to
> -Natehttp://www.naterice.com- Hide quoted text -

>
> - Show quoted text -

For some reason my replies aren't showing up in this group...:(

Thanks for the help with the script. It looks like the script above
will do what I want it to but I am getting an error when I try to run
it. It tells me that it is "Expected IF" state between the End &
Function of the last line of code. Any help? Tia...

Shenan Stanley

unread,
Apr 16, 2007, 5:23:38 PM4/16/07
to
shrpshtr wrote:
> For some reason my replies aren't showing up in this group...:(

Check your newsreader settings and what newsgroup server are you connecting
to?

Entire thread:
http://groups.google.com/group/microsoft.public.scripting.vbscript/browse_thread/thread/711310a35bd0c1b3/676953b59b936cca?lnk=st&q=&rnum=1#676953b59b936cca

--
Shenan Stanley
MS-MVP
--
How To Ask Questions The Smart Way
http://www.catb.org/~esr/faqs/smart-questions.html


Paul Randall

unread,
Apr 16, 2007, 6:03:54 PM4/16/07
to

"shrpshtr" <shrp...@gmail.com> wrote in message
news:1176755871.5...@b75g2000hsg.googlegroups.com...

Did you see this clue?


>>
>> Hope this helps. As always watch for line wrapping.
>>

I think you will see that line wrapping did occur here:


>> If Not i.IsReady Then Wscript.Echo "Please plug USB drive into
>> laptop..."

That is supposed to be one line, not two. Some newsreaders don't handle
long lines well.

-Paul Randall


shrpshtr

unread,
Apr 17, 2007, 9:31:40 AM4/17/07
to
On Apr 16, 6:03 pm, "Paul Randall" <paulr...@cableone.net> wrote:
> "shrpshtr" <shrps...@gmail.com> wrote in message
> >> -Natehttp://www.naterice.com-Hide quoted text -

>
> >> - Show quoted text -
>
> > For some reason my replies aren't showing up in this group...:(
>
> > Thanks for the help with the script. It looks like the script above
> > will do what I want it to but I am getting an error when I try to run
> > it. It tells me that it is "Expected IF" state between the End &
> > Function of the last line of code. Any help? Tia...
>
> Did you see this clue?
>
> >> Hope this helps. As always watch for line wrapping.
>
> I think you will see that line wrapping did occur here:
>
> >> If Not i.IsReady Then Wscript.Echo "Please plug USB drive into
> >> laptop..."
>
> That is supposed to be one line, not two. Some newsreaders don't handle
> long lines well.
>
> -Paul Randall- Hide quoted text -

>
> - Show quoted text -

This is the text I have in this script. I checked the line wrapping
and it appears correct. Please help...I think I just bit a pen in 1/2
trying to figure this out. LOL!

'first thing check the drive status, don't continue until it
'becomes available.

Do Until DriveStatus = True
DriveStatus = CheckDrive("i")
Loop


'some code down here to do some more stuff


'This function at the end of your code
Function CheckDrive(sDriveLetter)
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.DriveExists(sDriveLetter) Then

Set i = objfso.GetDrive(sDriveLetter)

Brian Wolven

unread,
Apr 17, 2007, 10:13:36 AM4/17/07
to
shrpshtr wrote:

> This is the text I have in this script. I checked the line wrapping
> and it appears correct. Please help...I think I just bit a pen in 1/2
> trying to figure this out. LOL!
>
> 'first thing check the drive status, don't continue until it
> 'becomes available.
>
> Do Until DriveStatus = True
> DriveStatus = CheckDrive("i")
> Loop
>
>
> 'some code down here to do some more stuff
>
>
> 'This function at the end of your code
> Function CheckDrive(sDriveLetter)
> Set objfso = CreateObject("Scripting.FileSystemObject")

> If objfso.DriveExists(sDriveLetter) Then

> Set i = objfso.GetDrive(sDriveLetter)

> If Not i.IsReady Then

> Wscript.Echo "Please plug USB drive into laptop..."
> CheckDrive = False

> Else

> Wscript.Echo "Please plug USB drive into laptop..."
> CheckDrive = False

> End If
>
> CheckDrive = True
> End Function

You have two if/then's up there, but only one else/endif. Programming
languages don't much like that kind of thing. Try adding another "End
If". ;)

Editors with syntax highlighting can be useful, too!

shrpshtr

unread,
Apr 17, 2007, 11:46:01 AM4/17/07
to
> Editors with syntax highlighting can be useful, too!- Hide quoted text -

>
> - Show quoted text -

Thanks for the response Brian. Here is the corrected script with the
appropriate (I think) # of if/end if statements. Now I am getting a
"Device Unavailble" message. AGGGHHHHH!

'first thing check the drive status, don't continue until it
'becomes available.

Do Until DriveStatus = True
DriveStatus = CheckDrive("i")
Loop


'some code down here to do some more stuff


'This function at the end of your code
Function CheckDrive(sDriveLetter)
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.DriveExists(sDriveLetter) Then
Set i = objfso.GetDrive(sDriveLetter)

End If

Brian Wolven

unread,
Apr 17, 2007, 2:06:18 PM4/17/07
to
shrpshtr wrote:
>
> Thanks for the response Brian. Here is the corrected script with the
> appropriate (I think) # of if/end if statements. Now I am getting a
> "Device Unavailble" message. AGGGHHHHH!
>
> 'first thing check the drive status, don't continue until it
> 'becomes available.
>
> Do Until DriveStatus = True
> DriveStatus = CheckDrive("i")
> Loop

Shouldn't that be "i:"?

shrpshtr

unread,
Apr 17, 2007, 3:31:27 PM4/17/07
to

Didn't notice that. I changed it and am still getting the "Device
Unavailable" message. I must be an idiot...

'first thing check the drive status, don't continue until it
'becomes available.

Do Until DriveStatus = True
DriveStatus = CheckDrive("i:")
Loop

'some code down here to do some more stuff


'This function at the end of your code
Function CheckDrive(sDriveLetter)
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.DriveExists(sDriveLetter) Then
Set i = objfso.GetDrive(sDriveLetter)
End If

Set objfso = CreateObject("Scripting.FileSystemObject")

Set i = objfso.GetDrive("i:")

nate...@gmail.com

unread,
Apr 17, 2007, 4:36:45 PM4/17/07
to

Please copy and paste the code below but take note of the line that is
between the "all on one line" remarks.


'first thing check the drive status, don't continue until it
'becomes available.

Do Until DriveStatus = True
DriveStatus = CheckDrive("i")
Loop

'some code down here to do some more stuff

'This function at the end of your code
Function CheckDrive(sDriveLetter)
Set objfso = CreateObject("Scripting.FileSystemObject")
If objfso.DriveExists(sDriveLetter) Then
Set i = objfso.GetDrive(sDriveLetter

'vvvvvvvAll on one line vvvvv


If Not i.IsReady Then Wscript.Echo "Please plug USB drive into
laptop..."

'^^^^^^END All on one line^^^^^^


CheckDrive = False
Else
Wscript.Echo "Please plug USB drive into laptop..."
CheckDrive = False
End If

CheckDrive = True
End Function

-Nate
http://www.naterice.com

Paul Randall

unread,
Apr 17, 2007, 11:24:18 PM4/17/07
to

"shrpshtr" <shrp...@gmail.com> wrote in message
news:1176816699.6...@o5g2000hsb.googlegroups.com...

As I said earlier, the problem is here:


>> If Not i.IsReady Then Wscript.Echo "Please plug USB drive into
>> laptop..."

This statement was supposed to be all on one line.

You changed it to two lines which has incorrect syntax:


> If Not i.IsReady Then
> Wscript.Echo "Please plug USB drive into laptop..."

It could have been written as one physical long line or one logical line
split up into two physical lines with a continuation (underscore):
If Not i.IsReady Then _


Wscript.Echo "Please plug USB drive into laptop..."

Or it could have been written as three physical lines with an If ... Then
... Endif block:


If Not i.IsReady Then
Wscript.Echo "Please plug USB drive into laptop..."

End If


Michael Bednarek

unread,
Apr 18, 2007, 12:08:45 AM4/18/07
to
On 17 Apr 2007 12:31:27 -0700, shrpshtr wrote in microsoft.public.scripting.vbscript:

>On Apr 17, 2:06 pm, Brian Wolven <brian.wol...@domain.invalid> wrote:
>> shrpshtr wrote:
>>
>> > Thanks for the response Brian. Here is the corrected script with the
>> > appropriate (I think) # of if/end if statements. Now I am getting a
>> > "Device Unavailble" message. AGGGHHHHH!

This has been tested and works here:

01 Option Explicit
02
03 Dim objShell
04 Set objShell = CreateObject("Wscript.Shell")
05
06 Do Until CheckDrive("I") = True
07 objShell.Popup "Please plug USB drive into laptop...", 0, "Insert USB Drive", 48
08 Loop
09 WScript.Quit
10
11 Function CheckDrive(strDriveLetter)
12
13 Dim objFSO
14
14 Set objFSO = CreateObject("Scripting.FileSystemObject")
16 CheckDrive = False
17 If objFSO.DriveExists(strDriveLetter) Then If objFSO.GetDrive(strDriveLetter).IsReady Then CheckDrive = True
18
19 End Function

This code has 19 lines; the line numbers must be removed.
Line 07 is 82 characters long, line 17 is 108 characters long.

shrpshtr

unread,
Apr 23, 2007, 9:57:22 AM4/23/07
to
On Apr 18, 12:08 am, Michael Bednarek <ROT13...@gtz.pbz.nh> wrote:
> On 17 Apr 2007 12:31:27 -0700, shrpshtr wrote in microsoft.public.scripting.vbscript:
>
> >On Apr 17, 2:06 pm, Brian Wolven <brian.wol...@domain.invalid> wrote:
> >> shrpshtr wrote:
>
> >> > Thanks for the response Brian. Here is the corrected script with the
> >> > appropriate (I think) # of if/end if statements. Now I am getting a
> >> > "Device Unavailble" message. AGGGHHHHH!
>
> This has been tested and works here:
>
> 01 Option Explicit
> 02
> 03 Dim objShell
> 04 Set objShell = CreateObject("Wscript.Shell")
> 05
> 06 Do Until CheckDrive("I") = True
> 07 objShell.Popup "Please plug USBdriveinto laptop...", 0, "Insert USBDrive", 48

> 08 Loop
> 09 WScript.Quit
> 10
> 11 Function CheckDrive(strDriveLetter)
> 12
> 13 Dim objFSO
> 14
> 14 Set objFSO = CreateObject("Scripting.FileSystemObject")
> 16 CheckDrive = False
> 17 If objFSO.DriveExists(strDriveLetter) Then If objFSO.GetDrive(strDriveLetter).IsReady Then CheckDrive = True
> 18
> 19 End Function
>
> This code has 19 lines; the line numbers must be removed.
> Line 07 is 82 characters long, line 17 is 108 characters long.
>
> --
> Michael Bednarek http://mbednarek.com/ "POST NO BILLS"

Thanks for all of the great help everyone. I know it took an act of
congress just about but the script is functioning just fine. I really
appreciate it.

Ryan

0 new messages