Alphabetizing Your Favorites Menu and Your Start Menu In Windows XP
This batch program is somewhat more complex than the ones we have looked at so far. It uses more advanced techniques to control program flow.
Alphabetize.bat alphabetizes your Favorites menu and the Programs branch of your Start menu. (You can have it act on your Favorites menu, your Start menu, or both.) The menus become unsorted as you add new items—which are appended to the bottom of the menu— or manually reorder them by dragging menu items. Windows XP does include a Sort By Name command on the shortcut menu that appears if you right-click one of these menus.
But Alphabetize.bat offers something more: If you decide you like the previous order better, Alphabetize.bat can restore the previous order—giving it an “undo” capability. Furthermore, Alphabetize.bat reorders the entire menu, including its submenus, whereas the Sort By Name command doesn’t sort the contents of submenus.
The order information for the Start and Favorites menus is kept in the registry. To alphabetize, this batch program exports the current settings to a file and then simply deletes the appropriate keys from the registry. Without an order specified in the registry, Windows alphabetizes the lists. As you add or move menu items, Windows re-creates the order information in the registry.
In the Alphabetize.bat listing that follows, we’ve included line numbers for reference. Note that you can’t use line numbers in an actual batch program.
1. @echo off
2. rem Alphabetizes Favorites and/or Start menu
3. setlocal
4. if not exist "%appdata%\inside out\" md "%appdata%\Inside Out\"
5.
6. if "%1" == "" goto usage
7. if "%1" == "/?" goto usage
8. if /i %1 == help goto usage
9. set type=%1
10. set action=%2
11. if "%2" == "" set action=sort
12.
13. set startmenu=false
14. set favorites=false
15. goto %type%
16. :STARTMENU
17. set startmenu=true
18. goto %action%
19. :FAVORITES
20. set favorites=true
21. goto %action%
22. :BOTH
23. set startmenu=true
24. set favorites=true
25. goto %action%
26.
27. :SORT
28. :SORTFAVORITES
29. if not %favorites% == true goto sortstartmenu
30. reg export "HKCU\Software\Microsoft\Windows\CurrentVersion\Ex plorer\ MenuOrder\Favorites" "%AppData%\Inside Out\Favorites.reg" > nul
31. reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Ex plorer\ MenuOrder\Favorites" /f > nul
32. if %errorlevel% equ 0 echo Favorites alphabetized.
33.
34. :SORTSTARTMENU
35. if not %startmenu% == true goto :eof
36. reg export "HKCU\Software\Microsoft\Windows\CurrentVersion\Ex plorer\ MenuOrder\Start Menu" "%AppData%\Inside Out\Start Menu.reg" > nul
37. reg delete "HKCU\Software\Microsoft\Windows\CurrentVersion\Ex plorer\
MenuOrder\Start Menu" /f > nul
38. if %errorlevel% equ 0 echo Start Menu alphabetized.
39. goto :eof
40.
41. :UNDO
42. :UNDOFAVORITES
43. if not %favorites% == true goto undostartmenu
44. if not exist "%appdata%\inside out\favorites.reg" goto noundofile
45. reg import "%appdata%\inside out\favorites.reg" > nul
46. echo Favorites order restored.
47.
48. :UNDOSTARTMENU
49. if not %startmenu% == true goto :eof
50. if not exist "%appdata%\inside out\start menu.reg" goto noundofile
51. reg import "%appdata%\inside out\start menu.reg" > nul
52. echo Start menu order restored.
53. goto :eof
54.
55. :NOUNDOFILE
56. echo No file exists to restore.
57. goto :eof
58.
59. :USAGE
60. echo.
61. echo Usage:
62. echo Alphabetize StartMenu^|Favorites^|Both [Undo]
63. echo.
64. echo Example:
65. echo Alphabetize Favorites
66. echo.
67. echo Add Undo to restore previous settings.
Lines 1 through 3 take care of some basic setup functions. They identify what this batch program does and turn echoing off. The Setlocal command specifies that environment variables created or changed by this batch program should be “local” to this batch program and should not affect other programs.
Line 4 creates a folder under the Application Data folder called Inside Out. Because the Md (make directory) command displays an error if the folder already exists, we use If Not Exist so that the Md command is executed only if the folder does not exist.
The second section of the batch program, lines 6 through 11, checks for command-line parameters. The %1 that appears in several of these lines represents the first argument on the command line, %2 represents the second, and so on up to %9. If you type the command alphabetize a, for example, the command interpreter sees this as the first line in this section of the batch program:
if "a" == "" goto usage
The test for equality would not be True, so the Goto statement would not be executed. But if you type alphabetize (without any arguments), the command interpreter would see
if "" == "" goto usage
and the Goto statement would be executed. Notice in Alphabetize.bat that %1 is surrounded by quotation marks. This isn’t a requirement. We could just as easily have written %1$==$. All we’re doing is making sure that the command interpreter always sees something on both sides of the equal signs. Without a character there, it sees the line as
if == goto usage
The result would be a syntax error, causing the batch program to terminate. The quotation marks are a readable way to be sure neither side is ever empty.
The first line of this section (line 6), therefore, is for the case when no command-line parameters are used, and it sends the command processor to the Usage label (line 59), where the program displays instructions for using Alphabetize.
Lines 7 and 8 do the same thing: They answer calls for help. Because the comparison in an If statement requires an exact match, in line 8 we used the /I switch, which causes the If statement to ignore case. This switch is available only if command extensions are enabled. The /I switch overcomes a major limitation of earlier versions of MS-DOS and Windows, in which you’d need to set up several If statements to test for capitalization variants of the word help. For example, you might test for help, HELP, and Help—and you still wouldn’t catch every possible form.
If the command line includes parameters that are not requests for help, lines 9 through 11 come into play. The Set commands assign the command-line parameters to environment variables that we can use later in the batch program. If the second command-line parameter is blank ("%2" == ""), the action is assumed to be Sort and is set to that value.
In lines 13 through 25, we set a group of environment variables that are used to tell the remainder of the batch program what to do.
Lines 13 and 14 set the environment variables StartMenu and Favorites to their default value of False. The Goto %Type% command (line 15) sends processing to the label that matches the Type variable set earlier. Because labels are not case sensitive, it doesn’t matter whether the user types both, Both, or some other variant. The rest of this section sets the StartMenu and Favorites variables to True if they are to be processed. They remain set to False if they are not to be processed. The Goto %Action% commands (lines 18, 21, and 25) send processing to the proper section of the batch program—to the Sort label (line 27) or the Undo label (line 41).
Now we are ready to start the actual work. Lines 27 through 39 sort the menus, and lines 41 through 57 restore the old menu arrangements.
The Sort section starts by checking that the value of the variable Favorites is True, in line 29. If it is not, the processing of Favorites is skipped. Next, the Reg Export command (line 30) creates a file that contains the current Favorites order. The environment variable AppData is used to locate the Inside Out folder that was created at the beginning of the batch program. After the backup file is made, the Reg Delete command (line 31) removes all the Favorites order information from the registry. Without this information in the registry, the menu reverts to its default alphabetical order. The /F switch does this without confirmation, and
> Nul eliminates the completion message.
Reg sets the error level value to 0 upon successful completion or to 1 if an error occurs. The If statement in line 32 echoes a message of success if the error level is 0. This form of the If statement works only with Windows 2000 and Windows XP;
if you want to create a program for use on computers running earlier versions of Windows, you could instead use the more traditional form:
if not errorlevel 1 echo Favorites alphabetized.
The SortStartMenu section (lines 34 through 39) performs the same process on the Start menu. Lines 35 and 39 demonstrate another feature introduced in Windows 2000: the Goto :EOF command. This special label, which must include the colon, causes the command processor to jump to the end of the batch program—in other words, to end execution. With MS-DOS and earlier versions of Windows, you must create a label at the end of the file and use it as the target of the Goto command to achieve the same result.
The Undo section of the batch program, lines 41 through 57, restores the original menu order. Again, the first check is to be sure that the Favorites are supposed to be processed (line 43). Then the If Not Exist command in line 44 checks to be sure that a backup file exists. If the file doesn’t exist, the NoUndoFile section (lines 55 through 57) displays a message to that effect.
The Reg Import command (line 45) does the actual work. It imports the contents of the backup file to the registry, restoring the previous order.
The UndoStartMenu section (lines 48 during 53) presents the similar process on the Start menu.
It’s a good idea to make batch programs self-documenting. The Usage section, lines 59 through 67, does just that. This section consists of a series of Echo commands that display the correct usage of this batch program. Note the use of the Echo command followed by a dot, which displays a blank line for improved legibility. (If you use Echo alone, it reports the state of the echoing function—on or off.)
The interesting thing about line 62 is the use of the escape symbol (^) to indicate that the pipe symbol (|) should be treated as a character and not interpreted as a pipe symbol. Without the escape symbol, the command interpreter would try to pipe the Echo command and the first few words to whatever follows the pipe symbol, which would cause a syntax error. You must use the escape symbol any time you want to echo a pipe symbol (|), a greater than sign (>), a less than sign (<), an ampersand (&), or a caret (^).
Although this batch program illustrates some advanced techniques of batch processing, it also demonstrates some limitations. If you misspell the first parameter, which should be Favorites, StartMenu, or Both, the batch program will fail. For example, if you type alphabetize favorits, the batch program displays this message:
The system cannot find the batch label specified - favorits
There is no way to trap for this error or to change the message to something more meaningful.
Likewise, if you happen to type one of the options for the first parameter as the second parameter, the batch program goes into an endless loop. For example, if you type alphabetize favorites both, the batch program appears to hang. It is actually very busy jumping back and forth between various labels, but the only way to stop it is to press Ctrl+Break.
These limitations are typical of complex batch programs. Batch programs provide a quick way to do simple tasks, but other tools (such as Windows Script Host) are better for more complex tasks.
A final word about debugging batch programs: You usually have to do some experimenting to get your batch programs to work exactly the way you want. You can write the batch program in one Command Prompt window and test it in another. To see exactly what the command interpreter sees, change the first line to
rem @echo off
This “comments out” the Echo Off command so that each line echoes to the screen with parameters and environment variables filled in before it is executed. In our sample file, you would probably also want to temporarily remove the redirection to Nul from each command so that you could see all the output.


LinkBack URL
About LinkBacks
Reply With Quote

LinkBacks Enabled by vBSEO
Bookmarks