Xref: utzoo comp.lang.c:39061 comp.os.msdos.programmer:5033 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!think.com!spool.mu.edu!uunet!zephyr.ens.tek.com!gvgpsa!gold.gvg.tek.com!shaunc From: shaunc@gold.gvg.tek.com (Shaun Case) Newsgroups: comp.lang.c,comp.os.msdos.programmer Subject: Re: DOS Environment Variables Summary: make and call a batchfile Message-ID: <2280@gold.gvg.tek.com> Date: 7 May 91 03:34:42 GMT Article-I.D.: gold.2280 References: <91125.051531AURPS@ASUACAD.BITNET> Followup-To: comp.os.msdos.programmer Organization: Grass Valley Group, Grass Valley, CA Lines: 103 In article <91125.051531AURPS@ASUACAD.BITNET> AURPS@ASUACAD.BITNET writes: >Does anyone know how to permanently change a DOS environment variable from >within an executing program? PUTENV() changes/creates a variable but it >is only in effect while the program is running. As soon as I go back to >DOS, the environment is restored to its original variables. I'm using >Turbo C. I've tried a few things, and the following is what has worked for me. It also happens to be portable betweem different MSDOS C compilers and versions of MSDOS. Additionally, this is really an MSDOS-specific question, so I have directed followups to comp.os.msdos.programmer. To set a master environment variable, try using the following batchfile with your program: File GO.BAT: ------------- cut here -------------- echo off REM use '@echo off' if you have dos 3.3 or higher set REM your program name here: test command /c setvar REM use 'call setvar' if you have dos 3.3 or higher set ------------- cut here -------------- here is test.c: ------------- cut here -------------- /********************* set_env_var.c Shaun Case, 1991 Public Domain *********************/ #include int main() { FILE *batfile; char varname[134]; char value[134]; printf("\nEnter variable name: "); gets(varname); printf("Enter value: "); gets(value); puts(""); if ((batfile = fopen("SETVAR.BAT", "w")) == NULL) { puts("Unable to open SETVAR.BAT, Omot. Bailing.\n\n"); return 1; } fprintf(batfile, "SET %s=%s\n", varname, value); fclose(batfile); return 0; } ------------- cut here -------------- I got the following output: COMSPEC=C:\4DOS.COM CMDLINE=go PATH=f:\tmp;c:\sys\util;c:\sys\bat;d:\borlandc\bin;d:\tc;C:\DOS;C:\WIN386;C:\;C:\TCP;c:\vga;c:\f-prot TEMP=f:\tmp TMP=f:\tmp FTPINIT=c:\tcp\init.tbl FTP_ATTR=0x71,0x74,0x21 FTP_CONFIG=c:\tcp\ftp.cfg USER=@Man PROMPT=$p$g Enter variable name: hodag Enter value: badger_nemesis COMSPEC=C:\4DOS.COM CMDLINE=setvar PATH=f:\tmp;c:\sys\util;c:\sys\bat;d:\borlandc\bin;d:\tc;C:\DOS;C:\WIN386;C:\;C:\TCP;c:\vga;c:\f-prot TEMP=f:\tmp TMP=f:\tmp FTPINIT=c:\tcp\init.tbl FTP_ATTR=0x71,0x74,0x21 FTP_CONFIG=c:\tcp\ftp.cfg USER=@Man PROMPT=$p$g HODAG=badger_nemesis -- shaunc@gold.gvg.tek.com atman%ecst.csuchico.edu@RELAY.CS.NET Postmaster of 1:119/666 1@9651 (WWIVnet) It's enough to destroy a young moose's faith! -- Bullwinkle