momo33 has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm writing a script that processes data from another program. That program writes the data to a location that depends on the the version of MS Windows and the username.

XP: C:\Documents and Settings\<user>\ApplicationData\ Vista/Win7: C:\Users\<user>\AppData\Roaming\

Does someone know a "trick" to get this path?

Thank you

Replies are listed 'Best First'.
Re: windows app-data path
by marto (Cardinal) on Nov 19, 2011 at 10:36 UTC

    I've not used it but File::HomeDir seems to offer this functionality.

Re: windows app-data path
by BrowserUk (Patriarch) on Nov 19, 2011 at 10:49 UTC

    See $ENV{ APPDATA }


    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      momo33, Im not sure if this will help you but I use my perl scripts in XP and win7, and here are the things I use to find the correct paths.. good luck :)

      $user = getlogin(); # get username if(-e(-d("C:/users"))){$users = "users";} #check if exists elsif(-e(-d("C:/Documents and Settings"))){ $users = "Documents and Settings"; } $OS = $^O;

        This approach will fail for example on non-English Windows XP. For example German Windows XP has Dokumente und Einstellungen as the directory name.

Re: windows app-data path
by afoken (Chancellor) on Nov 19, 2011 at 14:57 UTC

    Win32 has a GetFolderPath() function that appears to wrap SHGetSpecialFolderPath(). Call it with one of the CSIDL_xxx constants and you get the folder name.

    Each and every new Windows version has a new API to get the "special" folders' paths, and the old APIs are implemented as wappers for the new APIs. Follow the links to find the details. The current name for the real function seems to be SHGetKnownFolderPath, and it requires a KNOWNFOLDERID. You could use Win32::API to call that function if you detect that your program runs on a modern Windows version.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Thank you for all the answers! Some look very promising and together they will provide the solution.