Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options

writing to a users "My Documents" folder

by sweetblood (Parson)
on Jan 27, 2009 at 21:00 UTC ( #739341=perlquestion: print w/ replies, xml ) Need Help??
sweetblood has asked for the wisdom of the Perl Monks concerning the following question:

It's been quite sometime that I've written any perl code for a windows system, but I need to create an excel file on someones pc. I'd like to just put the file in their "My Documents" directory, I thought that would be easy, but I'm finding otherwise. Since I don't know which user might be running my script I can't simply "hardcode" the path. I know there must be someway to point to it but I'm running out of ideas.


Comment on writing to a users "My Documents" folder
Replies are listed 'Best First'.
Re: writing to a users "My Documents" folder
by Corion (Pope) on Jan 27, 2009 at 21:45 UTC

    Also see File::HomeDir, which will give you the correct paths not only on Windows but also on other operating systems.

Re: writing to a users "My Documents" folder
by ikegami (Pope) on Jan 27, 2009 at 21:06 UTC
    Isn't there an environment variable that contains the path that directory, or at least the user's profile directory? (Not on Windows at the moment.) That's probably the easiest.
      yep "$ENV{USERPROFILE}\\My Documents" does the trick -- Thanks!


Re: writing to a users "My Documents" folder
by boblawblah (Scribe) on Jan 27, 2009 at 21:59 UTC
    perl -e 'print "$_ = $ENV{$_}\n" for sort keys %ENV;'
    Run that. It will print of all your system environmental variables. Which includes the current users 'My Document' folder.

    It should be noted, that if you are using Win32::OLE to create the excel file that simply saying
    will save to the users My Documents folder. (The OLE object does not default to using your programs current working directory).
      but if I want the excel to be saved in my current directory then how do I do?? PS: giving the path has not worked for me.
Re: writing to a users "My Documents" folder
by Sandy (Curate) on Jan 27, 2009 at 21:16 UTC
    If you want to play it safe, do not assume that the user's personal folder is on the C drive.

    The windows registry (at least on my computer) contains the personal folder for the user

    HKEY_CURRENT_USER\ software\ microsoft\ windows\ CurrentVersion\ Explorer\ Shell Folders\ Personal\
    I suppose that there is a CPAN module that should be able to read the Windows registry.


Re: writing to a users "My Documents" folder
by hominid (Curate) on Jan 27, 2009 at 21:10 UTC
    Try getlogin
    my $login_name = getlogin; my $path = "C:\\Documents and Settings\\$login_name\\My Documents\\";
      "Documents and Settings" can change, but %USERPROFILE won't
Re: writing to a users "My Documents" folder
by jdporter (Canon) on Jan 28, 2009 at 13:02 UTC

    use Win32::GetFolderPath ...

    use Win32 qw( CSIDL_PERSONAL ); my $my_documents_path = Win32::GetFolderPath(CSIDL_PERSONAL);

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://739341]
Approved by ikegami
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2015-10-09 21:48 GMT
Find Nodes?
    Voting Booth?

    Does Humor Belong in Programming?

    Results (252 votes), past polls