PDA

View Full Version : Check to see if D drive is hard drive or CD ROM (W2K)



waqas501
05-19-2002, 03:10 AM
Hi. I am trying to write a small program that would first check to see if the 'D' drive on a computer is a harddrive or not. if so it would go to a section in the program and perform some installation commands on the 'D' drive. On the other hand if the 'D' drive does not exist or is a CD-Rom it would go to a different section and perform the installation in the 'C' drive. Also if the 'D' drive is a CD ROM and does not contain a CD it should perform the commands as if the computer does not have a D drive. if the program can check for available space in the C drives or D drives that would be great. Thanks a lot all of you.

Regards,
Waqas

waqas501
05-19-2002, 06:14 AM
Hi. In terms of pseudo code this is what I am trying to accomplish. I am basically unfamaliar with syntax and functions in VBS. The script i am trying to write will accomplish the folowing:

1)map a network drive to S: then it will perform following checks:

if d is harddrive or partition and has space >100MB then it runs s:\d.exe

else if e is hard drive and has space > 100MB run s:\e.exe esle run c.exe if any error occurs give a default message to give a message with an email address to send problem description to.

Thanks a lot guys.

Regards, Aditya & waqas

adamdelves
05-19-2002, 08:28 PM
The <a target="_blank" href=http://msdn.microsoft.com/library/en-us/vbenlr98/html/vaobjfilesystemobject.asp?>File System Object</a>, contains all the necessary properties and methods for file management, including

retriveing file/ folder size
returning all available drives
returning drive type
enumerating files and sub folders of directories
creating, deleting and moving files/ folder

To map a network drive you need to use the mapNetworkDrive method in the <a target="_blank" href=http://msdn.microsoft.com/library/en-us/script56/html/wsObjWshNetwork.asp?frame=true>Windows Script Host Network Object</a>

waqas501
05-20-2002, 03:15 PM
Hi Adam. Thanks for the reply. Can you help me with the coding? I know its a few lines only but I am totally unfamaliar with VB/Vb Script. I am a COBOL guy. I would really appreciate this favor if its not asking too much. Thanks again.

Best Regards,
Waqas

waqas501
05-20-2002, 03:28 PM
Adam,
Thanks for pinpointing me to that source. I have comeup with the following code so far but its not working. I still get some errors. any help is greatly appreciated:
Const FIXED = 2
Dim objFSO, objDrive
Dim WSHNetwork
Set WSHNetwork = WScript.CreateObject("WScript.Network")

WSHNetwork.MapNetworkDrive "S:", "\\pc504\c$"



Set objFSO = CreateObject("Scripting.FileSystemObject")
For Each objDrive In objFSO.Drives



If objDrive.DriveType = FIXED AND objDrive.DriveLetter = "d" Then
WshShell.Run("S:\d.bat")

elseif objDrive.DriveLetter = "e" and objDrive.DriveType = FIXED Then
wshell.run("S:\e.bat")
WScript.Quit
else
wshell.run("S:\c.bat")
Wscript.Quit
End If
Wscript.Quit
next

adamdelves
05-20-2002, 04:22 PM
I was compiling some code for you when you sent your last reply. Here is what I came up with. This script will check each fixed drive until it finds one with more than 100MB of space. When it does it sets done = true and stops searching. Hope this helps:

ADAM
<pre>
Option Explicit

Dim drives
Dim drive
Dim done
Dim shell
Dim fs
Dim net

Const DRIVE_UNKNOWN = 0
Const DRIVE_REMOVABLE = 1
Const DRIVE_FIXED = 2
Const DRIVE_REMOTE = 3
Const DRIVE_CD = 4
Const DRIVE_RAM = 5
Const nRemotePath = "Path of remote drive goes here"

done = false
' access components and object modules

set shell = CreateObject("wscript.shell")
set fs = CreateObject("scripting.filesystemobject")
set net = CreateObject("wscript.network")

' map the network drive
net.mapNetWorkDrive "S:", nRemotePath

' create a handle to the drives object

set drives = fs.drives

' cycle through the drives

For each drive in drives
' get drive type

If drive.drivetype = DRIVE_FIXED then
if driveSpace(drive) &gt; 100 then ' call function to return free space
Select Case uCase(drive.driveletter) ' retrieve drive letter
Case "C"
shell.run "S:\c.bat"
Case "D"
shell.run "S:\d.bat"
Case "E"
shell.run "S:\e.bat"
End select

' the script has carried out its task so exit the loop
done = true
Exit For
end if
end if
Next

' if there were no fixed drives or there were no fixed drives with more than
' 100MB of free space then tell the user

if not done then
MsgBox "An error occured- please send me an email at &lt;&lt;-EMAIL ADDRESS-&gt;&gt;"
end if

' clean up object mess
set fs = nothing
set shell = nothing
set net = nothing

Function driveSpace(driveObj)
Dim freeSpec

freeSpec = driveObj.freespace ' get free space
freeSpec = freespec/(2^20) ' freespace is returned in bytes so convert to Megabytes
driveSpace = Round(freespec, 0) ' round to the nearest megabyte
End Function
</pre>

waqas501
05-20-2002, 05:51 PM
Thanks a lot Adam. U r really really helpful. The script works. One more thing though if not asking much is that is it possible to set it so that first scans the D drive for the space and then e and c drives. I really appreciate your help.

Best Regards,
Waqas

Jama
05-21-2002, 05:19 AM
I used a *For I* statement with a series of *If then* statements To check drives d, e and then c In that order.
It makes the code a bit messy to look at, but so easy to understand that if you were to read it in a year from now, you won't have any difficulties understanding it and modifying it to your need.

The most important thing to understand is the "letter" variable, everything depends on it.
First, the script starts with "D" as the value of letter. The script will check to see if drive D exists, if it does, then it will check if it's a fixed drive and weather it has the amount of free space we need. If not, then the "letter" variable's value is incremented by one so it becomes "e" and the script will do the same checks for drive E as it did for D.

Once the script finds a suitable drive, it will run your program. Since the name of the program to be run is the same as the drive letter of the target drive, we can find the program name at run time with, um, you guessed it, the letter variable!

WshShell.run "S:\" & (Chr(letter)) & ".exe" 'will run S:\e.exe. if "e" is the drive that meets our needs.

If neither d nor e were suitable, then the "If i = 3 then" line will change the letter variable's value to "c" and the script will then use drive c if it's suitable.



'*********************** Script **************************
Set WshShell = WScript.CreateObject("WScript.Shell")
Set fso = CreateObject("Scripting.FileSystemObject")

Set WshNetwork = WScript.CreateObject("WScript.Network")
WshNetwork.MapNetworkDrive "S:", "\\pc504\c$"

Done = False
letter = Asc("D")

For i = 1 To 3

If i = 3 Then
letter = Asc("C")
End If


If Fso.DriveExists(Chr(letter)+":") Then
Set Drv = Fso.GetDrive(Chr(letter))

If Drv.drivetype = 2 Then
If Drv.FreeSpace / 1048576 &gt; 100 Then

'================================================= ========
'This section is not needed. I have added it to test the
'script On my computer. Delete it when you finish testing!
'================================================= ========
App = "S:\" & (Chr(letter)) & ".exe"
MsgBox "Drive " & (Chr(letter)) &_
":\ meets the requirements, the script will use this drive." &_
vbNewline & "The following program will be run:" & vbNewline &_
app & vbNewline & "Target " & Drv.path & vbNewline &_
"Free Space on target: " & Round(Drv.FreeSpace / 1048576, 0) &_
"MB", 64
'================================================= ========
'================================================= ========


WshShell.run "S:\" & (Chr(letter)) & ".exe"
Done = True

Exit For
End If
End If
End If

letter=letter+1

Next

If Done = False Then
MsgBox "Operation failed. Replace with your custom message!"
End If

Set WshShell = Nothing
Set fso = Nothing
Set WshNetwork = Nothing
Set Drv = Nothing

'*********************** END ************************

I hope I've clarified the script for you. But, if I've missed something or you're having difficulties with the script, please let me know.

Ps: rather than rely on the user to send you an email, why don't you automate it. You can have the script send you an email with little or no user intervention.
If this script is going to be run on multiple computers in a network, a better option would be to have the script on each computer amend it completion or non completion status to a log file on a network share. That way you can view all reports from one central location rather than fill your inbox with messages. Just a thought!

Jama

waqas501
05-21-2002, 02:33 PM
Thanks a lot Jama. That was nice. I hadnt known VB Scipt was so powerful. One more thing I was wondering was that if I provide the install package to remote users on a CD ROM and put the script on a CD ROM then is it possible to moidfy it so that it first determines the CD rom drive letter, then scans the drives ( d,e,c) and launches the appropriate package?
You have been such a big help I dont mind even if you refuse to repsond to this. Have a great one man.

Best Regards,
Waqas

Jama
05-21-2002, 05:59 PM
You can use the method supplied earlier by Adam to loop through all the drive in the system until you find a drive with DriveType value of 4 “CDRom”. The problem with this method is what to do if the system has 2 or more type 4 drives? Which one will your script use? My pc has a DVD Rom and a separate CDWriter, both will obviously have a drive type of 4!

The other method would be to get the running scripts full path at run time and then get its drive property.
This is by far easier than it sounds. This one line of code is all you need to display a script’s full name and path in a message box.

WScript.Echo WScript.ScriptFullName

Copy it to notepad, give it any name you want and save it wherever you like. It’ll help you understand the running scripts object properties.

The following script will get a handle to its self, then it will check the drive from which its being run to see if it’s a CDRom or not. It will then display some information about itself.

‘------------------------------ ScriptInfo.vbs ------------------------------------

Set fso = CreateObject("Scripting.FileSystemObject")
Set Scriptfile = fso.GetFile(WScript.ScriptFullName)
Set ScriptDrv = Fso.GetDrive(Scriptfile.drive)

If ScriptDrv.drivetype &gt;&lt; 4 Then
Wscript.echo "This is not a CDRom drive!"
Else
Wscript.echo "This is a CDRom drive!"
End If

Wscript.echo "This script is running from the " & ScriptDrv & ":\ drive!"
Wscript.echo "This script is in : " & Scriptfile.ParentFolder
Wscript.echo "This script’s full path is : " & Scriptfile.Path
Wscript.echo "This script is running from drive type : " & ScriptDrv.drivetype

‘---------------------------------------END ---------------------------------------

Now that we know how to retrieve this info, I would like to know what exactly do you want to do with it.
I mean, if you are distributing the script on a CD, then why do you want to know if it’s being run from the CDRom? If its not, how will that affect your script?
Please let me know.

Jama

adamdelves
05-21-2002, 08:08 PM
It took abit of thought but I have come up with a solution. VBscript comes with a dictionary object which you can add an variable to. You can then use the key method to access the members of the dictionary object in any order. Heres how it works:
<pre>
Option Explicit

Dim drives
Dim drive
Dim done
Dim shell
Dim fs
Dim net
Dim dictionary

Const DRIVE_UNKNOWN = 0
Const DRIVE_REMOVABLE = 1
Const DRIVE_FIXED = 2
Const DRIVE_REMOTE = 3
Const DRIVE_CD = 4
Const DRIVE_RAM = 5
Const nRemotePath = "Path of remote drive goes here"

' access components and object modules

set shell = CreateObject("wscript.shell")
set fs = CreateObject("scripting.filesystemobject")
set dictionary = CreateObject("scripting.dictionary")
set net = CreateObject("wscript.network")

' map the network drive
net.mapNetWorkDrive "S:", nRemotePath

' create a handle to the drives object

set drives = fs.drives

' cycle through the drives
' add the appropriate drives to the dictionary

For each drive in drives
' get drive type

If drive.drivetype = DRIVE_FIXED then
Select Case uCase(drive.driveletter) ' retrieve drive letter
Case "C"
dictionary.add "C", drivespace(drive)
Case "D"
dictionary.add "D", drivespace(drive)
Case "E"
dictionary.add "E", drivespace(drive)
End select
end if
Next

' check if the drives have more than 100MB of freespace in
' the required order
<font color=red>
if dictionary.item("D") &gt; 100 then
shell.run "C:\d.bat"
done = true
elseif dictionary.item("E") &gt; 100 then
shell.run "C:\e.bat"
done = true
elseif dictionary.item("C") &gt; 100 then
shell.run "C:\c.bat"
done = true
end if
</font color=red>
if not done then
MsgBox "An error occured- please send me an email at &lt;&lt;-EMAIL ADDRESS-&gt;&gt;"
end if

' clean up object mess
set fs = nothing
set shell = nothing
set net = nothing
set dictionary = nothing

Function driveSpace(driveObj)
Dim freeSpec

freeSpec = driveObj.freespace ' get free space
freeSpec = freespec/(2^20) ' freespace is returned in bytes so convert to Megabytes
driveSpace = Round(freespec, 0) ' round to the nearest megabyte
End Function
</pre>

waqas501
05-22-2002, 03:21 PM
Jama. For remote users I plan to provide the installpackages on a CD with the script rather than map a drive. so in their case they would click on the script and it would perform similar checks except that this time it would launch the respective package off the CD ROM (whose drive letter needs to be determioned). Thanks a lot for your help.

Regards,
Waqas

waqas501
05-22-2002, 03:41 PM
Thanka a lot Adams for giving this problem a thought. I checked your script and it works really good. Now I have 2 solutions. One from u and one from jama. I really appreciate u guys taking so much time for working on this.

Regards,
Waqas