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

LVL 50
DaveAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

conradjonesCommented:
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
0
DaveAuthor Commented:
It isn't the current version I am looking to retrieve. M existing code already achieves this in two lines
0
TakedaTCommented:
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.
0
JavaScript Best Practices

Save hours in development time and avoid common mistakes by learning the best practices to use for JavaScript.

RobSampsonCommented:
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.
0
DaveAuthor Commented:
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
0
RobSampsonCommented:
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.
0
DaveAuthor Commented:
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
0
RobSampsonCommented:
Dave,

As far as I'm aware, you can't.  That's like trying to copy a file from a machine that is not on, it won't respond to any queries.

As I mentioned, the only way to know that path when the XP is off, is for that XP machine (per user) to have written the user's My Docs path to somewhere that accessible.

A logon script could record that info for you, and then you can run a script at any time over that repository of files to identify the location for any user.

Regards,

Rob.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
DaveAuthor Commented:
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

0
RobSampsonCommented:
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.
0
DaveAuthor Commented:
thx Rob
0
RobSampsonCommented:
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.
0
DaveAuthor Commented:
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
 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.