Link to home
Start Free TrialLog in
Avatar of Dave
DaveFlag for Australia

asked on

Return the My Documents automatically for each installed OS

I'm running VBA to return the attributes of MP3 files. The OS version is important as the file attributes change between windows versions

The code below will return the My Documents folder of the current OS, plus it will provide all OS versions on the machine. What I'm chasing is whether there is a method to apply the SpecialFolder technique to find the My Documents folder for a given OS which may not be active.

For example if I have XP and Vista on a machine, with Vista active, can I automatically find the My Documents folder for the XP installation once i have the OS info from the WMI below?

Regards

Dave
Set objws = CreateObject("wscript.shell")
    strMyDoc = objws.specialfolders("MyDocuments")
  
  strcomputer = "."
Set objWMIService = GetObject("winmgmts:" & "{impersonationLevel=impersonate}!\\" & strcomputer & "\root\cimv2")
Set colOperatingSystems = objWMIService.ExecQuery("Select * from Win32_OperatingSystem")
For Each objOperatingSystem In colOperatingSystems
strOS = objOperatingSystem.Caption
Next

Open in new window

Avatar of conradjones
conradjones

Maye you would be better off reading the registry?


HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Explorer\Shell Folders

Value Name: Personal
Value Type: REG_SZ
Avatar of Dave

ASKER

It isn't the current version I am looking to retrieve. M existing code already achieves this in two lines
Im pretty sure that WMI wont read the OS that is not running at the time.  Maybe you could have the script read the boot.ini file to see what OSs are installed and go from there.
Hi, You cannot read any information about a computer that is not on, as it cannot accept any WMI connections.  The only way to obtain information from a computer that is currently off, is if that computer had written that information to somewhere external for you, prior to turning off.  Perhaps you could run a logon script that would dump the computer name, user name, and My Documents location.

Regards,

Rob.
Avatar of Dave

ASKER

Thanks gents.

Although to be clear I am working with the local computer that the code is running on (ie strComputer = ".")

So the code above is reading and returning the installed OS with

For Each objOperatingSystem In colOperatingSystems
strOS = objOperatingSystem.Caption
Next

Cheers

Dave
If you're running locally, then you can use

Const MY_DOCUMENTS = &H5&
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace(MY_DOCUMENTS)
strPath = objFolder.Self.Path

to get the current path to the My Documents folder for the current user.

Regards,

Rob.
Avatar of Dave

ASKER

Rob,

I have the current path with this code

Set objws = CreateObject("wscript.shell")
    strMyDoc = objws.specialfolders("MyDocuments")

My question here relates to running two or more  OS on the one machine, as to how do I find the My Documents folder programmatically for the OS that are not currentlly running

  1. Ie I have XP and Vista on a machine
  2. Vista is currently running
  3. How do I get the My Documents path for XP automatically? (without hard coding a drive and the normal path)
Cheers

DAve
ASKER CERTIFIED SOLUTION
Avatar of RobSampson
RobSampson
Flag of Australia 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
Avatar of Dave

ASKER

Rob,

Thx - this is sounding too complex. So I will mark it answered as such.

A side question.

My WMI code returns all the installed OS.  Is there a better way to retrieve only the active OS? I need to distinguish between XP and Vista/Windows &

Dave

Dave, I don't have any dual boot systems, but I'm surprised that Win32_OperatingSystem shows all installed OSs, and not just details on the active one.  If you run ScriptoMaticV2.hta and check the Win32_OperatingSystem properties, is there anything there that might tell you what's active?

Rob.
Avatar of Dave

ASKER

thx Rob
Hi, according to this:
http://msdn.microsoft.com/en-us/library/aa394239(VS.85).aspx

It states "If a computer has multiple operating systems installed, this class only returns an instance for the currently active operating system."

So now I'm really surprised you saw more than one.....

Thanks for the grade anyhow.

Regards,

Rob.
Avatar of Dave

ASKER

Not such a surprise - I had assumed that the loop was written to return mutiple OS as that was the code layout. Which makes sense as I wondered why I couldn't return a specific OS using an Index.  This is a first for me, ie a For Each loop designed to return only a single item.
Cheers
Dave