We help IT Professionals succeed at work.

vbscript - if FolderExists question

rrangel8
rrangel8 asked
on
Medium Priority
790 Views
Last Modified: 2012-05-11
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
Comment
Watch Question

%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


Author

Commented:
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

Author

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

appreciate your help!

Author

Commented:
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.

Author

Commented:
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.

Unlock this solution and get a sample of our free trial.
(No credit card required)
UNLOCK SOLUTION

Author

Commented:
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?
Yes.

Author

Commented:
th
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a sample view!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.