show/hide hidden files quickly

The fastest way that I have figured out to show/hide hidden and system files is to open my computer, go to View, choose Folder options, go to view and click the radial.  I was hoping that there might be some way to create a short cut either right to the last screen or that just changed the setting from show to hide automatically and so that I could put the shortcut on the start menu or desktop.  Does anyone know of a way?  If the solution requires coding, could you provide it?  I don't remember much.  Thanks.
Who is Participating?
ggilmanConnect With a Mentor Commented:
By the way, some of the comments in the module code don't mean a whole lot. I took the code from the online help and modified it some to suit your purpose.
unfortunately, that's it!
You can make a batch file such as hide.bat and put it on your desktop.  Place the command "attrib +h *" inside the file to hide all files within the directory. Here is the options for the command attrib.  

ATTRIB [+R | -R] [+A | -A ] [+S | -S] [+H | -H] [[drive:] [path] filename] [/S]

  +   Sets an attribute.
  -   Clears an attribute.
  R   Read-only file attribute.
  A   Archive file attribute.
  S   System file attribute.
  H   Hidden file attribute.
  /S  Processes matching files in the current directory and all subdirectories.

Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

To unhide the files type make an another batch file and put the command "attrib -h *"
oh yeah, a batch file!  duh, sorry!
Why not write a VB program that would do it easier for you... and with a nicer interface.

griffenAuthor Commented:
Well, how do I write the VB file?  Or if I use the batch file, I have certain folders that I want to hide/unhide and others that I have system files in that I would like to hide/unhide.  If I put the .bat on the desktop, how will it know which folder I want to hide, and if I put it in the folder, then how do I get to it when it is hidden?
Guys, guys !!!!
You are getting confused.  Re-read griffen's question.  He wants to enable/disable the DISPLAYING of hidden and system files.  He doesn't want to actually hide and unhide hidden and system files.  Right griffen ?

1cell is right.  If the option for this is stored in the registry, good look in creating a quick util to toggle it on and off.  Maybe if the option was stored in an .INI file, you could right a quick and dirty routine to modify the value.
Here's some VB code that will toggle between show all and show neither hidden or system.

part 1: This is a sample form I have. I click a command button which causes the toggle
Const HKEY_CURRENT_USER = &H80000001
Const HIDDEN_SETTINGS_KEY = "Software\Microsoft\Windows\CurrentVersion\Explorer\Advanced"
Enum hiddenValue
    hideAll = 0     'Both hidden and system files are not shown
    showAll = 1     'Show both hidden and system files
    HideHidden = 2  'Hide hidden files but show system files
End Enum
Private Sub Command1_Click()
    Dim retVal As Long
    Dim dataType As Long
    retVal = Val(GetKeyValue(HKEY_CURRENT_USER, HIDDEN_SETTINGS_KEY, "Hidden"))
    Select Case Val(retVal)
    Case hideAll
        Call UpdateKey(HKEY_CURRENT_USER, HIDDEN_SETTINGS_KEY, "Hidden", showAll)
    Case showAll
        Call UpdateKey(HKEY_CURRENT_USER, HIDDEN_SETTINGS_KEY, "Hidden", hideAll)
    Case HideHidden
        'Leave the same??
    End Select
End Sub

Part 2: This is a module

' This module reads and writes registry keys.  Unlike the
' internal registry access methods of VB, it can read and
' write any registry keys with string values.

Option Explicit
'-Registry API Declarations...
Private Declare Function RegCloseKey Lib "advapi32" (ByVal hKey As Long) As Long
Private Declare Function RegCreateKeyEx Lib "advapi32" Alias "RegCreateKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal Reserved As Long, ByVal lpClass As String, ByVal dwOptions As Long, ByVal samDesired As Long, ByRef lpSecurityAttributes As SECURITY_ATTRIBUTES, ByRef phkResult As Long, ByRef lpdwDisposition As Long) As Long
Private Declare Function RegOpenKeyEx Lib "advapi32" Alias "RegOpenKeyExA" (ByVal hKey As Long, ByVal lpSubKey As String, ByVal ulOptions As Long, ByVal samDesired As Long, ByRef phkResult As Long) As Long
Private Declare Function RegQueryValueEx Lib "advapi32" Alias "RegQueryValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal lpReserved As Long, ByRef lpType As Long, ByVal lpData As String, ByRef lpcbData As Long) As Long
Private Declare Function RegSetValueEx Lib "advapi32" Alias "RegSetValueExA" (ByVal hKey As Long, ByVal lpValueName As String, ByVal Reserved As Long, ByVal dwType As Long, ByVal lpData As Long, ByVal cbData As Long) As Long

'- Registry Api Constants...
' Reg Data Types...
Const REG_SZ = 1                         ' Unicode nul terminated string
Const REG_EXPAND_SZ = 2                  ' Unicode nul terminated string
Const REG_DWORD = 4                      ' 32-bit number

' Reg Create Type Values...
Const REG_OPTION_NON_VOLATILE = 0       ' Key is preserved when system is rebooted

' Reg Key Security Options...
Const READ_CONTROL = &H20000
Const KEY_NOTIFY = &H10
                       KEY_CREATE_SUB_KEY + KEY_ENUMERATE_SUB_KEYS + _
                       KEY_NOTIFY + KEY_CREATE_LINK + READ_CONTROL
' Reg Key ROOT Types...
Const HKEY_CLASSES_ROOT = &H80000000
Const HKEY_CURRENT_USER = &H80000001
Const HKEY_LOCAL_MACHINE = &H80000002
Const HKEY_USERS = &H80000003

' Return Value...
Const ERROR_NONE = 0

'- Registry Security Attributes TYPE...
    nLength As Long
    lpSecurityDescriptor As Long
    bInheritHandle As Boolean
End Type

' The resource string will be loaded into a control's property as follows:
' Object      Property
' Form        Caption
' Menu        Caption
' TabStrip    Caption, ToolTipText
' Toolbar     ToolTipText
' ListView    ColumnHeader.Text

Sub LoadResStrings(frm As Form)
  On Error Resume Next
  Dim ctl As Control
  Dim obj As Object
  'set the form's caption
  If IsNumeric(frm.Tag) Then
    frm.Caption = LoadResString(CInt(frm.Tag))
  End If
  'set the controls' captions using the caption
  'property for menu items and the Tag property
  'for all other controls
  For Each ctl In frm.Controls
    If TypeName(ctl) = "Menu" Then
      If IsNumeric(ctl.Caption) Then
        If Err = 0 Then
          ctl.Caption = LoadResString(CInt(ctl.Caption))
        End If
      End If
    ElseIf TypeName(ctl) = "TabStrip" Then
      For Each obj In ctl.Tabs
        If IsNumeric(obj.Tag) Then
          obj.Caption = LoadResString(CInt(obj.Tag))
        End If
        'check for a tooltip
        If IsNumeric(obj.ToolTipText) Then
          If Err = 0 Then
            obj.ToolTipText = LoadResString(CInt(obj.ToolTipText))
          End If
        End If
    ElseIf TypeName(ctl) = "Toolbar" Then
      For Each obj In ctl.Buttons
        If IsNumeric(obj.Tag) Then
          obj.ToolTipText = LoadResString(CInt(obj.Tag))
        End If
    ElseIf TypeName(ctl) = "ListView" Then
      For Each obj In ctl.ColumnHeaders
        If IsNumeric(obj.Tag) Then
          obj.Text = LoadResString(CInt(obj.Tag))
        End If
      If IsNumeric(ctl.Tag) Then
        If Err = 0 Then
          ctl.Caption = LoadResString(CInt(ctl.Tag))
        End If
      End If
      'check for a tooltip
      If IsNumeric(ctl.ToolTipText) Then
        If Err = 0 Then
          ctl.ToolTipText = LoadResString(CInt(ctl.ToolTipText))
        End If
      End If
    End If

End Sub

'sample usage - Debug.Print UpodateKey(HKEY_CLASSES_ROOT, "keyname", "newvalue")
Public Function UpdateKey(KeyRoot As Long, KeyName As String, SubKeyName As String, SubKeyValue As Long) As Boolean
    Dim rc As Long                                      ' Return Code
    Dim hKey As Long                                    ' Handle To A Registry Key
    Dim hDepth As Long                                  '
    Dim lpAttr As SECURITY_ATTRIBUTES                   ' Registry Security Type
    lpAttr.nLength = 50                                 ' Set Security Attributes To Defaults...
    lpAttr.lpSecurityDescriptor = 0                     ' ...
    lpAttr.bInheritHandle = True                        ' ...

    '- Create/Open Registry Key...
    rc = RegCreateKeyEx(KeyRoot, KeyName, _
                        0, REG_DWORD, _
                        REG_OPTION_NON_VOLATILE, KEY_ALL_ACCESS, lpAttr, _
                        hKey, hDepth)                   ' Create/Open //KeyRoot//KeyName
    If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError   ' Handle Errors...
    '- Create/Modify Key Value...
   ' If (SubKeyValue = "") Then SubKeyValue = " "        ' A Space Is Needed For RegSetValueEx() To Work...
    ' Create/Modify Key Value
    rc = RegSetValueEx(hKey, SubKeyName, _
                       0, REG_DWORD, _
                       VarPtr(SubKeyValue), Len(SubKeyValue)) 'LenB(StrConv(Str(SubKeyValue), vbFromUnicode)))
    If (rc <> ERROR_SUCCESS) Then GoTo CreateKeyError   ' Handle Error
    '- Close Registry Key...
    rc = RegCloseKey(hKey)                              ' Close Key
    UpdateKey = True                                    ' Return Success
    Exit Function                                       ' Exit
    UpdateKey = False                                   ' Set Error Return Code
    rc = RegCloseKey(hKey)                              ' Attempt To Close Key
End Function

'sample usage - Debug.Print GetKeyValue(HKEY_CLASSES_ROOT, "COMCTL.ListviewCtrl.1\CLSID", "")
Public Function GetKeyValue(KeyRoot As Long, KeyName As String, SubKeyRef As String) As String
    Dim i As Long                                           ' Loop Counter
    Dim rc As Long                                          ' Return Code
    Dim hKey As Long                                        ' Handle To An Open Registry Key
    Dim hDepth As Long                                      '
    Dim sKeyVal As String
    Dim lKeyValType As Long                                 ' Data Type Of A Registry Key
    Dim tmpVal As String                                    ' Tempory Storage For A Registry Key Value
    Dim KeyValSize As Long                                  ' Size Of Registry Key Variable
    ' Open RegKey Under KeyRoot {HKEY_LOCAL_MACHINE...}
    rc = RegOpenKeyEx(KeyRoot, KeyName, 0, KEY_ALL_ACCESS, hKey) ' Open Registry Key
    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Error...
    tmpVal = String$(1024, 0)                             ' Allocate Variable Space
    KeyValSize = 1024                                       ' Mark Variable Size
    ' Retrieve Registry Key Value...
    rc = RegQueryValueEx(hKey, SubKeyRef, 0, _
                         lKeyValType, tmpVal, KeyValSize)    ' Get/Create Key Value
    If (rc <> ERROR_SUCCESS) Then GoTo GetKeyError          ' Handle Errors
    tmpVal = Left$(tmpVal, InStr(tmpVal, Chr(0)) - 1)

    ' Determine Key Value Type For Conversion...
    Select Case lKeyValType                                  ' Search Data Types...
    Case REG_SZ, REG_EXPAND_SZ                              ' String Registry Key Data Type
        sKeyVal = tmpVal                                     ' Copy String Value
    Case REG_DWORD                                          ' Double Word Registry Key Data Type
        For i = Len(tmpVal) To 1 Step -1                    ' Convert Each Bit
            sKeyVal = sKeyVal + Hex(Asc(Mid(tmpVal, i, 1)))   ' Build Value Char. By Char.
        sKeyVal = Format$("&h" + sKeyVal)                     ' Convert Double Word To String
    End Select
    GetKeyValue = sKeyVal                                   ' Return Value
    rc = RegCloseKey(hKey)                                  ' Close Registry Key
    Exit Function                                           ' Exit
GetKeyError:    ' Cleanup After An Error Has Occured...
    GetKeyValue = vbNullString                              ' Set Return Val To Empty String
    rc = RegCloseKey(hKey)                                  ' Close Registry Key
End Function

I've tested my solution and it works fine for a toggle. You should be able to modify the form code pretty easily to suit your needs & provide a nice interface. As tremblyj mentioned, the setting is stored in the registry. Changing the value to 0,1,2 changes the view options.
griffenAuthor Commented:
Wow, thank you very much.  I must admit though that the only VB I have ever done is from within Access, so I am not sure where to do this.  Do I need MS Visual Basic to create this program.  I think I have an old copy of it somewhere.  Can I use the windows scripting host?  Either way, thank you for your help.
Your best choice would be to use Visual Basic. What I did was create a new standard exe project. Add a code module. Copy all of the code after "Part 2: This is a module" and paste it into the module. Now the part about how you want the rest of the design is really up to you. What I did to test it out was add a command button on the form. Copy the "Part 1" code into the form. Now when you click the command button, it acts as a toggle. But that's just the way I did it.
     Using the same commands I have programmed for the command button, you can just as easily have an application that uses no form and just does the toggle, which may actually be a little more like you want. To do this, create a second Module. In this module create a subroutine "Sub Main". Put the toggle code there. Under project properties, set the start up object to be "Sub Main". Compile and you're done. Again another variation would be to create your own radio buttons on the form and have them choose the mode rather than a toggle. If you decide which way you want to do it and need some more info, feel free to ask & I'll try to help.

I've never used windows scripting host so I can't exactly answer that question. However, for the most part, it should work anywhere you can program in VB, at least anywhere you can call APIs and have access to the registry. Again, I haven't tried modifying the registry other than through VB itself but other than the API calls it's just basic VB. The biggest thing you need is the HIDDEN_SETTINGS_KEY that I had to search for to get your solution.

Of course, if you copy the "Part 1" code, you need "Command1_Click" to be renamed to the name of your command button.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.