XP batch files: Create variable for current user account and find current user

Home -> Windows -> Batch File Programming

13023 views

From the computer of: seamonkey420 (130 recipes)
Created: Dec 07, 2006     Updated: Dec 29, 2006


2 comments:
View all comments

Add a comment

Add to:
Add to stumbleuponAdd to del.icio.usDigg itAdd to FURL

this is a quick batch file recipe that is more of an appetizer than a full meal. this recipe is just a quick batch file that lets you find the user who is currently logged in when the batch file is ran by using a FOR statement and the %USERPROFILE% variable in XP.

this batch file will do the following:

find the current user logged in when the file is ran
setup a variable, user, whose value will be set to the user logged in
and then echo back the user login.

Quote:
@echo off
for /f "tokens=3 delims=\" %%i in ("%USERPROFILE%") do (set user=%%i) 2>&1
echo "logged in user:%user%"
pause


this can be very useful when used in conjunction with other batch file commands.

if you want this info be put into a text file just use this command

Quote:
@echo off
for /f "tokens=3 delims=\" %%i in ("%USERPROFILE%") do (set user=%%i) 2>&1
echo "%user%" > c:\%user%.txt
pause


so lets say i'm logged in as john, a file called john.txt would be created and inside of the file the user login 'john' will be residing.

this command/line can be changed and used for various things such as copying files to a user's my documents or desktop or anything where you need to use the user account.

--updated code; removed do if exist statement, used a straightforward do

Subscribe to the Tech-Recipes Newsletter

You can get tips like this delivered in your email every week!

Enter your Email

We will never, ever sell your email address or spam you.




2 Recipe comments: View comments

XP batch files: Create variable for current user account and by dvessels
Re: XP batch files: Create variable for current user account and by RLJ151



Related recipes:

  Batch Files : Get users default printer via registry (WinXP)
  Using variables in Windows batch files
  Supressing Prompts In Batch Files While Deleting All Files
  XP Batch Files: Copy images from camera to pc and organize by date
  Supress Responses From Commands In Batch Files
  Batch File Converting of Video Files: SageTV example
  Create Batch File to Start or End Window Services
  XCOPY command : Using the /EXCLUDE flag
  Windows XP/Vista: Use a Batch File for Quickly Restarting explorer.exe
  Date Calculation In Batch Files XP/2000

 

Sponsored links

 

Login

Nickname

Password

Don't have an account yet? You can create one. As a registered user you have some advantages like theme manager, comments configuration and post comments with your name.