home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / cenvi23.zip / ENVSORT.CMD < prev    next >
OS/2 REXX Batch file  |  1995-10-11  |  2KB  |  61 lines

  1. @echo OFF
  2. REM ****************************************************************
  3. REM *** EnvSort.cmd - Sort environment variables alphabetically. ***
  4. REM *** ver.1         This works by writing a temporary batch    ***
  5. REM ***               file to set all of the environment         ***
  6. REM ***               variables in alphabetic order, then        ***
  7. REM ***               removing the batch file.                   ***
  8. REM ****************************************************************
  9. ECHO SORTING ENVIRONMENT VARIABLES...
  10.  
  11. REM *** USE TEMP OR TMP VARIABLE TO FIND DIRECTORY ***
  12. REM *** TO STORE TEMPORARY FILE                    ***
  13. SET __CMD=.
  14. IF NOT "%TMP%"=="" SET __CMD=%TMP%
  15. IF NOT "%TEMP%"=="" SET __CMD=%TEMP%
  16. REM REMOVE BACKSLASH IF THERE IS ONE
  17. call CEnviSet "i=strlen(__CMD); if(__CMD[i-1]=='\\') __CMD[i-1]=0;"
  18. call CEnviSet "j=strlen(__CMD); for(i=0;i<j;i++ ) if( __CMD[i]=='/' ) __CMD[i] = '\\';"
  19.  
  20. SET __CMD=%__CMD%\_ENVSORT.CMD
  21.  
  22. SET | CEnvi2 %0.cmd
  23. GOTO CENVI_EXIT
  24.  
  25.    // Build list of all environment variables
  26.    Count = 0;
  27.    while ( ELine = gets() )
  28.       Lines[Count++] = ELine;
  29.  
  30.    // Sort variable lines
  31.    qsort(Lines,Count,"strcmpi");
  32.  
  33.    // open temporary batch output file
  34.    if ( !(fp = fopen(__CMD,"wt")) ) {
  35.       printf("Error opening \"%s\" for writing.\a",__CMD);
  36.       exit(EXIT_FAILURE);
  37.    }
  38.  
  39.    // for each variable, write out to set to nothing
  40.    for ( i = 0; i < Count; i++ ) {
  41.       ELine = Lines[i];
  42.       if ( Equal = strchr(ELine,'=') )
  43.          fprintf(fp,"SET %.*s\n",Equal-ELine+1,ELine);
  44.    }
  45.  
  46.    // write out all the variable lines to batch file
  47.    for ( i = 0; i < Count; i++ )
  48.       fprintf(fp,"SET %s\n",Lines[i]);
  49.  
  50.    // all done. close batch file
  51.    fclose(fp);
  52.  
  53. :CENVI_EXIT
  54.  
  55. REM *** PLAY BACK THE TEMPORARY BATCH FILE; ALL DONE
  56. CALL %__CMD%
  57. DEL %__CMD%
  58. SET __CMD=
  59.  
  60. REM *** ENVIRONMENT
  61.