Using VBS to cancel hiding all the files in the folder

Time:2021-2-11

Q:
Hello, script expert! How to run a script to unhide all files in a folder?
— GA
A:
Hello, GA. As you know, one of our script experts spent countless hours playing hide and seek with other kids in the neighborhood when he was young. In fact, the script expert clearly remembers lying in a shallow ditch and letting other groups walk past him without knowing that he was there. (it worked so well that he was seriously considering digging a shallow ditch in his office so that he could hide in it when someone came to him.)
Back in those days, you don’t want a script that automatically unhide everything; it might be useful, but not fun. But as a system administrator, you probably don’t want to play hide and seek with files. You may be willing to accept scripts that can automatically unhide all files in a folder, which are similar to the following scripts:

Copy codeThe code is 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 a bit unusual because we use both WMI and filesystem object. (of course, it sounds dangerous, but it’s not: after all, we are trained professionals.) First connect to the local computer’s WMI service (which we’ll talk about in a moment), and then use the following query to retrieve the collection of all the files in the C:: scripts folder (that is, CIM)_ All instances of the datafile class:
Set FileList = objWMIService.ExecQuery _
    (“ASSOCIATORS OF {Win32_Directory.Name=’c:\Scripts’} Where ” _
        & “ResultClass = CIM_DataFile”)
We use WMI to retrieve the collection of files, because it is very fast to use, and in most cases the same script can be used to retrieve the collection of files from a remote computer. Unfortunately, WMI cannot be used to unhide files; CIM_ The datafile class does include a property called hidden, but it is read-only. That’s why we have to use both “WMI” and “filesystem object”: we use “WMI” to get the collection of files, and then use “filesystem object” to unhide them.
Frustratingly, this also means that the script has to run on the local machine; unlike WMI, filesystem object cannot work remotely. That’s bad.
Anyway, after getting the file collection, we create an instance of “file system object” and then create a “for each” loop to traverse the group of files. In the “for each” loop, use the following 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 the file path; therefore, the file “name” is at least similar to the file path in ﹣ WMI ﹣ C: ﹣ scripts ﹣ my_ file.txt .
After binding to a given file, checks whether the file is hidden; hiding or not is exactly part of the file properties. Use the down code to check whether the “switch” of the hidden property is turned on. This statement is true if the switch is open 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
be careful. File attributes are stored as “bitmap” attributes. In this column, we will not explain the details of the “bitmap” attribute, but you can find the detailed explanation and usage of “bitmap” in the “Microsoft Windows 2000” script guide “.
Why care if the file is hidden? The simplest way to cancel the hiding of hidden files is to turn the switch from on to off. In fact, the downlink code plays this role
objFile.Attributes = objFile.Attributes XOR 2
However, the XOR operator is not particularly clever: it just changes a switch from one state to another. If the switch is on, turn it off; if the switch is off, turn it on. That’s why we check the current status of the file. If the file is hidden, use “XOR” to turn the switch from on to off. But what if the file is not hidden? In this case, you don’t need a transfer switch; doing so hides the file. (because XOR will turn the switch from off to on.) Therefore, check the value of each file before starting the transfer switch.
Run this script – found you, all out! All previously hidden files will reappear. Then it’s your turn to hide and see if the files can find you.