WindowsDevCenter.com
oreilly.comSafari Books Online.Conferences.

advertisement


AddThis Social Bookmark Button

Cooking with Windows Server, Part 1
Pages: 1, 2, 3

Hiding a File or Folder

Problem

You want to hide a file or folder from view within Windows Explorer.

Solution

Using a graphical user interface

  1. Open Windows Explorer.

  2. Browse to the file or folder you want to hide.

  3. Right-click the file or folder and select Properties.

  4. Check the box beside Hidden (to hide) or uncheck the box (to unhide).

  5. Click OK.

Using a command-line interface

To hide a file, use the attrib.exe command:

> attrib +H <Path>

Here is an example:

> attrib +H d:\mysecretscript.vbs

To unhide a file, use the -H option:

> attrib -H <Path>

Here is an example:

> attrib -H d:\mysecretscript.vbs

Using VBScript

' This code hides or unhides
 a file.
' ------ SCRIPT CONFIGURATION ------
strFile = "<FilePath>"  ' e.g., d:\mysecretscript.vbs
boolHide = True         ' True to hide, False to unhide 
' ------ END CONFIGURATION ---------
set objFSO = CreateObject("Scripting.FileSystemObject")
   
' Change this to GetFolder to hide/unhide a folder
set objFile = objFSO.GetFile(strFile)
   
if boolHide = True then
   if objFile.Attributes AND 2 then
      WScript.Echo "File already hidden" 
   else
      objFile.Attributes = objFile.Attributes + 2 
      WScript.Echo "File is now hidden"
   end if 
else
   if objFile.Attributes AND 2 then
      objFile.Attributes = objFile.Attributes - 2 
      WScript.Echo "File is not hidden"
   else
      WScript.Echo "File is already not hidden" 
   end if 
end if

Discussion

There are many operating system files that are hidden by default. Microsoft did this so you don't get yourself into trouble by accidentally editing or deleting important system files. You also may want to do this if you don't want users to see certain files or folders. The files and folders will still be accessible if the users know the full path; they just won't be visible by default in Windows Explorer. That, however, can be easily circumvented. Windows Explorer provides an option to make all hidden files and folders viewable. From the menu, select ToolsFolder Options. Click the View tab. You just need to select Show hidden files and folders and you'll be able to see them. If you truly don't want users to be able to access certain files or folders, your best bet is to restrict access to them via NTFS permissions.

See Also

MS KB 141276 (How to View System and Hidden Files in Windows)

Robbie Allen is the coauthor of Active Directory, 2nd Edition and the author of the Active Directory Cookbook.


View catalog information for Windows Server Cookbook

Return to the WindowsDevCenter.com.