Creating environment variable

Posted on 2000-04-26
Last Modified: 2010-05-02

I need to create an environment variable during installation of my VB App. I think I have found a way to do this with WinNT. There is an environment variable called %SystemDrive% which I can use to locate or create autoexec.bat. I can add the line:

SET env_var="whatever"

Once the machine has rebooted, it should work.

My problem is, this may be installed on WinNT or Win9x. There is no %SystemDrive% variable on Win9x machines.. How can I create an environment variable on these platforms? Do anyone know an easier way to create an environment variable?
Question by:jmnolan
  • 4
  • 3
  • 2
  • +2

Expert Comment

ID: 2752367
If all you have to do is locate / create autoexec.bat, you should do a file check. Think autoexec.bat is always in the root, so you may check (even from a DOS bat) if exists in the root of any of the existing drive letters.

If it's not present anywhere, you can then look for the win*.exe (tha main windows executable) in the same way, so you will know in which letter drive the system is. You can then create the text file autoexec.bat in the root of this letter.

Greetings from Spain!

Author Comment

ID: 2752498
I was hoping there would be a more reliable way than checking all of the drives...

Expert Comment

ID: 2752557
I work with Win NT from the beginning, so I don't know about Win9x. But I suggest you to post a "linking question" to here in the Operating systems area to get a wider experts check to the question.
Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.


Expert Comment

ID: 2752942

Are you trying to do this in VB, or in a Windows Batch file?  If you're doing it in VB, it's easy.  There are many Windows API's that you can use to get system information and write out files.  If you're looking for Windows Batch file creation information... take this to the Windows board... not the Visual Basic board.

( )

Author Comment

ID: 2753031
I am altering the VB setup project to create environment variables. I was hoping there would be some sort of "CreateEnvironmentVariable( sName, sValue)" type of function. From what I've been hearing it looks like I have to do all the dirty work myself, I was looking for a pre-written function.

Accepted Solution

KDivad earned 100 total points
ID: 2753271
Since Windows and autoexec must exist on the same drive (don't they?), finding the windows folder should give you the autoexec drive.

WinDir = Environ$("windir")
If WinDir <> "" Then
    Open Left$(WinDir, 3) & "autoexec.bat" For Append As 1
    Print #1, "Set EnvVar=EnvVal"
    MsgBox "Windows doesn't exist on this system?!?!." & Chr$(10) & "How are you running this app?!?!", vbInformation, "Error: Windows missing?"
End If

Expert Comment

ID: 2753334
Why not savesetting the variable to the system registry? If you do this, then you can just write a simple prog to save/get a variable:

function PutEnv(VarName as string, VarValue as string) as boolean

    on error goto PUTENV_Error_Handler

    SaveSetting "Environment Variables", "Vars", VarName, VarValue
    Exit Function

    Exit Function
end function

function GetEnv(VarName as string, optional DefaultValue as string="") as string

    on error goto GETENV_Error_Handler

    GetEnv = getsetting("Environment Variables","Vars",VarName,DefaultValue)
    Exit function

    exit Function
end function


Expert Comment

ID: 2753880
I'm guessing that it probably interacts with some DOS apps that can't access the registry.

Author Comment

ID: 2755172
Actually, this is for CrypKey. In order to encrypt multiple files with the same license, I have to encrypt all of the files with an environment variable which will have the path to the license file. Unfortunately, you can't know where it is going to be installed until it is actually being installed. I am supposed to create an environment variable at that time with the path to the license file.

If %windir% exists on all windows platforms and autoexec.bat can be found in the root of that directory then I can try it that way. I am already using the registry for NT:

HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Control\Session Manager\Environment

Expert Comment

ID: 2756955
%windir% exists on Win95, Win95B and Win98SE. I don't know about any others as I've never used any others, but I would assume that Win98 is the same. If I'm not mistaken, Windows MUST be on the boot-drive to work. Therefore, autoexec can be found/created in the root of that drive. You could check for the existence of %windir% and if it's not there, use the reg key. That should cover Win9x and WinNT.

Expert Comment

ID: 2760440
Glad I could help!
Thanks for the points!

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
change vba from autofit to 13.5 width? 4 29
Macro which automatically sends attachment to Outlook 14 76
Updates not working for MS Windows 7 12 181
Run code from text file in vb 1 71
Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
There are many ways to remove duplicate entries in an SQL or Access database. Most make you temporarily insert an ID field, make a temp table and copy data back and forth, and/or are slow. Here is an easy way in VB6 using ADO to remove duplicate row…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Show developers how to use a criteria form to limit the data that appears on an Access report. It is a common requirement that users can specify the criteria for a report at runtime. The easiest way to accomplish this is using a criteria form that a…

791 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question