Use vbs to unhide all files in the folder

Source: Internet
Author: User
Tags bitmask

Q:
Hello, script expert! How can I run a script to unhide all files in a folder?
-- Ga
A:
Hello, Ga. You know that one of our script experts spent countless times playing hide-and-seek games when they were young and other children in the neighbor's house. As a matter of fact, the scripting expert still clearly remembers to lie in the shangou and let other Groups walk over him, but he does not know that he is there. (This method works so effectively that he is seriously considering digging a shalou in his office so that someone can find him and hide in the shalou .)
Back in those days, you don't want a script that can automatically unhide everything; it may be useful, but not fun. But as a system administrator, you probably do not want to hide and seek files. You may be happy to accept the script that automatically unhide all files in the folder, which is similar to the following: CopyCode The Code is as follows: strcomputer = "."
Set ob1_miservice = GetObject ("winmgmts: \" & strcomputer & "\ Root \ cimv2 ")
Set filelist = obw.miservice. 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 is not: after all, we are specially trained professionals .) Connect to the WMI Service of the Local Computer (we will talk about this soon), and then use the following query to retrieve C: the set of all files in the \ scripts folder (that is, all instances of the cim_datafile class ):
Set filelist = obw.miservice. 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 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 cancel hiding files. The cim_datafile class does include a property named hidden, but it is a read-only property. This is why we must use WMI and FileSystemObject at the same time: We Use WMI to get the collection of files, and then use FileSystemObject to unhide these files.
It is frustrating that this also means that the script must run on a local computer; FileSystemObject cannot work remotely, unlike WMI. Really bad.
In any case, after obtaining the file set, we create a FileSystemObject instance and create a for each loop to traverse the files in this group. In the for each loop, bind the following code to each file:
Set objfile = objfso. GetFile (objfile. Name)
As you can see, we only call the GetFile method to pass the value of the name attribute for it (obtained Using WMI ). The name attribute is equivalent to the file path. Therefore, the "name" of the file is at least similar to c: \ scripts \ my_file.txt in WMI.
After binding to a given file, check whether the file is hidden. Hiding or not hiding is part of the file property. Use the downstream code to check whether the hidden attribute "Switch" is enabled ". If the switch is enabled and the file is hidden, this statement is true. If the switch is off and the file is not hidden, this statement is false:
If objfile. attributes and 2 then
Note. File attributes are stored as bitmask attributes. In this column, we do not detail the details of the bitmask attribute, but you can find detailed explanations and usage of bitmask in the Microsoft Windows 2000 script guide.
Why are you concerned about whether the file is hidden? The simplest way to unhide a hidden file is to switch the switch from on to off. In fact, the downstream code plays this role:
Objfile. Attributes = objfile. Attributes XOR 2
However, the XOR operator is not particularly clever: It only converts a switch from one state to another. If the switch is on, turn it off. If the switch is off, turn it on. This is why we check the current state of the file. If the file is hidden, use XOR to switch the switch from open to off. But what should I do if the file is not hidden? In this case, the conversion switch is not required. This will hide the file. (Because XOR switches the switch from off to on .) Therefore, check the value of each file before starting the conversion switch.
Run this script-Find you. All of them are available! -All previously hidden files will reappear. Then it's your turn to hide and see if these files can find you.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.