Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Setting Up User Variables from DOS cmd line 1

Status
Not open for further replies.

amacbean

Programmer
Nov 18, 2002
9
US
Can anyone tell me how to create a User Variable from the command line?

(Instead of GUI way using My Computer -> Properties -> Enviroment)

I am writing a batch script that needs to set one up for the current user... Using SET only sets and enviremnt variable and is lost after the cmd window is closed?
 
ENDLOCAL
Ends localization of environment changes in a batch file, restoring environment variables to their values before the matching setlocal command. There is an implicit endlocal command at the end of the batch file.

endlocal
If command extensions are enabled (the default in Window), the endlocal command restores the enabled/disabled state of command extensions to what it was before the matching setlocal command was executed.

@echo off
rem This program starts the fhredlog batch program on the network,
rem directs the output to a file, and displays the file
rem in Notepad.
setlocal
path=F:\scripts\fhredlog;%path%
call labmice>c:\fhredlog.out
endlocal
start notepad c:\fhredlog.out
=========================================================
What I use is
SETLOCAL

Allows you to create a environment variable for use within a batch file. This variable lasts until a matching endlocal command is encountered, or the until end of the batch file.

setlocal option

Parameter option
When command extensions are enabled, the setlocal batch command accepts an optional argument, which can be either enableextensions or disableextensions. This enables or disables the command extensions until the matching endlocal command, regardless of their setting prior to the setlocal command.

The setlocal command also sets the errorlevel value when it is passed an argument. The errorlevel value is set to zero (0) if one of the two valid arguments is given and set to one (1) otherwise
========================================================
% (variable)

A variable is a replaceable parameter. The parameters %0 and %1 to %9 can be placed anywhere within a batch file. When the batch file is run, %0 is replaced by the name of the batch file, and the argument variables %1 to %9 are replaced by the corresponding parameters entered on the command line.

For example, to copy the contents of one folder to another, you would add the following statement in your batch file:

xcopy %1\*.* %2

When you run the file, you would type the following:

mybatch.bat C:\afolder D:\bfolder.

The effect is the same as if you had written xcopy C:\afolder \*.* D:\bfolder in the batch file.

The % parameter expands the batch script argument variables (%0, %1, ..., %9) as follows:

%* in a batch script is a wildcard reference to all the arguments. For individual argument variables, the expansion options are explained in the following tables.

Variable Description
%~1 expands %1 and removes any surrounding quotes (")
%~f1 expands %1 to a fully qualified path name
%~d1 expands %1 to a drive letter
%~p1 expands %1 to a path
%~n1 expands %1 to a file name
%~x1 expands %1 to a file extension
%~s1 expanded path contains short names only
%~a1 expands %1 to file attributes
%~t1 expands %1 to date/time of file
%~z1 expands %1 to size of file
%~$PATH:1 searches the directories listed in the PATH environment variable and expands %1 to the fully qualified name of the first one found. If the environment variable name is not defined or the file is not found by the search, then this modifier expands to the empty string.

The modifiers can be combined to get compound results:

Variable Description
%~dp1 expands %1 to a drive letter and path
%~nx1 expands %1 to a file name and extension
%~dp$PATH:1 searches the directories listed in the PATH
environment variable for %1 and expands to the drive letter and path of the first one found
%~ftza1 expands %1 to a dir-like output line

In the above examples %1 and PATH can be replaced by other valid values. The %~ syntax must be terminated by a valid argument number. The %~ modifiers may not be used with %*.






 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top