?
Solved

Permission Denied Error on VB Script

Posted on 2009-05-06
4
Medium Priority
?
495 Views
Last Modified: 2012-05-06
I receive a permission denied error on line 34, character 10 when I run this script against a folder location that has multiple nested folder levels and lots of files.  I am running the script against the root folder.  I have gone back through and reapplied permissions to every single file and folder for 'Everyone'.  I even made a copy of the entire folder structure, then manually deleted the files and folders using Windows explorer.  This confirmed that there was not a permission issue with accessing the files.  I am running the script locally on the actual computer the folder if located on.  I can run the script against a single folder within the root folder and it works okay.  I can also add each folder into the script text file and it will delete a few then stop with a permission issue.  I assume this has something to do with a filename length or folder name length.

Again, this is not an actual permission issue as I can manually delete all of the files and folders myself.  The script is run locally so it does not have permission issues.
Dim fso, startFolder, OlderThanDate
Dim strFilename, strPath
 
Set fso = CreateObject("Scripting.FileSystemObject")
strFilename = "c:\work\ee\urgent.txt"
OlderThanDate = DateAdd("d", -60, Date)  ' 60 days (adjust as necessary)'
 
      set objFSO = CreateObject("Scripting.FileSystemObject")
      if objFSO.FileExists(strFilename) Then
            set objFile = objFSO.GetFile(strFilename)
            Dim TextStream
            Set TextStream = objFile.OpenAsTextStream(1,0)
            Dim Line,i
 
            Do While Not TextStream.AtEndOfStream
                  strPath = trim(TextStream.readline)
                  DeleteOldFiles strPath, OlderThanDate
                  RemoveEmptyFolders strPath
            Loop
 
            Set TextStream = nothing
      Else
            Msgbox "Input File not found"
      End If
Msgbox "Old Backup files have been deleted...Done !!"
 
Function DeleteOldFiles(folderName, BeforeDate)
   Dim folder, file, fileCollection, folderCollection, subFolder
 
   Set folder = fso.GetFolder(folderName)
   Set fileCollection = folder.Files
   For Each file In fileCollection
      If file.DateLastModified < BeforeDate Then
         fso.DeleteFile(file.Path)
      End If
   Next
 
    Set folderCollection = folder.SubFolders
    For Each subFolder In folderCollection
       DeleteOldFiles subFolder.Path, BeforeDate
    Next
 
End Function
 
'====================
Sub RemoveEmptyFolders(sFolderName)
 
        Dim objFolder, objSubFolders, objFiles
        Dim xFolder, xFile
        Dim ret
 
        'On Error Resume Next'
 
        Set objFolder = fso.GetFolder(sFolderName)
        Set objSubFolders = objFolder.SubFolders
        Set objFiles = objFolder.Files
 
        'count files'
        iTotFiles=iTotFiles + objFiles.count
        'set to remove readonly
        Const READONLY = 1
        For Each xFile In objFiles
                xFile.Attributes = xFile.Attributes And Not READONLY
        Next
 
        For Each xFolder In objSubFolders
           xFolder.Attributes = xFolder.Attributes And Not READONLY
           RemoveEmptyFolders (xFolder.Path)
        Next
 
        'count directories found'
        itotdirs=itotdirs+1
        
        ' If the folder is empty then delete the folder.'
        If objFiles.Count <= 0 and objSubFolders.count <= 0 Then
                iDelDirs=iDelDirs+1
                fso.DeleteFolder sFolderName, True
        End If
    
End Sub

Open in new window

0
Comment
Question by:murryc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 14

Accepted Solution

by:
rejoinder earned 2000 total points
ID: 24320774
Can you change line 34 to read;
fso.DeleteFile file.Path
or
fso.DeleteFile file.Path, True

See if either of those help.
0
 

Author Comment

by:murryc
ID: 24329963
fso.DeleteFile file.Path, True did the trick.

I have another small tweak.  Let me know if you want to me start a new question for it, since you already answered by first question.  This one might be super easy though.

The script deletes files older than a certain number of days.  I need to tweak it to delete files NEWER than 1-1-2009.  
0
 
LVL 14

Assisted Solution

by:rejoinder
rejoinder earned 2000 total points
ID: 24330020
No problem.

You will have to code the date into the variable OlderThanDate;
OlderThanDate = CDate("01/01/2009")
Change this line;
If file.DateLastModified < BeforeDate Then
to this...
If file.DateLastModified > BeforeDate Then

That should do it.
0
 

Author Comment

by:murryc
ID: 24330805
Worked Perfectly!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is an addendum to the following article: Acitve Directory based Outlook Signature (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_24950055.html) The script is fine, and works in normal client-server domains…
Not long ago I saw a question in the VB Script forum that I thought would not take much time. You can read that question (Question ID  (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/Q_28455246.html)28455246) Here (http…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question