ENVIRON$ - QB64 Wiki

ENVIRON$

From QB64 Wiki

Revision as of 19:26, 1 July 2012 by Clippy (Talk | contribs)
(diff) ← Older revision | Current revision (diff) | Newer revision → (diff)
Jump to: navigation, search

The ENVIRON$ function returns a STRING environmental value from the PC's environmental settings list.


Syntax:

setting$ = ENVIRON$({list_number%|systemID$})


  • The function can use an INTEGER list_number order value or STRING systemID$ parameter.
  • List_number refers to the number order of the environmental list. Returns are not in any particular numerical order.
  • SystemID is the specific STRING parameter requested. Returns that environmental STRING setting only:
  • "BLASTER" returns current Sound Blaster settings if installed.
  • "COMPUTERNAME" returns OEM serial number of the PC.
  • "HOMEPATH" returns current user's Administrator or "OWNER" folder path.
  • "OS" returns the Windows Operating System version.
  • "PATH" returns full Windows path settings
  • "PROGRAMFILES" returns path to Program files folder normally "C:\PROGRAM FILES"
  • "PROMPT" returns current OS prompt setting. Windows 95 and 98 had version in brackets "[WIN 98]$P$G"
  • "SYSTEMROOT" returns the absolute path to the Windows folder including the drive letter. Normally "C:\WINDOWS"
  • "TEMP" returns path to TEMP folder. Normally "C:\TEMP"
  • "USERNAME" returns the current Administrator name or "OWNER".
Note: There are other possible system settings that are not listed! Run Example 1 to see the list on your computer.
  • The OS in Win 9X or ME can be found in the "PROMPT" parameter ID. Returns are limited in Win 9X and ME.
  • Note: QB64 may not return the same environment list as Qbasic or SET does in DOS.


Example 1: Viewing the list of environmental parameter settings using a counter loop like SET does in DOS.

DO i = i + 1 setting$ = ENVIRON$(i) ' get a setting from the list PRINT setting$ IF i MOD 20 = 0 THEN PRINT "Press a key": SLEEP: CLS LOOP UNTIL setting$ = ""


Example 2: Creating a shortcut on a user's desktop for QB64.EXE using the program's icon. Must be run in program's folder to work!

'=== Enter the EXE file and ICON or BMP image for the shortcut. Program$ = "QB64.EXE" '<<<<<<<<<< Enter the exact program name for shortcut ICON$ = "QB64ICON.BMP" '<<<<<<<<<< Enter icon or bitmap to use from program's folder DeskTopShortcut Program$, ICON$ END '====== END DEMO CODE ====== SUB DeskTopShortcut (Program$, ICON$) f = FREEFILE SHELL _HIDE "CD > PRGMDIR.INF" 'get the current program path OPEN "PRGMDIR.INF" FOR INPUT AS #f LINE INPUT #f, PATH$ CLOSE #f KILL "PRGMDIR.INF" PATH$ = PATH$ + "\": FILE$ = PATH + Program$ PRINT PATH$ 'DEMO print A$ = ENVIRON$("HOMEDRIVE") '=== Get Current User setting from Environment. B$ = ENVIRON$("HOMEPATH") C$ = A$ + B$ 'shortcut to user's desktop if found IF C$ = "" THEN C$ = ENVIRON$("ALLUSERSPROFILE") 'try desktop for all users PRINT C$ 'DEMO print URLFILE$ = MID$(Program$, 1, INSTR(Program$, ".")) + "URL" 'change EXE to URL IF C$ > "" THEN SHORTCUT$ = C$ + "\Desktop\" + URLFILE$ 'create filename for the desktop ELSE SHORTCUT$ = PATH$ + URLFILE$ 'if all else fails put in program folder END IF PRINT SHORTCUT 'DEMO print OPEN SHORTCUT$ FOR APPEND AS #f IF LOF(f) THEN CLOSE #f: EXIT SUB '=== if filesize is NOT Zero don't overwrite! Q$ = CHR$(34) '=== Write URL Shortcut file info. PRINT #f, "[InternetShortcut]" 'URL type PRINT #f, "URL=" + Q$ + "file://" + FILE$ + Q$ 'URL program file PRINT #f, "WorkingDirectory=" + Q$ + PATH$ + Q$ 'Working path PRINT #f, "IconIndex = " + Q$ + "0" + Q$ '0 is first index PRINT #f, "IconFile = " + Q$ + PATH$ + ICON$ + Q$ 'Icon path in working folder CLOSE #f END SUB

Adapted from code by Dav
Explanation: The SUB program finds the current program's path and user's desktop path. It then creates the shortcut on the desktop with a program icon. The custom icon should be in the program's folder. If an environmental path is not found, the shortcut is placed in the program's folder. The SUB can be added to any program.
NOTE: A temorary file named PRGMDIR.INF is created and deleted.


See also:



Navigation:
Go to Keyword Reference - Alphabetical
Go to Keyword Reference - By usage
Go to Main WIKI Page