Quantcast
Channel: VBForums - ASP, VB Script
Viewing all articles
Browse latest Browse all 688

FolderExists function

$
0
0
I'm brand new to writing in vbscript, so I apologize for my lack of knowledge as I was asked to write this out of the blue.

I basically have to write a vbscript that will check every file (and those within subfolders) and subfolder on a drive.
If they're older than whatever days it needs to delete them.

I created a MAIN folder with several files and two subfolders with several files in it them as well.
I tried testing it from the cmd prompt on my desktop and my script worked perfect as far as writing the path/name of the files and subfolders that were older than whatever time I set at the top.

So, I decided to go ahead and do the testing of the Delete portion and received a runtimer error: Type mismatch: 'objFSO.FolderExists'
It deleted the files in the main folder correctly and wrote the path/name to the output file.
It deleted the subfolder it was supposed to along with the files within it correctly and wrote the path/name to the output file.
It did not delete the second subfolder which is correct because it shouldn't have.

I'm not exactly sure how to test this in debug. Why am I getting an error at objFSO.FolderExists(objSubFolder)? Is it because I did a objSubFolder.Delete True a few lines above? Should I have declared a variable chkSub = objSubFolder.Path first and used that within the FolderExists() function since objSubFolder is being deleted?

Hope that makes sense and thx! :)


Code:

'Only OutputFile, StartingFolderPath, and TimeCheck need to be modified

Set objFSO = CreateObject("Scripting.FileSystemObject")

'Output file listing files to be deleted
OutputFile = "C:\Users\abc1234\Desktop\Files_Planned_For_Deletion.txt"
Set objTextFile = objFSO.CreateTextFile(OutputFile, True)

'assigns the starting folder
StartingFolderPath = "C:\Users\abc1234\Desktop\VBscript_Test"

'anything older than this many days will be removed
'in DateDiff() Functions below m=month, d=day, h=hour, n=minute, s=second
TimeType = "h"
TimeCheck = 6

'declares the starting folder
Set objFolder = objFSO.GetFolder(StartingFolderPath)

'declares the files in the starting folder
Set colFiles = objFolder.Files

'loops through the files in the starting folder and sees if they need to be deleted
For Each objFile in colFiles
    DeleteFiles objFile
Next


'calls the function to loop through all subfolders
ShowSubFolders objFolder


'calls the function to loop through all files including those in subfolders
ShowSubFolderFiles objFolder


objTextFile.Close
Set objFSO = Nothing
Set objTextFile = Nothing
Set objFolder = Nothing
Set colFiles = Nothing


Sub ShowSubFolders(Folder)
'loops through the subfolders in the folder sent in through the arguement
For Each Subfolder in Folder.SubFolders
        Set objSubFolder = objFSO.GetFolder(Subfolder.Path)

        If DateDiff(TimeType, objSubfolder.DateLastModified,Now) > TimeCheck Then
                'WScript.StdOut.Write (objSubFolder.Path & vbCrLf)  'Writes foldername to screen
                objTextFile.Write(objSubFolder.Path & vbCrLf)  'Writes foldername to output file
                objSubFolder.Delete True  'Deletes targeted foldername
        End If
       
        'recalls this function to check for subfolders again if the subfolder exists
        'this allows you to infinitly search for subfolders within subfolders
        If objFSO.FolderExists(objSubFolder) Then
                ShowSubFolders objSubFolder
        End If
Next
Set objSubFolder = Nothing
End Sub


Sub ShowSubFolderFiles(Folder)
'loops through the subfolders in the folder sent in through the arguement
For Each Subfolder in Folder.SubFolders
        Set objSubFolder = objFSO.GetFolder(Subfolder.Path)
        Set colFiles = objSubFolder.Files

        'loops through the files in the subfolder and sees if they need to be deleted
        For Each objFile in colFiles
                DeleteFiles objFile
        Next
       
        'recalls this function to check for subfolders again
        'this allows you to infinitly search for subfolders within subfolders
        ShowSubFolderFiles objSubfolder
Next
Set objSubFolder = Nothing
Set colFiles = Nothing
End Sub


Sub DeleteFiles(chkFile)
'Deletes files if they're older than set number of days
If DateDiff(TimeType, chkFile.DateCreated, Now) > TimeCheck Then
        'WScript.StdOut.Write(chkFile.Path & vbCrLf)  'Writes filename to screen
        objTextFile.Write(chkFile.Path & vbCrLf)    'Writes filename to output file
        chkFile.Delete True  'Deletes targeted filename
End If
End Sub


Viewing all articles
Browse latest Browse all 688

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>