Big problem of recursivity
Hello all,
I need to impement a very complexe script to do the following :
We've a specific clients folder structure whick is the same for every clients (with 4 level of subfolders ..). We have different services in the company and clients can be clients of one or many different services but all have the same folder structure (when we enter a new client, it's structure is created automatically).
Now, I'd like to make a script running every night to hide empty folders of the structure (it's a wish of the users), in this way, than can see, if a part of the stucture is not displayed that they are not customer of this part. So, I'm trying to make a recursive script that allows to hide empty folders, but as soon as a folder is not empty, its parents don't have to be hidden, of course.
Here's what I made :
strComputer = "."
Const cFOL = "C:\DELL\"
'--------------------------------------------------------------
'* Declare Objects
'*--------------------------------------------------------------
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Bidon = RepVide(cFOL)
Set objFSO = Nothing
'***************************************
'* Routine de parcours de la structure
'***************************************
Function EmptyFolder(FolderSpec)
EmptyFolder = True
NoFile = True
For Each strFOL In objFSO.GetFolder(FolderSpec).Subfolders
NoFile = EmptyFolder(strFOL.Path)
Next
if (objFSO.GetFolder(FolderSpec).Files.Count) = 0 AND NoFile Then
EmptyFolder = True
Else
EmptyFolder = False
End if
End Function
This worked but as soon as I've 2 levels that are empty (even if the 3rd is not), the upper lever is considered to be hidde, which is not correct.
So, every help is welcome
Per advance, thanks for your help
Arnaud
Re: Big problem of recursivity
If I understand the requirements, this should do it.
Code:
Option Explicit
Const Hidden = 2
Const Normal = 16
Const RootDir = "C:\dell"
Dim fso
Dim blnVisible
Set fso = CreateObject("Scripting.FileSystemObject")
HideEmptyFolder fso.GetFolder(RootDir)
Set fso = Nothing
Private Sub HideEmptyFolder(fold)
Dim fld
'Check for subfolders
For Each fld In fold.SubFolders
HideEmptyFolder fld
Next
'Check if the current directory has files
If fold.Files.Count = 0 Then
'If the current directory doesn't have
'files then check if there are visible
'directories in it.
For Each fld in fold.SubFolders
If fld.Attributes = Normal Then
'If a directory is visible set a flag
'and exit the loop. There is no need to
'check any further
blnVisible = true
Exit For
End If
Next
'Set the folder attribute.
'Hidden if the current directory doesn't have
'files in it and all of it's subdirectories are
'hidden. Otherwise show it.
If blnVisible Then
fold.Attributes = Normal
Else
fold.Attributes = Hidden
End If
'Reset the flag for later use
blnVisible = false
Else
'The current directory has files so set
'the attribute to normal
fold.Attributes = Normal
End If
End Sub
Re: Big problem of recursivity
Hello Markt,
Thanks very very much for your help. it's working very fine. If you send to me your postal address at arnaud_DOT_forster_AT_mwprog_DOT_ch, I'll send to you some chocolate (if you like it) ;)
Now, for everybody, I've a nother question about this script. Is it possible, when I'm on my folder structure, to right-click on a folder and then to display on my contextual menu a line to launch the script with the path to the folder I just right-clicked in paramters ?
Thanks very much
Re: Big problem of recursivity
Ok,I've found how to do it.
Here's the vbs script to add the function to my registry :
Const HKEY_LOCAL_MACHINE = &H80000002
strComputer = "."
Set oReg=GetObject("winmgmts:{impersonationLevel=impersonate}!\\" & _
strComputer & "\root\default:StdRegProv")
strKeyPath = "SOFTWARE\Classes\Folder\shell\Run_my_vbs\Command"
strValueName = null
strValue = "wscript \\My_server\e$\Scripts\My_vbs.vbs %L"
oReg.CreateKey HKEY_LOCAL_MACHINE,strKeyPath
oReg.SetStringValue HKEY_LOCAL_MACHINE,strKeyPath,strValueName,strValue
But I've the following problem. My script is on a server to allow multiple people to run it. When I test the function on my computer, I right clik on a folder and chose my function to my contextual menu. Than, the script starts as it should be. But when the script get the path (c:\My_folder), of course, this directory doesnt exists on the server, so how can I say to the script to find the argument on the machine on whick it's executed .. ?
Re: Big problem of recursivity
Still better but I've problem with spaces in the path. So, when I right-click on the folder, I execute the script. I've the following code to retrieve the path of the folder :
Set objArgs = WScript.Arguments
For I = 0 to objArgs.Count - 1
strArgs= strArgs &" "& objArgs(I)
Next
RootDir = StrArgs
When there's a space in the path, I get an error when calling the function
HideEmptyFolder fso.GetFolder(RootDir)
But if I declare a const with the same path, this is working ...
Const RootDir = "C:\Documents and Settings\My_Folder\Desk\Test"
I tried to insert some quotes before and after the path using
RootDir = Chr(34) & StrArgs & Chr(34)
but no result
If someone has an idea...
Thanks
Re: Big problem of recursivity
Ok, I've found. I didnt see that a white space in the beginning of the args, so I added a LTrim() function and eveything works fine now,
Thabnks