Reputation: 13
My code below albe to Unhide all hidden files, folders, subfolders and subfiles. The problem is it'll stop if any access denied path. How to make the code skip access denied path and continue with other paths.
Dim MyDrive As String = "D:\"
Dim FileCounter As Integer = 0
Dim FolderCounter As Integer = 0
Dim DriveObj As New IO.DirectoryInfo(MyDrive)
Dim Files As IO.FileInfo() = DriveObj.GetFiles("*.*", IO.SearchOption.AllDirectories)
Dim Directories As IO.DirectoryInfo() = DriveObj.GetDirectories("*.*", IO.SearchOption.AllDirectories)
Dim Filename As IO.FileSystemInfo
For Each Filename In Files
On Error Resume Next
If (IO.File.GetAttributes(Filename.FullName) And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
' Show the file.
IO.File.SetAttributes(Filename.FullName, IO.FileAttributes.Normal)
FileCounter = FileCounter + 1
End If
Next
Dim DirectoryName As IO.DirectoryInfo
For Each DirectoryName In Directories
On Error Resume Next
If (IO.File.GetAttributes(DirectoryName.FullName) And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
' Show the folder.
IO.File.SetAttributes(DirectoryName.FullName, IO.FileAttributes.Normal)
FolderCounter = FolderCounter + 1
End If
Next
Upvotes: 1
Views: 3478
Reputation: 802
When I was testing code, I realised that DriveObj.GetFiles(...)
and DriveObj.GetDirectories(...)
are stopping when access is denied, not For
loops. To fix that, you have to use recursive file and directory listing.
Private Function GetFilesRecursive(ByVal initial As String) As List(Of String)
' This list stores the results.
Dim result As New List(Of String)
' This stack stores the directories to process.
Dim stack As New Stack(Of String)
' Add the initial directory
stack.Push(initial)
' Continue processing for each stacked directory
Do While (stack.Count > 0)
' Get top directory string
Dim dir As String = stack.Pop
Try
' Add all immediate file paths
result.AddRange(Directory.GetFiles(dir, "*.*"))
' Loop through all subdirectories and add them to the stack.
Dim directoryName As String
For Each directoryName In Directory.GetDirectories(dir)
stack.Push(directoryName)
Next
Catch ex As Exception
End Try
Loop
' Return the list
Return result
End Function
Private Function GetDirectoriesRecursive(ByVal initial As String) As List(Of String)
' This list stores the results.
Dim result As New List(Of String)
' This stack stores the directories to process.
Dim stack As New Stack(Of String)
' Add the initial directory
stack.Push(initial)
' Continue processing for each stacked directory
Do While (stack.Count > 0)
' Get top directory string
Dim dir As String = stack.Pop
Try
' Add all immediate file paths
result.AddRange(Directory.GetDirectories(dir, "*.*"))
' Loop through all subdirectories and add them to the stack.
Dim directoryName As String
For Each directoryName In Directory.GetDirectories(dir)
stack.Push(directoryName)
Next
Catch ex As Exception
End Try
Loop
' Return the list
Return result
End Function
Private Sub Unhide()
Dim MyDrive As String = "D:\"
Dim FileCounter As Integer = 0
Dim FolderCounter As Integer = 0
Dim Files As List(Of String) = GetFilesRecursive(MyDrive)
Dim Directories As List(Of String) = GetDirectoriesRecursive(MyDrive)
For Each Filename In Files
If (IO.File.GetAttributes(Filename) And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
'Show the file.
IO.File.SetAttributes(Filename, IO.FileAttributes.Normal)
FileCounter = FileCounter + 1
End If
Next
For Each DirectoryName In Directories
If (IO.File.GetAttributes(DirectoryName) And IO.FileAttributes.Hidden) = IO.FileAttributes.Hidden Then
'Show the folder.
IO.File.SetAttributes(DirectoryName, IO.FileAttributes.Normal)
FolderCounter = FolderCounter + 1
End If
Next
End Sub
Upvotes: 1