I'm having difficulty in getting my script to work on a server. The script works perfectly on my local machine by searching through a specified directory for folders that meet the criteria specified in the script. The script then writes the list of folders to a text file, such as CSV and then proceeds to delete these folders.
However, for some odd reason, when I try to run it on one of the file servers, the script runs but brings back a runtime error: permission denied.
Here is the code:
'*******************************************************************************
'* File: EmptyFolders.vbs
'* Author:
'* Purpose: This script will delete empty folders that are older than 2 days from a
'* specified directory and write the list to a text file.
'* Version: 1.01 (May 09, 2008)
'* Technology: VBScript
'* Requirements: Windows 2000 or later
'*******************************************************************************
'On Error Resume Next
Option Explicit
Dim arrFolders()
Dim objFSO
Dim objFile
Dim intSize
Dim intDaysOld
Dim objSubfolder
Dim strFolderName
Dim strFolder
Dim i
Dim strDirPath
Dim colNamedArguments
Dim strLogDirPath
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colNamedArguments = WScript.Arguments.Named
If Not colNamedArguments.Exists("Path") Then 'Checks for Path argument
Wscript.Echo "Usage: /Path:<dirpath> and /LogPath:<logdirpath> required."
Wscript.Quit
Else
strDirPath = colNamedArguments.Item("Path")
End If
If Not colNamedArguments.Exists("LogPath") Then 'Checks for LogPath argument
Wscript.Echo "Usage: /Path:<dirpath> and /LogPath:<logdirpath> required."
Wscript.Quit
Else
strLogDirPath = colNamedArguments.Item("LogPath")
Wscript.Echo "Directory Path: " & strDirPath
Wscript.Echo "Log Directory Path: " & strLogDirPath
End If
If objFSO.FolderExists(strDirPath) Then 'Checks if directory exists
Else
Wscript.Echo "Directory does not exist."
Wscript.Quit
End If
Set objFile = objFSO.OpenTextFile(strLogDirPath, ForWriting, True)
intSize = 0 'Initial size of array
intDaysOld = 2 'Folder age in days
ShowSubFolders objFSO.GetFolder(strDirPath) 'Calls subroutine
Sub ShowSubFolders(Folder)
For Each objSubfolder In Folder.SubFolders
If objSubfolder.Size = 0 And _
objSubfolder.DateLastModified < (Date() - intDaysOld) Then 'Checks age of folders
strFolderName = objSubfolder.Path
ReDim Preserve arrFolders(intSize) 'Adjusts dynamic array size
arrFolders(intsize) = strFolderName
intSize = intSize + 1 'Increases array list for each item
Wscript.Echo "Path: " & objSubfolder.Path 'Displays path of empty folder
Wscript.Echo "Size: " & objSubfolder.Size 'Displays size of empty folder
End If
ShowSubFolders objSubfolder 'Loops subroutine
Next
End Sub
If intSize > 0 Then 'Checks size of array
For i = Ubound(arrFolders) To 0 Step -1 'Reverses the array
strFolder = arrFolders(i)
objFile.WriteLine strFolder & " " & Now 'Writes array list, date and time to log
objFSO.DeleteFolder(strFolder) 'Deletes subfolders, uncomment to delete
Next
Else
objFile.WriteLine "No folders were found. " & Now 'Writes output to log
End If
objFile.Close
However, for some odd reason, when I try to run it on one of the file servers, the script runs but brings back a runtime error: permission denied.
Here is the code:
'*******************************************************************************
'* File: EmptyFolders.vbs
'* Author:
'* Purpose: This script will delete empty folders that are older than 2 days from a
'* specified directory and write the list to a text file.
'* Version: 1.01 (May 09, 2008)
'* Technology: VBScript
'* Requirements: Windows 2000 or later
'*******************************************************************************
'On Error Resume Next
Option Explicit
Dim arrFolders()
Dim objFSO
Dim objFile
Dim intSize
Dim intDaysOld
Dim objSubfolder
Dim strFolderName
Dim strFolder
Dim i
Dim strDirPath
Dim colNamedArguments
Dim strLogDirPath
Const ForWriting = 2
Set objFSO = CreateObject("Scripting.FileSystemObject")
Set colNamedArguments = WScript.Arguments.Named
If Not colNamedArguments.Exists("Path") Then 'Checks for Path argument
Wscript.Echo "Usage: /Path:<dirpath> and /LogPath:<logdirpath> required."
Wscript.Quit
Else
strDirPath = colNamedArguments.Item("Path")
End If
If Not colNamedArguments.Exists("LogPath") Then 'Checks for LogPath argument
Wscript.Echo "Usage: /Path:<dirpath> and /LogPath:<logdirpath> required."
Wscript.Quit
Else
strLogDirPath = colNamedArguments.Item("LogPath")
Wscript.Echo "Directory Path: " & strDirPath
Wscript.Echo "Log Directory Path: " & strLogDirPath
End If
If objFSO.FolderExists(strDirPath) Then 'Checks if directory exists
Else
Wscript.Echo "Directory does not exist."
Wscript.Quit
End If
Set objFile = objFSO.OpenTextFile(strLogDirPath, ForWriting, True)
intSize = 0 'Initial size of array
intDaysOld = 2 'Folder age in days
ShowSubFolders objFSO.GetFolder(strDirPath) 'Calls subroutine
Sub ShowSubFolders(Folder)
For Each objSubfolder In Folder.SubFolders
If objSubfolder.Size = 0 And _
objSubfolder.DateLastModified < (Date() - intDaysOld) Then 'Checks age of folders
strFolderName = objSubfolder.Path
ReDim Preserve arrFolders(intSize) 'Adjusts dynamic array size
arrFolders(intsize) = strFolderName
intSize = intSize + 1 'Increases array list for each item
Wscript.Echo "Path: " & objSubfolder.Path 'Displays path of empty folder
Wscript.Echo "Size: " & objSubfolder.Size 'Displays size of empty folder
End If
ShowSubFolders objSubfolder 'Loops subroutine
Next
End Sub
If intSize > 0 Then 'Checks size of array
For i = Ubound(arrFolders) To 0 Step -1 'Reverses the array
strFolder = arrFolders(i)
objFile.WriteLine strFolder & " " & Now 'Writes array list, date and time to log
objFSO.DeleteFolder(strFolder) 'Deletes subfolders, uncomment to delete
Next
Else
objFile.WriteLine "No folders were found. " & Now 'Writes output to log
End If
objFile.Close