I have developed a couple programs that aids in creating and maintaining AutoMenu.bat menu folders.
Code: Select all
@echo off
setlocal EnableDelayedExpansion
rem EditMenu.bat: Insert/Extract options into/from a menu folder
rem Antonio Perez Ayala
if "%~1" neq "" if "%~1" neq "/?" goto begin
echo Insert/Extract options into/from a menu folder
echo/
echo EditMenu.bat menuFolder [k [newOption]]
echo/
echo K is the character Key for the old/new option
echo NewOption is the name of a subfolder or .BAT/.TXT file
echo/
echo If both K and newOption are given, the option is inserted at K position.
echo/
echo If just K is given, such an existent option is extracted from the menu
echo and placed alone. If you want to remove it, delete such a folder or file.
echo/
echo In both cases, the rest of options are reordered in the menu.
echo/
echo If K is not given either, the program navigate the menu folder and check
echo that all subfolders and files have the format required for AutoMenu.bat
goto :EOF
:begin
cd %1 || ( echo Menu folder not exist: %1 & goto :EOF )
if "%~2" neq "" goto editMenu
cls
echo/
:checkMenu
setlocal EnableDelayedExpansion
echo %~1
for /F "delims=" %%f in ('dir /B') do for /F "tokens=1,2* delims=-" %%a in ("%%f") do (
set "key=%%a"
if "%%b" equ "" (
echo === Option and description missing ===
echo %%f
echo ======================================
) else if "%%c" equ "" (
echo === Description missing ===
echo %%f
echo ===========================
) else if "!key:~1!" neq "" (
echo === Selection key must be a single char ===
echo %%f
echo ===========================================
)
set "name=%%f"
if "!name:~-4,1!" neq "." (
cd "%%f"
call :checkMenu "%%f"
cd ..
)
)
goto :EOF
:editMenu
echo/
echo Edit menu %cd%
dir /B
echo/
set "options=123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
if "%~3" neq "" goto insertOption
:deleteOption
set "newKey="
for /F "tokens=1* delims=-" %%a in ('dir /B') do (
if not defined newKey (
if "%%a" equ "%~2" (
ren "%%a-%%b" "%%b"
echo Option %%a extracted as "%%b"
set "newKey=%%a!options:*%%a=!"
)
) else (
ren "%%a-%%b" "!newKey:~0,1!-%%b"
set "newKey=!newKey:~1!"
)
)
echo/
dir /B
goto :EOF
:insertOption
if not exist "%~3" echo New option not exists: %3 & goto :EOF
set "newKey="
for /F "tokens=1* delims=-" %%a in ('dir /B') do (
if not defined newKey (
if "%%a" equ "%~2" (
ren "%~3" "%%a-%~3"
echo Option "%~3" inserted at position %%a
set "newKey=!options:*%%a=!"
ren "%%a-%%b" "!newKey:~0,1!-%%b" 2> NUL
set "newKey=!newKey:~1!"
)
) else (
ren "%%a-%%b" "!newKey:~0,1!-%%b"
set "newKey=!newKey:~1!"
)
)
echo/
dir /B
goto :EOF
EditMenu.bat is an auxiliary program useful to insert a new option in the menu folder, or to delete an option from the menu folder. Use it this way:
Code: Select all
EditMenu.bat menuFolder [K [NewOption]]
K is the character Key for the new option and NewOption is the name of a subfolder or .BAT/.TXT file that will be inserted in the menu. The NewOption must be in the same menuFolder and must have the same name format of the other options, excepting for the Key part. For example, lets assume that in
"A Menu Example" folder there are these options:
Code: Select all
1-FIRST-This is the first option
2-SECOND-The second one
3-THIRD-The third option
4-FOURTH-And the fourth and last one
NEW-This is the new option
After execute this line:
Code: Select all
EditMenu.bat "A Menu Example" 3 "NEW-This is the new option"
... the contents of "A Menu Example" folder will be this one:
Code: Select all
1-FIRST-This is the first option
2-SECOND-The second one
3-NEW-This is the new option
4-THIRD-The third option
5-FOURTH-And the fourth and last one
If just K is given, such an existent option is extracted from the menu. If after previous example you execute this line:
... then the folder contents would be this:
Code: Select all
1-FIRST-This is the first option
2-NEW-This is the new option
3-THIRD-The third option
4-FOURTH-And the fourth and last one
SECOND-The second one
If you want to delete the original option 2, just delete the "SECOND-The second one" folder/file. If you want to move such an option to other place, insert it now in its new place.
As you can see, this program makes it very easy to create and maintain large menu folders. I wish I had it when I created the "Windows-DOS Command Help" large menu!
If K key is not given either, the program navigate the menu folder and check that all subfolders and files have the format required for AutoMenu.bat. Any option with a bad format will makes AutoMenu.bat fail.
===========================================================================================
Code: Select all
@echo off
setlocal EnableDelayedExpansion
rem MakeAlpha.bat: Collect all *.bat files from an AutoMenu.bat menu folder
rem and create an Alpha folder with letter subfolders
rem Antonio Perez Ayala
if "%~1" equ "" echo Usage: %~NX0 menuFolder & goto :EOF
if not exist %1 echo Menu folder not exist: %1 & goto :EOF
for %%d in (%1) do set "dest=%%~DPdAlpha"
if exist "%dest%" (
set /P "=Delete " < NUL
rd /S "%dest%" 2>NUL
)
cls
md "%dest%"
rem Step 1: Collect *.bat files in corresponding letter subfolders
set "level=-1"
call :nextLevel %1
rem Step 2: Adjust names of letter folders
echo/
echo -----------------------------
cd "%dest%"
for /F %%d in ('dir /B') do (
echo/
echo Letter: "%%d"
cd %%d
set "name="
set "option=123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
for /F "delims=" %%f in ('dir /B') do for /F "delims=-" %%a in ("%%f") do (
echo %%a
ren "%%f" "!option:~0,1!-%%f"
set "option=!option:~1!"
if "!name:~-3!" neq "etc" (
set "newName=!name! %%a"
if "!newName:~62!" equ "" (
set "name=!newName!"
) else (
set "name=!name! etc"
)
)
)
cd ..
ren %%d "%%d-%%d-!name!"
)
goto :EOF
:nextLevel folder
setlocal EnableDelayedExpansion
cd %1
echo/
echo Folder: %~1
set /A level+=1
if %level% leq 1 (
for /F "tokens=1,2 delims=-" %%a in ("%~N1") do set "parent=%%a-%%b"
) else (
set /P "parent=Enter "parent" reference for nested subfolder: "
)
for %%f in (*.bat) do for /F "tokens=1,2 delims=-" %%a in ("%%f") do (
echo %%a-%%b
set "letter=%%b" & set "letter=!letter:~0,1!"
if not exist "%dest%\!letter!" md "%dest%\!letter!"
REM Change "COPY" by "MKLINK" to create symbolic links instead:
copy "%%f" "%dest%\!letter!\%%b-%parent%.bat" > NUL
REM mklink "%dest%\!letter!\%%b-%parent%.bat" "%%~Ff" > NUL
)
for /D %%d in (*) do call :nextLevel "%%d"
exit /B
The other auxiliary program is
MakeAlpha.bat. This program navigate for all options of the menu folder given in the parameter, order they in alphabetical order, and copy/accumulate they in letter subfolders that are created under "Alpha" folder. Each new option include as description a backwards reference to the original option in the base menu. After that, the program process the new Alpha menu folder options and insert in their descriptions a list of the original options included in each letter. The result is an alphabetical index of the options in the original menu.
After the Alpha menu folder was created, you must insert it as a new option in the original menu. For example:
Code: Select all
rem Create the Alpha menu folder of "Windows-DOS Commands Help" menu
MakeAlpha.bat "Windows-DOS Commands Help"
rem Insert it as the last option in the original menu
move "Alpha" "Windows-DOS Commands Help\A-ALPHA-Commands in Alphabetical order"
The result is pretty good!
An interesting aspect of MakeAlpha.bat program is that the new options inserted in the Alpha menu could be
symbolic links to the original files, instead of physical copies of the files. To do that, change the specified line in the MakeAlpha.bat program in order to use MKLINK command instead of COPY, but remember that you must have admin privileges in order to use MKLINK. This could be very useful if the option files are large.
For example, suppose you organize your music .MP3 files collection in folders distributed in several sublevels, like AUTHOR>ALBUM>TRACK. If you want to create an Alpha list of all songs in your collection, the new menu must have a
copy of each music file, but if such a "copy" is a symbolic link, then the data space occupied by each .mp3 file is
not duplicated. You may use the same method to create "sub-collections" of songs in any other order, like by type of music, or individual play lists, etc. BTW I am currently working in a method to play music files from an AutoMenu menu.
Antonio