Saving Vars

Discussion forum for all Windows batch related topics.

Moderator: DosItHelp

Post Reply
Message
Author
gaventemples31415
Posts: 2
Joined: 24 Mar 2014 14:27

Saving Vars

#1 Post by gaventemples31415 » 24 Mar 2014 14:32

How would I save a variable so that it can be accessed if the program has been closed?

foxidrive
Expert
Posts: 6031
Joined: 10 Feb 2012 02:20

Re: Saving Vars

#2 Post by foxidrive » 24 Mar 2014 15:58

You can write the variable to a file.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Saving Vars

#3 Post by Compo » 24 Mar 2014 16:15

Or use the registry:

Code: Select all

@Echo off&SetLocal
Set "NewVar=Compo"
Set "KEY=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Reg Add "%KEY%" /V NewVar /D "%NewVar%" /F 1>Nul

gaventemples31415
Posts: 2
Joined: 24 Mar 2014 14:27

Re: Saving Vars

#4 Post by gaventemples31415 » 25 Mar 2014 13:59

SO, I would just change Key to the variable's name?

ShadowThief
Expert
Posts: 1166
Joined: 06 Sep 2013 21:28
Location: Virginia, United States

Re: Saving Vars

#5 Post by ShadowThief » 25 Mar 2014 14:40

Key is where in the registry you're storing it. Change the value of NewVar to the variable you're storing.

Compo
Posts: 600
Joined: 21 Mar 2014 08:50

Re: Saving Vars

#6 Post by Compo » 25 Mar 2014 15:21

gaventemples31415 wrote:SO, I would just change Key to the variable's name?

No, if you want to have a variable identified as %CDROM% with a Value of E: then change the script to read:
@Echo off&SetLocal
Set "CDROM=E:"
Set "KEY=HKLM\SYSTEM\CurrentControlSet\Control\Session Manager\Environment"
Reg Add "%KEY%" /V CDROM /D "%CDROM%" /F 1>Nul

Dos_Probie
Posts: 233
Joined: 21 Nov 2010 08:07
Location: At My Computer

Re: Saving Vars

#7 Post by Dos_Probie » 26 Mar 2014 18:05

gaventemples31415 wrote:How would I save a variable so that it can be accessed if the program has been closed?

More information about your specific program and what exactly you are trying to do would be helpful for a better answer..DP

carlsomo
Posts: 91
Joined: 02 Oct 2012 17:21

Re: Saving Vars

#8 Post by carlsomo » 06 Apr 2014 01:39

You could try SETX

type SETX -I @ command prompt for examples
It may not work if script is not run in admin mode depending on win version

Aacini
Expert
Posts: 1913
Joined: 06 Dec 2011 22:15
Location: México City, México
Contact:

Re: Saving Vars

#9 Post by Aacini » 06 Apr 2014 10:05

Some time ago I wrote a Batch-JScript hybrid script called SetEnv.bat that allows to define persistent variables of severals types. Here it is:

Code: Select all

@if (@CodeSection == @Batch) @then


:: SetEnv.bat: Creation of persistent environment variables via a JScript subroutine
:: Antonio Perez Ayala

@echo off
setlocal EnableDelayedExpansion

if "%~1" neq "" if "%~1" neq "/?" goto begin
set start=
for /F "delims=:" %%a in ('findstr /N "^</*usage>" "%~F0"') do (
   if not defined start (set start=%%a) else set /A lines=%%a-start-1
)
set line=0
for /F "skip=%start% tokens=1* delims=:" %%a in ('findstr /N "^" "%~F0"') do (
   echo(%%b
   set /A line+=1
   if !line! equ %lines% goto :EOF
)

<usage>
Create persistent environment variables.

SETENV charVar=code strVar:=string ... [/T:envtype]

  charVar    Specifies the name of one-character environment-variable.
  code       Ascii (Unicode) code of the character assigned to charVar;
             any code is valid, excepting zero.

  strVar     Specifies the name of string environment-variable.
  string     String of characters assigned to strVar;
             if contain spaces or special characters, enclose it in quotes.

  /T         Specify the environment type for the variables.
  envtype     SYSTEM    Applies to all users of the computer and is saved
                        between logoffs and restarts in the registry.
              USER      Applies to the user currently logged on to the
                        computer and is saved between logoffs and restarts.
              VOLATILE  Applies to current logon session and is not saved
                        between logoffs and restarts (this is the default).
              PROCESS   Applies to current process and might be passed to
                        child processes.

Examples:

    [call] SetEnv BEL=7 BS=8 TAB=9 CR=13 LF=10
    [call] SetEnv LastLogoff:="%date% @ %time%" /T:USER
</usage>

:begin

rem Define the variables via JScript
Cscript /nologo /E:JScript "%~F0" %*
goto :EOF


@end


// JScript section: SetEnv subprogram

// Define environment variables given in command-line arguments with this format:
//     charVar=code strVar:="string" ... [/T:SYSTEM|USER|VOLATILE|PROCESS]
// Antonio Perez Ayala

// Reference: http://technet.microsoft.com/en-us/library/ee156595.aspx

var envType = WScript.Arguments.Named.Item("T");
if ( envType == undefined ) envType = "VOLATILE";
var WshShell = WScript.CreateObject("WScript.Shell"),
    colEnvVars = WshShell.Environment(envType),
    args = WScript.Arguments.Unnamed, name_Value, equalSignPos;
for ( var i = 0; i < args.Length; i++ ) {
   name_Value = args.Item(i);
   equalSignPos = name_Value.indexOf("=");
   if ( name_Value.charAt(equalSignPos-1) == ":" )
      colEnvVars(name_Value.slice(0,equalSignPos-1)) = name_Value.slice(equalSignPos+1);
   else {
      colEnvVars(name_Value.slice(0,equalSignPos)) = String.fromCharCode(parseInt(name_Value.slice(equalSignPos+1)));
   }
}

Antonio

Post Reply