perl "whoami" in WINDOWS (want to use different directory for each user)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • roniz5
    New Member
    • Feb 2007
    • 5

    perl "whoami" in WINDOWS (want to use different directory for each user)

    Hello.

    I write a perl script that 5 different people will use.
    Each one of the 5 will run the script on the same computer, but will login to this computer with a different user_name and so (I guess) with a different user environment variable.

    If we were working on Unix, I could simply use the WHOAMI unix command in the perl script. But we are working with WINDOWS.

    So, in short, how can the perl script know who is the current user, without asking him, in order to use a different directlry for each user ?

    Thanks
    Roni
    <EMAIL REMOVED BY MOD>
  • numberwhun
    Recognized Expert Moderator Specialist
    • May 2007
    • 3467

    #2
    Originally posted by roniz5
    Hello.
    Hello!

    Originally posted by roniz5
    I write a perl script that 5 different people will use.
    Each one of the 5 will run the script on the same computer, but will login to this computer with a different user_name and so (I guess) with a different user environment variable.

    If we were working on Unix, I could simply use the WHOAMI unix command in the perl script. But we are working with WINDOWS.

    So, in short, how can the perl script know who is the current user, without asking him, in order to use a different directlry for each user ?
    Ok, first.... perldoc is your friend. Reference it often and always. ( just a tip that could have helped here).

    That said, take a look at this page. That is the perldoc page for Perl's special variables. On that page, you will see that Perl has the hash %ENV already defined, containing all of the environment variables as keys and their values as.....well.... .the values.

    Now, the question would be, "How do I utilize that variable?". That question can be answered by visiting this perldoc page. That page is for the keys function, which basically grabs all of the keys in a hash for you.

    The code on that page(the very first example) is the code that you are looking for. Here it is:

    Code:
        @keys = keys %ENV;
        @values = values %ENV;
        while (@keys) {
    	print pop(@keys), '=', pop(@values), "\n";
        }
    Now, if you are using strict and warnings (as you should ALWAYS be doing), then you will want to prepend the array definitions with "my" or Perl will throw complaints at you.

    When you run this code on your Windows machine (perl <scriptName>) , it will print out ALL of the environment variables defined, and their corresponding values (using the values function shown above). One of the variables is "USERNAME".

    Now, all you have to do is write some code that will reference the USERNAME variable from the hash and grab its value so your script can use it.

    Hope this helps to put you on the path to completing your script.

    Regards,

    Jeff

    Comment

    • KevinADC
      Recognized Expert Specialist
      • Jan 2007
      • 4092

      #3
      I think it will depend on which version of Windows you are using. But try Jeffs suggestions and see if that works for you.

      Comment

      • roniz5
        New Member
        • Feb 2007
        • 5

        #4
        Thank you very much Jeff!

        It works.

        Thanks for the quick, accurate and full of humor reply :)

        Roni

        Comment

        • numberwhun
          Recognized Expert Moderator Specialist
          • May 2007
          • 3467

          #5
          Excellent! Glad that I could help and that it worked for you.

          Regards,

          Jeff

          Comment

          Working...