Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Permission Issue With Accessing Subfolder 2

Status
Not open for further replies.

JustScriptIt

Technical User
Oct 28, 2011
73
US
I modified the script from

Code:
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim strDir, objDir
strDir = "d:\"
Set objDir = FSO.GetFolder(strDir)
PrintFiles(objDir)

Sub PrintFiles(pCurrentDir)
    Dim aItem, sBaseName
    
   
   For Each aItem In pCurrentDir.Files
      If fso.GetExtensionName(aItem.Path) = "vbs" Then
         sBaseName = fso.GetParentFolderName(aItem.path) & _
              fso.GetBaseName(aItem.path)

         Wscript.echo sBaseName
         Wscript.echo

         
      End If
   Next

 
   For Each aItem In pCurrentDir.SubFolders
      PrintFiles(aItem)
   Next


End Sub

When I execute one of the subfolder causes

Code:
(14, 4) Microsoft VBScript runtime error: Permission denied

When I execute in debug mode, the program reaches

Code:
For Each aItem In pCurrentDir.SubFolders
      PrintFiles(aItem)

Few times beore it throws the runtime error.

How to troubleshoot?
 
It appears that you don't have permissions on one or more of the subfolders on the D drive.

Code:
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim strDir, objDir
strDir = "d:\"
Set objDir = FSO.GetFolder(strDir)
PrintFiles(objDir)

Sub PrintFiles(pCurrentDir)
    Dim aItem, sBaseName
    
   
   For Each aItem In pCurrentDir.Files
      If fso.GetExtensionName(aItem.Path) = "vbs" Then
         sBaseName = fso.GetParentFolderName(aItem.path) & _
              fso.GetBaseName(aItem.path)

         Wscript.echo sBaseName
         Wscript.echo

         
      End If
   Next

 
   For Each aItem In pCurrentDir.SubFolders
      [COLOR=blue yellow]Wscript.echo(aItem)[/color]
      PrintFiles(aItem)
   Next


End Sub

Add the line highlighted above and take note of the last folder name returned before the error occurs.
 
Now I modified the code to handle the permissions error, but now I have another problem.

If the program sees a .vbs file in the d:\ directory, it prints it as

d:\\file.vbs

but if the .vbs is in a subdirectory, it prints fine, i.e.

d:\folder\file.vbs

How to correct this? Below is code:

{CODE]
Option Explicit
Dim fso
Set fso = CreateObject("Scripting.FileSystemObject")

Dim strDir, objDir
strDir = "d:\"
Set objDir = FSO.GetFolder(strDir)


''''''''''Declare Sub Routines''''''''''

Sub HandleError()
On Error Goto 0
Err.Clear
End Sub

Sub PrintFiles(pCurrentDir)
Dim aItem, sBaseName


For Each aItem In pCurrentDir.Files
If fso.GetExtensionName(aItem.Path) = "vbs" Then
sBaseName = fso.GetParentFolderName(aItem.path) & _
"\" & fso.GetBaseName(aItem.path)

Wscript.echo sBaseName
Wscript.echo


End If
Next

On Error Resume Next
For Each aItem In pCurrentDir.SubFolders
If (Err.Number <> 0) Then
HandleError()
Wscript.echo aItem & " cannot be accessed"
Wscript.echo
Else
PrintFiles(aItem)

End If
Next


End Sub


''''''''''End of Sub Routine Declaration''''''''''

PrintFiles(objDir)
[/CODE]
 
Why not replace this:
strDir = "d:\"
with this ?
strDir = "d:"

Hope This Helps, PH.
FAQ219-2884
FAQ181-2886
 
I tried replacing d:\ with d:, nut I get the same output :-(
 
This is because GetParentFolderName returns a "\" when the file is in the root. You will need to look at the GetParentFolderName result, and either strip off the extra \ if it's present, or avoid adding another \ when it is present.
 
It worked. See modified subroutine for PrintFiles

Code:
Sub PrintFiles(pCurrentDir)
	Dim aItem, sParentName, sBaseName
    
   	
	For Each aItem In pCurrentDir.Files
		If fso.GetExtensionName(aItem.Path) = "vbs" Then
	
			If Right(fso.GetParentFolderName(aItem.path),1) = "\" Then
				sParentName = Replace(fso.GetParentFolderName(aItem.path),"\","")
			Else
				sParentName = fso.GetParentFolderName(aItem.path)
			End If

			sBaseName = sParentName & _
				"\" & fso.GetBaseName(aItem.path)

			Wscript.echo sBaseName
			Wscript.echo

         
		End If
   	Next

 	On Error Resume Next
	For Each aItem In pCurrentDir.SubFolders	
		If (Err.Number <> 0) Then
	  		HandleError()
			Wscript.echo aItem & " cannot be accessed"
			Wscript.echo
		Else
			PrintFiles(aItem)
	
		End If
   	Next


End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top