Get hard drive letters

Deploying to Windows? Utilizing VB Script execution? This is the place to ask Windows-specific questions.

Moderators: FourthWorld, heatherlaine, Klaus, kevinmiller, robinmiller

SparkOut
Posts: 2945
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Sat Dec 06, 2008 1:08 pm

I'm sorry, that was a bit harsh. I know English is not your first language and I apologise - but it does seem that your tone is more "do it for me" than "please help me to discover where I am going wrong".

If you open Rev, then in the toolbar there will be a Rev Online icon. Click that and it will open the Rev Online browser where you can find many sample stacks being shared by different Rev users for education or development or interest, etc.

mwieder
VIP Livecode Opensource Backer
VIP Livecode Opensource Backer
Posts: 3581
Joined: Mon Jan 22, 2007 7:36 am
Contact:

Post by mwieder » Sat Dec 06, 2008 5:44 pm

...

Use Rev's Development menu and select "Revolution Online". Select "User Spaces" and Browse Users. The user name is "SparkOut".

What are you trying to do? You're in way over your head...

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Post by alemrantareq » Mon Dec 08, 2008 6:25 am

Dear SparkOut,
I give some info about me. I can't use internet at my home as there is no internet connection available in my home area and it takes high costs if I share it from another areas. So, I go to the internet browsing shops and we, the browsers, are strictly restricted to install any softwares on their pcs. I thought there will be the another way to meet the rev online. Thats why, I did my last post. I'm sorry to you cause if I'd share it with you before, you wouldn't appreciate me wrong.

In my script, I think I couldn't set the custom property properly. I'll be very greatful if you just only check my custompropertyset. If you really become tired on me, then I think I can't go with this project anymore.

SparkOut
Posts: 2945
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Mon Dec 08, 2008 9:28 am

In the Rev IDE, select the button and use the Property Inspector to set the properties of the button. In the top drop-down box you can select "Custom Properties".
Click the "plus" symbol by the Custom Properties label at the top. Give your new Custom Property a name - in this case we've called it cVBScript.
In the box at the lower part of the inspector, Property Contents, paste the vbscript code:

Code: Select all

intDriveType = 3 
strComputer = "." 
result = "" 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colDisks = objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk") 
For Each objDisk in colDisks 
  Select Case objDisk.DriveType 
  Case intDriveType 
    result = result & objDisk.DeviceID & vblf 
  End Select 
Next 
Set colDisks = nothing 
Set objWMIService = nothing
Now the button has a custom property called cVBScript which contains the vbscript code to use by getting Rev to "do" the code as vbscript. Note that this code is foreign to Rev, and you do not use the script editor to make any kind of vbscript edits, it is just text that happens to be stored in the property. You don't have to store the vbscript in a custom property, it is just a convenient way to get Rev to hold it before it is used with the "do" command.

Edit the script of the button:

Code: Select all

on mouseUp 
  do the cVBScript of me as "vbscript" 
  put the result into field "fldDrives" 
end mouseUp
(note that a custom property is not a variable - although it can be set to variable things - but you can't just refer to the name of it on its own, you have to interrogate Rev to obtain the property of a particular object. (Just like asking Rev to put "the width of field fldName" - width is a property, and you're asking Rev for the property of a particular field.) In this case you are getting the custom property in the mouseUp handler of a particular button, so "of me" refers to the button. You could just as easily say "do the cVBScript of card "Drive Info" if that happened to be the place which you had made the cVBScript custom property.)

I really think you should practice more with some of the basics of Rev before tackling something like this. If you have more understanding of the way to make Rev work, then you will get on much better. Everyone on this forum is helpful and nobody is selfish with their code, so I am sure you will get a lot of help when you need it, but you do need to have a good idea of how to make the basic controls work in what you are doing in order to appreciate the scripts people provide. This isn't to discourage you or anyone else from making ambitious projects in Rev - I think Rev is the perfect thing to bring "professional" standard application development within reach of a novice, or hobbyist - but you do need to walk before you run.

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Post by alemrantareq » Sat Dec 13, 2008 12:04 pm

Dear SparkOut,

Thanks for your reply in details. Your script works successfully with rev 3.0 but not with rev 2.8.1. It shows "Alternate language not found" - why?


ii) I want to check a text file and some texts of it whether it exists in my local hard drives or not. Its name "Test.txt" and I want to check "This is my text" line of it. So, I've made a vbscript of a btn named cVBScript and replace "<<intDriveType>>" with "3" (without quote). Then gave the following script for that btn -

Code: Select all

on mouseUp
   put the cVBScript of me into tScript
   do tScript as "vbscript"
   repeat for each line tline in the result
      put tline & "/Test.txt" into tfile
      if there is a file tfile then
         put lineoffset("This is my text", tfile) into tline
         if tline is not zero then
            answer "Found"
         end if
      end if
   end repeat
end mouseUp
I made the text file with that line in C: drive but it doesnt response - why?

iii) If I want to put some texts after the hard drives in a scroll field, after which line I've to put them in vbscript? Thanks in advance....

SparkOut
Posts: 2945
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Sat Dec 13, 2008 2:36 pm

i) because adding vbscript as an alternate language for the "do" command is a recent enhancement to Rev. Check the notes in the dictionary.

ii) Presumably you have made a custom property of the button and set the contents of that property to the vbscript, as explained previously. If not, then your check of the file contents will never be reached.
If the hard disks are being returned correctly by the vbscript call, then as is stands your code will not check for the contents of the file, as you have only set the tfile variable to be the path and the filename. The lineoffset will always fail because it is checking for "This is my text" in "C:/Test.txt" - so you first need to read the contents of the file into a variable to check it. Use

Code: Select all

put URL ("file:" & tfile) into tFileToCheck
or similar to get the contents first. Then use tFileToCheck not tFile in the lineoffset check.

iii) What do you want to put into the scrolling field in what format?

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Post by alemrantareq » Sun Dec 14, 2008 6:17 am

Hi SparkOut,
Thanks for your reply. I'm repling according to my previous post.

i) That means I cant get the hard drive letters with rev 2.8.1, isnt it ?

ii) Yes, I've succeed to make a custom property and check the text file whether it exists in my hard drives or not. But when I went to check its line, I failed. Thats why I posted the previous. Now, I've modified the following btn script according to your suggestion -

Code: Select all

on mouseUp
   put the cVBScript of me into tScript
   do tScript as "vbscript"
   repeat for each line tline in the result
      put tline & "/Test.txt" into tfile
      if there is a file tfile then
         put url ("file:" & tfile) into cfile
         put lineoffset("This is my text", cfile) into tline
         if tline is not zero then
            answer "Found"
         end if
      end if
   end repeat
end mouseUp
But it still not working. Why ?

iii) Actually I want to put the text "My File" after each hard drive volume in the scrolling field "MyField".

Code: Select all

on mouseUp
   put the cVBScript of me into tScript
   do tScript as "vbscript"
   repeat for each line theline in the result
      put theline & "/My File" into field "MyField"
   end repeat
end mouseUp
It shows only the last volume with that text, not the all volumes. Why ?

SparkOut
Posts: 2945
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Sun Dec 14, 2008 10:57 am

i) No, you still can get the hard drive letters, but you can't get Rev to "do" the script. You will have to write out the vbscript file to a temporary location and then use the shell command to activate it. To return the value of the results to the command line for Rev to use, you must also add the following line to the end of the vbscript

Code: Select all

WScript.Echo result
("result" is the variable name that you used to put the drive letters into earlier in the script.)

Code: Select all

on mouseUp
   local tVBSfile, tfile, tResults, cfile
   put specialFolderPath("temporary") & "/tempRevScript.vbs" into tVBSfile
   answer "Writing a temporary VBS file"
   put the cVBScript of me into url ("file:" & tVBSfile)
   answer "Using shell to execute VBS"
   set the hideConsoleWindows to true
   get shell("cscript.exe //nologo" && quote & tVBSfile & quote)
   put it into tResults
   if char -1 of tResults is CR then delete char -1 of tResults
   answer "Results obtained: " & cr & tResults
   repeat for each line tline in tResults
   answer "Looping through the results"
      put tline & "/Test.txt" into tfile
      answer "Checking for file" && tfile 
      if there is a file tfile then 
         answer tfile && "found"
         put url ("file:" & tfile) into cfile
         answer tfile && "contents:" & cr & cfile
         put lineoffset("This is my text", cfile) into tline 
         answer "Checking for text in the file"
         if tline is not zero then 
            answer "Found"
         else
            answer "Not found" 
         end if 
      end if 
   end repeat 
   answer "Deleting the temporary file"
   delete file (tVBSfile)
   answer "All results checked - exiting"
end mouseUp
ii) Because you have got something wrong... While you are debugging and testing, I suggest you add lines to your code which simply answer "the stage in the code" so that you can see what is happening, as well as stepping through the code in the debugger and watching the variables as you do.

iii) Because you are setting the entire contents of the field each time you pass through the loop. If you start with an empty field, change "into" to "after" and this will add the new value to the end of the existing content. You will need to initialise the field to be empty before you start the loop. Also bear in mind that if you are using the 2.8.1 "get shell" version, "the result" will not contain the returned values, "it" will contain the results from the WScript.Echo value returned from the shell, which you should put into a variable for processing. ("it" and "the result" are volatile and will change throughout the runtime of the script.)

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Post by alemrantareq » Sat Dec 20, 2008 8:59 am

Hi SparkOut,
Thanks for your reply.

i) Your script works nicely. Now, I want to know a little bit. I want to get the hard drives and removable disks togather in rev 2.8.1. How to do that?

ii) I modified the script again and it successfully works in rev 3.0 -

Code: Select all

on mouseUp
   do the cVBScript of me as "vbscript"
   repeat for each line tline in the result
      put tline & "/Test.txt" into tfile
      if there is a file tfile then
         put url ("file:" & tfile) into cfile
         if lineoffset("This is my text", cfile) <> 0 then
            answer "Found"
         end if
      end if
end mouseUp

iii) The following script also solved my problem (iii) -

Code: Select all

on mouseUp
   put the cVBScript of me into tScript
   do tScript as "vbscript"
   repeat for each line theline in the result
      put theline & "/My File" & return after field "MyField" 
   end repeat
end mouseUp

SparkOut
Posts: 2945
Joined: Sun Sep 23, 2007 4:58 pm

Post by SparkOut » Sat Dec 20, 2008 11:35 am

i) You need to adapt the wmi vbscript to return info for both drive types 3 (hard disks) and drive types 2 (removable disks). Note that you will likely not get any results for removable disks unless there is media in the drive.

Code: Select all

' set up pseudo-constants for legibility
intHardDiskType = 3 
intRemovableDiskType = 2

strComputer = "." 
result = "" 
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strComputer & "\root\cimv2") 
Set colDisks = objWMIService.ExecQuery ("SELECT * FROM Win32_LogicalDisk") 
For Each objDisk in colDisks 
  Select Case objDisk.DriveType 
  Case intHardDiskType
    result = result & objDisk.DeviceID & vblf
  Case intRemovableDiskType
    result = result & objDisk.DeviceID & vblf 
  End Select 
  'This is the same as saying
  'If objDisk.DriveType = 3 or objDisk.DriveType = 2 then...
  'VBScript does not seem to consolidate the select actions,
  'so you have to explicitly give the action for both of the cases
  'that we have selected here.
Next 
Set colDisks = nothing 
Set objWMIService = nothing
Wscript.Echo result
Look up http://www.w3schools.com/vbscript/vbscr ... ionals.asp and test and learn. What the script above does is to interrogate all drives connected to the target computer, putting the details into a collection called colDisks. Then each "objDisk" in the collection is tested for its .DriveType and an action will be "Selected" in the "Case" that the .DriveType is 3 and (as it happens, the same action) in the "Case" that the .DriveType is 2. In other variations (eg Rev) the "Select - Case" construct the Cases which need to take the same action can be grouped together and only one action declaration is required. (A "break" statement is usually required at the end of each "case" though.)

alemrantareq
Posts: 203
Joined: Wed Jul 23, 2008 8:46 am

Post by alemrantareq » Sun Dec 21, 2008 8:35 am

Hi SparkOut,

Thanksssssss !!!!!!!! :lol:

Post Reply