Jump to content











Photo
- - - - -

Edit menu.lst from a DOS batch

menu.lst grub4dos batch edit text

  • Please log in to reply
20 replies to this topic

#1 Vikram

Vikram

    Newbie

  • Members
  • 28 posts
  •  
    France

Posted 22 August 2013 - 06:12 PM

Hi.

 

I don't know how to edit text with DOS.

 

I would like to create a DOS batch to edit the default title of the menu.lst.

 

I have set a computer with 3 partitions:

_ grub4dos v0.4.5c 03/03/2013 is installed in the MBR

_ the first partition hosts menu.lst and grldr at the root of A: and various utilities

_ the second and the third partitions host two independant Windows 7 64 bits installations.

 

By default, the computer starts on the third partition. When taking control of the computer from a remote place, I would like to be able to change the default title to boot either on the 2nd or the 3rd partition using a batch. The batch would run in a Windows 7 context.

 

In menu.lst, "default 0" would have to be changed for "default 1", and back.

For that matters, "default 0" (without the quotes) is at line 26, column 1, starting both from 1.

 

I already found a working but over complex solution to my problem. As I didn't know how to change the number in menu.lst, I found a solution involving playing with 3 lst files and a choice is offered to the user. I find this solution inelegant and I would like to be able to straight edit a stream.

 

If someone could help me in that task, thanks.

 

 



#2 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 22 August 2013 - 06:27 PM

If you have access to files on one of the partitions then you could use this at the top of the menu.lst

if exist (hd0,0)/default0 default 0
if exist (hd0,0)/default1 default 1
if exist (hd0,0)/default2 default 2

The all you need to do is rename the file - you could use a Dos or Windows batch file

 

Set1.bat

if exist C:\default0 ren c:\default0 default1

if exist C:\default2 ren c:\default2 default1

if not exist c:\default1 echo fred > c:\default1

 

Set0.bat

if exist C:\default1 ren c:\default0 default0

if exist C:\default2 ren c:\default2 default0

if not exist c:\default0 echo fred > c:\default0

 

Set2.bat

if exist C:\default1 ren c:\default1 default2

if exist C:\default0 ren c:\default0 default2

if not exist c:\default2 echo fred > c:\default2

 

 

You will need to change the drive letters as they will be different when booting from different partitions.



#3 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 22 August 2013 - 06:36 PM

There is no "built-in" editing in batch. :(
Are you wanting "real DOS" or Command Prompt in a NT system? (command.com or cmd.exe)? :dubbio:

Usually you need to re-write (as opposed to edit) the whole file if you want to remain "strictly" in batch.

If a third party tool is allowed, gsar would do:
http://home.online.no/~tjaberg/

:cheers:
Wonko

#4 Vikram

Vikram

    Newbie

  • Members
  • 28 posts
  •  
    France

Posted 22 August 2013 - 06:56 PM

Thanks Steve, but the solution you're providing is basically the same as the one I found. It implies renaming different files.

What I would like to do is editing menu.lst straight from a batch (or using a third party executable).

 

Yes Wonko, DOS seems to lack a sed/grep tools to edit streams.

I will have a try with gsar, thanks.



#5 Vikram

Vikram

    Newbie

  • Members
  • 28 posts
  •  
    France

Posted 22 August 2013 - 07:16 PM

On reflection, yes, I find Steve's approach interesting because simpler than that I found, thought it's not exactly what I'm trying to get.



#6 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 22 August 2013 - 07:38 PM

Live editing a menu.lst file via a batch file is risky. The way I proposed does not  edit the menu.lst directly and so should be safe. 



#7 Vikram

Vikram

    Newbie

  • Members
  • 28 posts
  •  
    France

Posted 22 August 2013 - 09:01 PM

Thanks Steve, I have successfully adapt your solution.

 

For the end user, the pc must boot the first TITLE.

Thus, to make things more robust, is there a way to tell menu.lst to create a file default0 AND delete a possibly existing default1 IF default0 and default1 are both existing if there is no default* ?


Edited by Vikram, 22 August 2013 - 09:08 PM.


#8 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 22 August 2013 - 09:05 PM

You could re-write the batch files:

 

if exist c:\default0 del default0 > nul

if exist c:\default1 del default1 > nul

if exist c:\default2 del default2 > nul

echo fred > c:\default0
 
 
etc.


#9 Vikram

Vikram

    Newbie

  • Members
  • 28 posts
  •  
    France

Posted 22 August 2013 - 09:10 PM

I have edited my previous post.



#10 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 22 August 2013 - 09:12 PM

default 0  will be the default anyway if none of the defaultx files exist.

 

But you can always put a default 0 line as the first line in the menu.lst.



#11 Vikram

Vikram

    Newbie

  • Members
  • 28 posts
  •  
    France

Posted 22 August 2013 - 09:16 PM

Hum, dumb question :loleverybody: .



#12 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 23 August 2013 - 08:21 AM

I will RE-ask the SAME question:
Is it DOS or NT command prompt (command.com or cmd.exe)?

 

EDIT: My bad :blush:, I see that you have specified a running Windows 7.

And add a new one:
Can you post an example menu.lst?

 

Live editing a menu.lst file via a batch file is risky.

Sure, so it is crossing the road. :ph34r:
Heck, a good 1/3 to 1/2 of what we do on reboot.pro is because we like to live dangerously! ;)


:cheers:
Wonko



#13 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 23 August 2013 - 08:25 AM

Live editing a menu.lst file via a batch file is risky.

 

Sure, so it is crossing the road. :ph34r:
Heck, a good 1/3 to 1/2 of what we do on reboot.pro is because we like to live dangerously! ;)


:cheers:
Wonko

 

What I meant was that live editing a menu.lst remotely was risky in the sense that if anything went wrong, you could not fix it remotely but would need to travel to the physical computer to fix it.



#14 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 23 August 2013 - 09:39 AM

What I meant was that live editing a menu.lst remotely was risky in the sense that if anything went wrong, you could not fix it remotely but would need to travel to the physical computer to fix it.

Sure, that would add some "spice" to it! ;)

Seriously, once the method, whatever it will be, is properly tested, talking of writing a bunch of bytes, the only thing that may actually go wrong is a blackout/reboot in the exact fraction of a second that the file is accessed/written.

Using gsar (or hexalter) to write a single byte should be as fast as possible.

http://kuwanger.net/.../hexalter.shtml

 

:cheers:

Wonko



#15 steve6375

steve6375

    Platinum Member

  • Developer
  • 7566 posts
  • Location:UK
  • Interests:computers, programming (masm,vb6,C,vbs), photography,TV,films
  •  
    United Kingdom

Posted 23 August 2013 - 09:45 AM

I was thinking more of human error - e.g. editing the menu.lst to modify/improve it, but in doing so accidentally breaking the batch file that searches/replaces the default entry and then only finding out it is broken when you try to remotely switch the default menu entry over. 

Guess it depends or risk/reward ratio!



#16 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 23 August 2013 - 10:42 AM

I was thinking more of human error - e.g. editing the menu.lst to modify/improve it, but in doing so accidentally breaking the batch file that searches/replaces the default entry and then only finding out it is broken when you try to remotely switch the default menu entry over. 

Guess it depends or risk/reward ratio!

Yep, with a plain "hexalter" approach this could happen, but with gsar, if you replace "default 0" with "default 1" and viceversa it won't likely happen.

Another approach could be to use a "tag file".

I mean, let's say that from the windows 7 one issues:



ECHO tagging>C:\a_non_common_file_name.tag

and (to remove it):



IF EXIST C:\a_non_common_file_name.tag DEL C:\a_non_common_file_name.tag

And in the menu.lst there is something *like*:



set mydef=default 0
ls (hd0,0)/a_non_common_file_name.tag && set mydef=default 1

:cheers:

Wonko



#17 Vikram

Vikram

    Newbie

  • Members
  • 28 posts
  •  
    France

Posted 23 August 2013 - 02:01 PM

This is my CMD file :

:: Les fichiers partition3 et partition2 sont là pour indiquer à menu.lst sur quelle partition le PC démarre. Si partition3 est présent, cela signifie que le PC démarre sur la partition 3. Si partition2 est présent, le PC démarre sur la partition 2.

@ECHO OFF

:: Test de montage de la partition 1
IF NOT EXIST A:\NUL (
	(ECHO select disk 0&ECHO select partition 1&ECHO set id=07 override&ECHO assign letter=A&ECHO exit)|diskpart > NUL
) ELSE (
	GOTO TESTS
)
	
:TESTS
	:: si partition3 = 0 et partition2 = 0 : le pc démarre sur la partition 3
	:: si partition3 = 1 et partition2 = 1 : le pc démarre sur la partition 3
	:: si partition3 = 1 et partition2 = 0 : le pc démarre sur la partition 3
	:: si partition3 = 0 et partition2 = 1 : le pc démarre sur la partition 2
	:: donc si partition3 = 0 et partition2 = 1, le pc démarre sur la partition 2, dans tous les autres cas, le pc démarre sur la partition 3
	IF NOT EXIST "A:\partition3" (
		IF EXIST "A:\partition2" (
			GOTO DemarrageActuelSurPartition2
		)
	) ELSE (
		GOTO DemarrageActuelSurPartition3
	)

:: si partition2 existe
:DemarrageActuelSurPartition2
	ECHO ==============================================
	ECHO LE PC DMARRE ACTUELLEMENT SUR LA PARTITION 2.
	ECHO ==============================================
	ECHO CHOIX POSSIBLES:
	ECHO A. Ne faire aucun changement et terminer le programme: taper A.
	ECHO B. D‚marrer le PC sur la partition 3 … partir de maintenant: taper B.
	ECHO.
	CHOICE /C AB /M "Veuillez choisir une option: "
	:: Les ERRORLEVEL doivent être placés dans le sens décroissant.
	:: Ici, ERRORLEVEL 2 correspond à la pression de B, et ERRORLEVEL 1 à la pression de A.
	IF ERRORLEVEL 2 GOTO CHANGEMENTPourDemarrageSurPartition3
	IF ERRORLEVEL 1 GOTO PasDeModification
:CHANGEMENTPourDemarrageSurPartition3
	:: si B
	REN "A:\partition2" partition3 > NUL
	IF EXIST "A:\partition3" IF NOT EXIST "A:\partition2" (
		ECHO ===========================================
		ECHO LE PC DMARRE DSORMAIS SUR LA PARTITION 3.
		ECHO ===========================================
		ECHO Fin du script.
		PING -n 5 127.0.0.1 > NUL
		GOTO Fin
	) ELSE (
		GOTO ErreurFichier
	)

:: dans tous les autres cas
:DemarrageActuelSurPartition3
	DEL "A:\partition2" > NUL
	COPY /Y NUL "A:\partition3" > NUL
	IF EXIST "A:\partition3" IF NOT EXIST "A:\partition2" (
		ECHO ==============================================
		ECHO LE PC DMARRE ACTUELLEMENT SUR LA PARTITION 3.
		ECHO ==============================================
		ECHO CHOIX POSSIBLES :
		ECHO A. Ne faire aucun changement et terminer le programme: taper A.
		ECHO B. D‚marrer le PC sur la partition 2 … partir de maintenant: taper B.
		ECHO.
		CHOICE /C AB /M "Veuillez choisir une option: "
		ECHO.
		IF ERRORLEVEL 2 GOTO CHANGEMENTPourDemarrageSurPartition2
		IF ERRORLEVEL 1 GOTO PasDeModification
	) ELSE (
		GOTO ErreurFichier
	)
:CHANGEMENTPourDemarrageSurPartition2
	:: si B
	REN "A:\partition3" partition2 > NUL
	IF EXIST "A:\partition2" IF NOT EXIST "A:\partition3" (
		ECHO ===========================================
		ECHO LE PC DMARRE DSORMAIS SUR LA PARTITION 2.
		ECHO ===========================================
		ECHO Fin du script.
		PING -n 5 127.0.0.1 > NUL
		GOTO Fin
	) ELSE (
		GOTO ErreurFichier
	)
	
:ErreurFichier
	ECHO "!!!!Erreur: Impossible de modifier les fichiers!!!!"
	ECHO Fin du script.
	PING -n 5 127.0.0.1 > NUL
	GOTO Fin
	
:PasDeModification
	:: si A, aucune modification effectuée
	ECHO Ancune modification effectu‚e.
	ECHO Fin du script.
	PING -n 5 127.0.0.1 > NUL
	GOTO Fin

:Fin
	:: Démonter la partition 1
	(ECHO select disk 0&ECHO select partition 1&ECHO set id=DE override&ECHO exit)|diskpart > NUL
	EXIT

But it's not totally working. I have issues to set the following condition:

(IF NOT EXIST file1 AND IF EXIST file2), THEN do, ELSE do

Indeed, I would like to check the presence of the files "partition3" and "partition2". Thought extensive search through the net, I didn't find a satisfying answer.

 

This the menu.lst:

debug 0
graphicsmode -1 800 600 -1 > nul
font (hd0,0)/unifont.hex.gz > nul
color normal=15 highlight=0xE0 helptext=0x0E heading=0x08 standard=15 border=0x0F
(hd0,0)/menusetting.gz 0 0 1 16 5 10 75 13
#Nombre total de colonnes en 800px de large: 101
#/menusetting %wdspace% %lnspace% %bdwidth% %tophelp% %noitems% %topstart% %menuw% %rstart%
#/menusetting     1         2         3         4         5         6         7        8
#De %wdspace% à %rstart%:
#/menusettings  1  %espacement des mots%
#               2  %espacement des entrées%
#               3  %largeur de la bordure%
#               4  %l'aide commence à partir de ce nombre de lignes à partir du haut%
#               5  %nombre d'entrées du menu%
#               6  %le menu commence à partir de ce nombre de lignes à partir du haut%
#               7  %largeur du menu%
#               8  %le menu commence à partir de ce nombre de colonnes à partir de la droite%

#/menusettings.gz u = retour des valeurs par défaut de grub4dos

write (md)0x220+1 !BAT\necho -n -P:0000 $[0117]                                MENU DE DÉMARRAGE DE L'ORDINATEUR                                   \necho -n -P:0612 $[0107]Veuillez faire un choix parmi les options suivantes:\necho -n -P:3412 $[0007]Ordinateur installé par ############### le 01/08/2013 (#########@gmail.com).\0 && initscript (md)0x220+1

#parttype (hd0,0) 0xDE

timeout 10
if exist (hd0,0)/partition3 default 0
if exist (hd0,0)/partition2 default 1

#numérotation auto des entrées
#write 0x8274 0x2001

password qd;in
#équivalent à admin en QWERTY

title Démarrer Windows\n\n Démarrage automatique dans 10 secondes.\n Appuyez sur une touche pour arrêter le décompte.
#fallback 1 permet de passer immédiatement à l'entrée 1 si celle-ci (entrée 0) échoue.
fallback 1
#parttype (hd0,0) 0xDE
#parttype (hd0,1) 0xDE
root (hd0,2)
chainloader (hd0,2)/bootmgr

title Démarrer le système de secours\n
#parttype (hd0,0) 0xDE
#parttype (hd0,1) 0x07
root (hd0,1)
chainloader (hd0,1)/bootmgr

title Éteindre l'ordinateur\n\n Pressez Entrée pour éteindre l'ordinateur.
#parttype (hd0,0) 0xDE
halt

title Redémarrer l'ordinateur\n\n Pressez Entrée pour redémarrer l'ordinateur.
#parttype (hd0,0) 0xDE
reboot

title Outils avancés\n\n Uniquement si vous savez ce que vous faites!
configfile (hd0,0)/OutilsAvances.lst

If someone could explain me how to do a proper (IF not exist file1 AND IF exist file2, THEN do, ELSE do), I would be thankful.


Edited by Vikram, 23 August 2013 - 02:41 PM.


#18 Vikram

Vikram

    Newbie

  • Members
  • 28 posts
  •  
    France

Posted 23 August 2013 - 02:30 PM

THIS CMD IS WORKING (I got rid of the double check I don't master as I explained in my last post):

:: Les fichiers partition3 et partition2 sont là pour indiquer à menu.lst sur quelle partition le PC démarre. Si partition3 est présent, cela signifie que le PC démarre sur la partition 3. Si partition2 est présent, le PC démarre sur la partition 2.

@ECHO OFF

:: Test de montage de la partition 1
IF NOT EXIST A:\NUL (
	(ECHO select disk 0&ECHO select partition 1&ECHO set id=07 override&ECHO assign letter=A&ECHO exit)|diskpart > NUL
) ELSE (
	GOTO TESTS
)
	
:TESTS
	:: si partition3 = 0 et partition2 = 0 : le pc démarre sur la partition 3
	:: si partition3 = 1 et partition2 = 1 : le pc démarre sur la partition 3
	:: si partition3 = 1 et partition2 = 0 : le pc démarre sur la partition 3
	:: si partition3 = 0 et partition2 = 1 : le pc démarre sur la partition 2
	:: donc si partition3 = 0 et partition2 = 1, le pc démarre sur la partition 2, dans tous les autres cas, le pc démarre sur la partition 3
	IF EXIST "A:\partition2" (GOTO DemarrageActuelSurPartition2) ELSE GOTO DemarrageActuelSurPartition3

:DemarrageActuelSurPartition2
	IF EXIST "A:\partition3" DEL "A:\partition3"
	ECHO ==============================================
	ECHO LE PC DMARRE ACTUELLEMENT SUR LA PARTITION 2.
	ECHO ==============================================
	ECHO CHOIX POSSIBLES:
	ECHO A. Ne faire aucun changement et terminer le programme: taper A.
	ECHO B. D‚marrer le PC sur la partition 3 … partir de maintenant: taper B.
	ECHO.partition
	CHOICE /C AB /M "Veuillez choisir une option: "
	ECHO.
	:: Les ERRORLEVEL doivent être placés dans le sens décroissant.
	:: Ici, ERRORLEVEL 2 correspond à la pression de B, et ERRORLEVEL 1 à la pression de A.
	IF ERRORLEVEL 2 GOTO CHANGEMENTPourDemarrageSurPartition3
	IF ERRORLEVEL 1 GOTO PasDeModification
	:CHANGEMENTPourDemarrageSurPartition3
		:: si B
		REN "A:\partition2" partition3 > NUL
		ECHO ===========================================
		ECHO LE PC DMARRE DSORMAIS SUR LA PARTITION 3.
		ECHO ===========================================
		ECHO Fin du script.
		PING -n 5 127.0.0.1 > NUL
		GOTO Fin

:DemarrageActuelSurPartition3
	COPY /Y NUL "A:\partition3" > NUL
	ECHO ==============================================
	ECHO LE PC DMARRE ACTUELLEMENT SUR LA PARTITION 3.
	ECHO ==============================================
	ECHO CHOIX POSSIBLES :
	ECHO A. Ne faire aucun changement et terminer le programme: taper A.
	ECHO B. D‚marrer le PC sur la partition 2 … partir de maintenant: taper B.
	ECHO.
	CHOICE /C AB /M "Veuillez choisir une option: "
	ECHO.
	IF ERRORLEVEL 2 GOTO CHANGEMENTPourDemarrageSurPartition2
	IF ERRORLEVEL 1 GOTO PasDeModification
	:CHANGEMENTPourDemarrageSurPartition2
		:: si B
		REN "A:\partition3" partition2 > NUL
		ECHO ===========================================
		ECHO LE PC DMARRE DSORMAIS SUR LA PARTITION 2.
		ECHO ===========================================
		ECHO Fin du script.
		PING -n 5 127.0.0.1 > NUL
		GOTO Fin
	
	
:PasDeModification
	:: si A, aucune modification effectuée
	ECHO Ancune modification effectu‚e.
	ECHO Fin du script.
	PING -n 5 127.0.0.1 > NUL
	GOTO Fin

:Fin
	:: Démonter la partition 1
	(ECHO select disk 0&ECHO select partition 1&ECHO set id=DE override&ECHO exit)|diskpart > NUL
	EXIT

Edited by Vikram, 23 August 2013 - 02:30 PM.


#19 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 23 August 2013 - 03:22 PM

I am evidently missing something :w00t:: A: is the drive letter normally reserved to first floppy disk :dubbio:

 

More generally:

 

:: si partition3 = 0 et partition2 = 0 : le pc démarre sur la partition 3
:: si partition3 = 1 et partition2 = 1 : le pc démarre sur la partition 3
:: si partition3 = 1 et partition2 = 0 : le pc démarre sur la partition 3
:: si partition3 = 0 et partition2 = 1 : le pc démarre sur la partition 2

 

One of the possible ways:

@ECHO OFF
:common_code

IF EXIST "partition2" IF NOT EXIST "partition3" GOTO :part2
:part3
ECHO whatever is needed to deploy on part3
GOTO :EOF
:part2
ECHO whatever is needed to deploy on part2
GOTO :EOF

:cheers:

Wonko



#20 Vikram

Vikram

    Newbie

  • Members
  • 28 posts
  •  
    France

Posted 23 August 2013 - 05:42 PM

I am evidently missing something :w00t:: A: is the drive letter normally reserved to first floppy disk :dubbio:

 

If you say so... I won't contradict you. Nervertheless, it has been many years since a saw a floppy drive.So why worry?

 

 

Reading your example, I wonder if all my "if exist" constructions weren't doomed by the fact I always used the full path of the files... like this "IF EXIST "A:\File".



#21 Wonko the Sane

Wonko the Sane

    The Finder

  • Advanced user
  • 16066 posts
  • Location:The Outside of the Asylum (gate is closed)
  •  
    Italy

Posted 23 August 2013 - 06:17 PM

I mean that you won't ever find *anything* in a "A:" drive on any machine built in the last - say - 8 years, unless it is a custom made PC and has a floppy or you added to it an external (USB) floppy drive OR if you are booting from an El-Torito  floppy emulation CD/DVD, that is what was perplexing me.

 

If you prefer, only VERY rarely it happens nowadays that an A: drive exists on a PC, so it is IMprobable that your statement "THIS CMD IS WORKING" is actually accurate unless it is a very unusual PC.

 

:cheers:

Wonko 







Also tagged with one or more of these keywords: menu.lst, grub4dos, batch edit text

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users