Link to home
Start Free TrialLog in
Avatar of rrangel8
rrangel8

asked on

vbscript - if FolderExists question

Not sure what is going on with this script. I am trying to grab data from each users application data folder. If the file exists then echo. If you want to test then you can put a known folder.

Option Explicit

'Variables
Dim objShell,FSO
Dim strUserProfile,strAppData,objFile


Wscript.echo "working so far"

' Get the current users profile folder
Set objShell = CreateObject("WScript.Shell")
strUserProfile=objShell.ExpandEnvironmentStrings("%USERPROFILE%")
strAppData=objShell.ExpandEnvironmentStrings("%APPDATA%")
' Set reference to the file system
Set FSO = createobject("Scripting.FileSystemObject")

' Check if each folder exists.

If FSO.FolderExists(strUserProfile & strAppData) & "\Microsoft" = True Then
WScript.Echo "im here"
Else
WScript.Echo "not here"
End If
Avatar of rlandquist
rlandquist
Flag of United States of America image

%USERPROFILE% = C:\Users\YOURNAME
%APPDATA% = C:\Users\YOURNAME\AppData

So you are saying you are looking for a folder named:
C:\Users\YOURNAMEC:\Users\YOURNAME\AppData\Microsoft

You just need
If FSO.FolderExists(strAppData) & "\Microsoft" = True Then

To check variable locations open a command prompt and type set and press enter


Avatar of rrangel8
rrangel8

ASKER

this should work for all users with a profile on a machine.
Oh, then you need to approach this a little different.

Try this:
'Create Objects
Set objShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")

'Get special folder paths
strAllUserProfile = objShell.ExpandEnvironmentStrings("%USERPROFILE%")

'Get Path of profile folder
strAllProfiles = objFSO.GetParentFolderName(strAllUserProfile)
Set objProfiles = objFSO.GetFolder(strAllProfiles)

'Exclude System Default Profiles
Set SkipProfilesArray = CreateObject("Scripting.Dictionary")
SkipProfilesArray.Add "Administrator", "Administrator"
SkipProfilesArray.Add "All Users", "All Users"
SkipProfilesArray.Add "Default User", "Default User"
SkipProfilesArray.Add "LocalService", "LocalService"
SkipProfilesArray.Add "NetworkService", "NetworkService"

'Check date of ntuser.dat in each profile folder and delete if criteria meet
For Each strUserProfile In objProfiles.SubFolders
    If Not SkipProfilesArray.Exists(strUserProfile.Name) Then
        If objFSO.FolderExists(strUserProfile & "\Desktop") Then
            WScript.Echo "Folder Found for " & strUserProfile.Name
        Else
            WScript.Echo "Folder Missing or No Access for " & strUserProfile.Name
        End If
    End If
Next

WScript.Echo VbCrLf & "Script Finished"

Open in new window

sorry guys...i was out of the office Friday. will test out today.

appreciate your help!
RLANDQUIST,

thanks for the input. I copied and pasted the script on my VM. I changed the name of the folder to a known existing folder in each users profile in Application Data and it did not pick up the folder. What do you think? I have funk software, ICA CLIENT IDENTITIES, MICROSOFT, VANDYKE, WINBATCH in the application data folder and didn't pick up any of these.

RLANDQUIST,

thanks for the input. I copied and pasted the script on my VM. I changed the name of the folder to a known existing folder in each users profile in Application Data and it did not pick up the folder. What do you think? I have funk software, ICA CLIENT IDENTITIES, MICROSOFT, VANDYKE, WINBATCH in the application data folder and didn't pick up any of these.

ASKER CERTIFIED SOLUTION
Avatar of rlandquist
rlandquist
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ahhh...i tried it earlier with "application data\microsoft" not like yours "\application data\microsoft"

Your the man.

So, let me expand on this if you will. Will this same logic work for FILES?
th