Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

vbYESNO - Select Case? Or ?

Status
Not open for further replies.

Nu2Java

Technical User
Jun 5, 2012
166
US
Hi there...

I have this code to set Folder attributes, but I have not done a lot with FUNCTIONS. I want this to popup a YES or NO for the user to run it or not. I am not sure if Select Case is the way to go or if there is a better way to write this script. I have had several versions so far. Any help is a great learning experience for me.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("Wscript.Shell")
Dim Msg

sCurrentDirectory = WSHShell.CurrentDirectory & "\"
notEmpty(sCurrentDirectory)

Msg = MsgBox("This script should be run in the directory you want to Hide/Unhide folders for." & vbcr & vbcr & _
"Do you want to run the script now" ,vbYesNo,"Path: " & sCurrentDirectory )

s = Timer
f= -1

'WScript.Echo("Completed " & f & " folders for " & Timer - s & " seconds")

Function notEmpty(setFolder)

On Error Resume Next

Select Case Msg
Case vbYes
  f = f +1
  Set objFolder = objFSO.GetFolder(setFolder)
  attr = 2  '' hidden
  if objFolder.Files.Count > 0 then attr = 0
  subattr = 2
  for each objSubfolder in objFolder.SubFolders
    subattr = notEmpty(objSubfolder.Path)
    if subattr = 0 then attr = 0
  next
  objFolder.Attributes = attr
  notEmpty=attr 
Case vbNo
    WScript.Quit
End Select

End Function
 
An if/then will suffice and it should be moved outside the function.
A function should be independent - able to run by itself with just the parameters passed in. It doesn't work in your example because [tt]Msg[/tt] if defined after the function is called.
And you shouldn't use On Error Resume Next. It hides all errors.

Code:
function notEmpty(setFolder)
  f = f + 1
  Set objFolder = objFSO.GetFolder(setFolder)
  attr = 2  '' hidden
  if objFolder.Files.Count > 0 then attr = 0
  subattr = 2
  for each objSubfolder in objFolder.SubFolders
    subattr = notEmpty(objSubfolder.Path)
    if subattr = 0 then attr = 0
  next
  objFolder.Attributes = attr
  notEmpty=attr 
end function

msg = msgbox("question", vbYesNo, "title")

if (msg = vbYes) then
   attribute = notEmpty(sCurrentDirectory)
else
   wscript.quit
end if

-Geates

 
And it should be named appropriately. The name suggests one action but it does another. What do you want your function to do?

Here's a simple example of the proper (pointless, but proper) use of a function.

Code:
function add(x, y)
   add = x + y
end function

a = 5
b = 10
c = add(a, b)

msgbox a & " + " & b & " = " & c

-Geates

NOTE: I wish the author had the option to edit/delete

 
Thanks Geates, I am still a little confused how the function is either called or not. What my code does (excluding my vbYesNo) is set the folders that are empty to hidden. But what I want is to ask the question first to the user IF they are sure they want to perform this action. So I guess I am confused at what point I either run the Function, or quit the script.
 
I think your confusion is the order of where the code is. The function "notEmpty" happens to be defined first in Geates' sample (blue below), but that doesn't mean that it is executed at that time. It is only executed when called.

The first line of code that actually gets executed is the first red line, which asks the yes/no question and stores the answer in the variable "msg". The rest of the code in red says "if the user clicked yes, execute the function "notEmpty". Otherwise, quit the script.


Code:
[COLOR=blue]function notEmpty(setFolder)
  f = f + 1
  Set objFolder = objFSO.GetFolder(setFolder)
  attr = 2  '' hidden
  if objFolder.Files.Count > 0 then attr = 0
  subattr = 2
  for each objSubfolder in objFolder.SubFolders
    subattr = notEmpty(objSubfolder.Path)
    if subattr = 0 then attr = 0
  next
  objFolder.Attributes = attr
  notEmpty=attr 
end function[/color]

[COLOR=red]msg = msgbox("question", vbYesNo, "title")

if (msg = vbYes) then
   attribute = notEmpty(sCurrentDirectory)
else
   wscript.quit
end if[/color]
 
Thanks Guitarzan... Here is what I have so far based on the first reply from Geates.

Code:
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set WSHShell = CreateObject("Wscript.Shell")
Dim Msg

sCurrentDirectory = WSHShell.CurrentDirectory & "\"
notEmpty(sCurrentDirectory)


function notEmpty(setFolder)
  f = f + 1
  Set objFolder = objFSO.GetFolder(setFolder)
  attr = 2  '' hidden
  if objFolder.Files.Count > 0 then attr = 0
  subattr = 2
  for each objSubfolder in objFolder.SubFolders
    subattr = notEmpty(objSubfolder.Path)
    if subattr = 0 then attr = 0
  next
  objFolder.Attributes = attr
  notEmpty=attr 
end function

msg = msgbox("question", vbYesNo, "title")

if (msg = vbYes) then
   attribute = notEmpty(sCurrentDirectory)
else
   wscript.quit
end if

The only problem is, the function runs anyway and sets the empty folders to hidden before selecting yes or no.
 
Get rid of the 6th line: notEmpty(sCurrentDirectory)

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
Ahhh... I was calling it and didn't even notice that. Thanks PHV for pointing that out. Thanks to all for the help, greatly appreciated.
 
Personal preference...

I am still a little confused how the function is either called or not.
The differences in functions and sub and when to use them are many. But, in broad terms, functions are self-contained, "single" steps that return a value and subs are and do not. Consider the math problem: (5 + ((3 * 6) / 2) - 3). Solving the problem is a sub routine and the steps taken to solve it are the functions. Here's how I would break it down into sub routines and functions.

Code:
sub routine: solve math problem
   the problem to solve is (5 + ((3 * 6) / 2) - 3)
   result1 = multiply 3 and 6 (the result of the function is 18)
   result2 = divide result1 by 2 (the result of the function is 9)
   result3 = add 5 to result2 (the result of the function is 14)
   answer = subtract 3 from result3 (the result of the function is 11)
end sub

-Geates

 
Geates... Thanks for the additional input. This has inspired me to look more into Functions and how to decide if and when they should be used. My next adventure with this script is to write the folder names that changed to a text file to keep a log file as to what is changing daily. I really appreciate your time, Thanks again.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top