2

I have a script that creates a subfolder named 'pst' to all folders in the root directory c:\test. What code do I need to add to this script to make the 'pst' folder hidden?

folder_path = "c:\test"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folder_path)
For each f in folder.SubFolders
fso.CreateFolder(f & "\PST")
next

Any help is greatly appreciated.

2 Answers 2

2

All you need to do is play with the folder attributes.

Const Directory = 16
Const Hidden = 2

folder_path = "c:\test"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folder_path)
For each f in folder.SubFolders
    With fso.CreateFolder(fso.BuildPath(f, "PST")) 'returns folder object
        .Attributes = Directory Or Hidden 'set hidden flag for directory
    End With
Next
Sign up to request clarification or add additional context in comments.

Comments

1

You can try something like this :

Option Explicit
Dim fso,folder_path,folder,f
folder_path = "c:\test"
Set fso = CreateObject("Scripting.FileSystemObject")
Set folder = fso.GetFolder(folder_path)
For each f in folder.SubFolders
'wscript.echo f & "\PST"
    If Not fso.FolderExists(f & "\PST") Then
        fso.CreateFolder(f & "\PST")
        Call Hide(f & "\PST")
    End If
    If fso.FolderExists(f & "\PST") Then
        Call Hide(f & "\PST")
    End If
Next
'**********************************************
Sub Hide(MyFolder)
    Dim Command,Result,Ws
    Command = "Cmd /c attrib +h " & DblQuote(MyFolder) &""
'wscript.echo Command
    Set Ws = CreateObject("Wscript.Shell")
    Result = Ws.Run(Command,0,True)
End Sub
'**********************************************
Function DblQuote(Str)
    DblQuote = Chr(34) & Str & Chr(34)
End Function
'**********************************************

6 Comments

You are already calling FSO so why not just change the attributes by setting Hidden, using WScript.Shell to call attrib is imho using a sledgehammer to crack a walnut.
@Lankymart i like to do it in the hard way, and it works 5/5 :)
Point taken but imagine this was part of a huge script and you had to instantiate two COM objects instead of the one? What implications that could then have if those objects were not correctly released in memory, not to mention the performance (It would be interesting to see which performs better).
To be fair it will work, I just think @kul-tigin approach is cleaner and probably more efficient.
That's the difference between answering the question and trying to do the OPs work for them. The original code never had a FolderExists() check, so the sample they provided doesn't. That's not to say you can't suggest it to the OP that it would be a good idea. For all you know the OP only ever uses it when a folder exists making the FolderExists() check superfluous to the requirement.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.