473,804 Members | 3,675 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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

5 New Member
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>
Jul 3 '07 #1
4 5115
numberwhun
3,509 Recognized Expert Moderator Specialist
Hello.
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 ?
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:

Expand|Select|Wrap|Line Numbers
  1.     @keys = keys %ENV;
  2.     @values = values %ENV;
  3.     while (@keys) {
  4.     print pop(@keys), '=', pop(@values), "\n";
  5.     }
  6.  
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
Jul 3 '07 #2
KevinADC
4,059 Recognized Expert Specialist
I think it will depend on which version of Windows you are using. But try Jeffs suggestions and see if that works for you.
Jul 3 '07 #3
roniz5
5 New Member
Thank you very much Jeff!

It works.

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

Roni
Jul 4 '07 #4
numberwhun
3,509 Recognized Expert Moderator Specialist
Excellent! Glad that I could help and that it worked for you.

Regards,

Jeff
Jul 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
2384
by: R. Bressers | last post by:
Hi, I've got the following problem with PHP5.0 : index.php: ---------- <?php system("./whoami.pl");
0
9707
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9585
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10338
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10323
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10082
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9161
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6856
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5658
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4301
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.