Ask:
Hello, Scripting Guy! How do I run a script to unhide all files in a folder?
--GA
For:
Hello, GA. You know one of our Scripting Guys spends countless hours playing hide-and-seek with other kids in the neighborhood. In fact, the Scripting Guy clearly remembers lying in a shallow ditch and letting the other group walk through him without knowing he was there. (This works so much that he is seriously considering digging a shallow ditch in his office so that someone can hide in a shallow ditch when they find him.) )
Back in those days, you wouldn't want a script that would automatically unhide everything, and it might be useful, but it's not fun. But as a system administrator, you probably don't want to play Hide-and-seek with files. You might be happy to accept a script that automatically suppresses all files in a folder, which is similar to the following:
Copy Code code as follows:
StrComputer = "."
Set objWMIService = GetObject ("winmgmts:\\" & StrComputer & "\root\cimv2")
Set FileList = objWMIService.ExecQuery _
("Associators of {win32_directory.name= ' c:\Scripts '} Where" _
& "ResultClass = Cim_datafile")
Set objFSO = CreateObject ("Scripting.FileSystemObject")
For each objfile in FileList
Set objfile = Objfso.getfile (objfile.name)
If Objfile.attributes and 2 Then
Objfile.attributes = objfile.attributes XOR 2
End If
Next
This script is somewhat unusual because we use both WMI and FileSystemObject. (Of course, this sounds dangerous, but it's not: After all, we are specially trained professionals.) First connect to the WMI service on the local computer (which we'll talk about shortly), and then use the following query to retrieve a collection of all the files in the C:\Scripts folder (that is, all instances of the Cim_datafile Class):
Set FileList = objWMIService.ExecQuery _
("Associators of {win32_directory.name= ' c:\Scripts '} Where" _
& "ResultClass = Cim_datafile")
We use WMI to retrieve a collection of files because it is quick to use, and in most cases you can use the same script to retrieve a collection of files from a remote computer. Unfortunately, WMI cannot be used to unhide files; The Cim_datafile class does include a property named Hidden, but it is a read-only property. That's why we have to use WMI and FileSystemObject at the same time: we use WMI to get a collection of files, and then use FileSystemObject to unhide the files.
Sadly, this also means that the script must be running on the local computer, because unlike WMI, FileSystemObject cannot work remotely. That's lame.
Anyway, after we get the file collection, we create a FileSystemObject instance and then create a For Each loop to traverse this group of files. In the For Each loop, use the downlink code to bind to each file:
Set objfile = Objfso.getfile (objfile.name)
As you can see, we just called the GetFile method to pass the value of the Name property (obtained using WMI). The Name property is equivalent to a file path, so the file "name" is at least similar to C:\Scripts\My_file.txt in WMI.
After binding to a given file, check to see if the file is hidden, or hide or not hide exactly as part of the file's properties. Use the downlink code to check if the "Switch" for hidden properties is turned on. This statement is True if the switch is turned on and the file is hidden. If the switch is off and the file is not hidden, this statement is False:
If Objfile.attributes and 2 Then
Attention. File properties are stored as bitmask properties. In this column, we don't specify the details of the bitmask property, but you can find detailed explanations and usage of bitmask in the Microsoft Windows 2000 Scripting Guide.
Why do you care if the file is hidden? The easiest way to remove hidden files is to turn the switch off, in fact, the downlink code does this:
Objfile.attributes = objfile.attributes XOR 2
However, the XOR operator is not particularly smart: it simply converts a switch from one state to another. If the switch is on, turn it off, and if the switch is off, open it. That's why we check the file's current state. If the file is hidden, use XOR to turn the switch from open to closed. But what if the file is not hidden? In this case, you do not need to convert the switch; this hides the file. (Because XOR switches the switch from off to open.) Therefore, you should check the value of each file before starting the conversion switch.
Run this script-find you, all out! – All files that were previously hidden will appear again. Then it's your turn to hide and see if these files can find you.