Use the %UserProfile% variable within a VBscript

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%")

March 24th, 2015 1:53pm

Hi,

That should work just fine. Run this to see:

Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
MsgBox strHomeFolder

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 1:59pm

Are you concatenating your strPath correctly?

Can you post the script you are trying when using the %userprofile% variable? or at least the strPath variable declaration.

March 24th, 2015 2:08pm

Thank You for the tip, however once I added the suggested piece to the beginning of my script I now when I run the script I receive the error "Expected Literal Constant". I believe my issue is in the way I am referencing the variable later in the script, specifically line 4 of my script where I try to create the "Const" string path. As I mentioned before I'm no real scripting expect in fact this is my first VBscript but if you have an explanation of what I am doing wrong I'd love to hear is as I'd love to learn more. Here is the script now that I have added the suggested portion: 

DIM objFSO, oShell
Set oShell = CreateObject("WScript.Shell")
strHomeFolder = oShell.ExpandEnvironmentStrings("%USERPROFILE%")
Const strPath = strHomeFolder & "\Onedrive for Buisness"     <------ (I believe my issue is here)
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


Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 2:18pm

Thank You for the response, I completely agree about the strPath declaration and have posted the script below. 
March 24th, 2015 2:22pm

I think the problem is that you are declaring the variable as a constant, and passing another variable into it.

If you change the problem line by removing the Const to:

strPath = strHomeFolder & "\Onedrive for Business"  

and try that, it should work - hopefully!

Free Windows Admin Tool Kit Click here and download it now
March 24th, 2015 2:28pm

By default OneDrive is here "%UserProfile%\OneDrive"

To get the folder we would do this:

Set fso = CreateObject("Scripting.FileSystemObject")

strOneDrivePath = CreateObject("WScript.Shell").ExpandEnvironmentStrings("%USERPROFILE%") & "\OneDrive"
Search strOneDrivePath
WScript.Echo"Done."

Sub Search(str)

    Set folder = fso.GetFolder(str)
    For Each file In folder.Files
        If file.DateLastModified < (Now() - 90) Then
            file.Delete True
        End If
    Next
    
    For Each subFolder In folder.SubFolders
        Search subFolder.Path 
        If subFolder.Files.Count = 0  Then
            subFolder.Delete True
        End If
    Next

End Sub

March 24th, 2015 3:42pm

This topic is archived. No further replies will be accepted.

Other recent topics Other recent topics