I am currently working on a VBscript that will delete any files with a "Modify date" of older than 90 days. I am able to get the script to work correctly if I use the absolute path ("C:\<Username>\OneDrive for Business") however when I try to use the %Useprofile% to make the path relative the script fails. I have done some research no delacing Windows Variables however I'm not a scripting expect and am a little confused. Here is the current script that I am using without trying to use the Windows variable:
Const strPath = "c:\KCraig\OneDrive for Business"
Dim objFSO
Set objFSO = CreateObject("Scripting.FileSystemObject")
Call Search (strPath)
WScript.Echo"Done."
Sub Search(str)
Dim objFolder, objSubFolder, objFile
Set objFolder = objFSO.GetFolder(str)
For Each objFile In objFolder.Files
If objFile.DateLastModified < (Now() - 90) Then
objFile.Delete(True)
End If
Next
For Each objSubFolder In objFolder.SubFolders
Search(objSubFolder.Path)
' Files have been deleted, now see if
' the folder is empty.
If (objSubFolder.Files.Count = 0) Then
objSubFolder.Delete True
End If
Next
End Sub
________________________________
Based on a little research I believe I should add this piece to the script (see below). However when I do the script is stall unable to recognize the %userprofile% variable. Any assistance or tips would be greatly appreciated. Thank You.
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")