1

I need help with this statement at the moment when executed in a batch file it will launch all lines of a text file e.g.

file1.txt:

notepad
wordpad

so it will launch:

start notepad
start wordpad

Although I would like to be able to specify which line it will execute, instead of executing them all (which it is doing at the moment)

for /f "delims=|" %%i in (file1.txt) do @start "x" %%i

4
  • This isn't very clear, do you want an interactive method of choosing which line in the batch file gets executed?
    – phuzion
    May 13, 2011 at 18:48
  • @phuzion My apologies, yes it does have to be interactive May 13, 2011 at 18:55
  • Can you elaborate on why you want to do this? The use case is going to affect how to go about solving this problem. My main issue with this is that you seem to be asking for a batch file that will parse a file and provide the user with programs to run. A batch file is probably the worst tool for this task. Are you in a restricted environment that forces you to use this technology? May 13, 2011 at 19:06
  • Hi, yes I am in a restricted enviroment, as you can imagine its not the easiest thing to do using batches, quite frustrating. But I have gone and used another way of doing it now, so this question can be closed if needs be. May 13, 2011 at 20:19

4 Answers 4

1

I’m not sure what you want (or what the problem is), but it reads like you need to use the /w switch of the start command:

for /f "delims=|" %%i in (file1.txt) do @start /w "x" %%i
1
  • Hi, I mean, if I specify line number 2 of the text file (wordpad) it will launch wordpad. (Nothing else) May 13, 2011 at 18:50
1

Here is a batch file that will allow you to choose between the two options:

echo off
title Notepad or Wordpad
setlocal
set OK=N

:again
set /p choice=Please enter:    1 [Notepad],    2 [Wordpad]
if [%choice%]==[] goto again
if [%choice%]==[1] goto 1
if [%choice%]==[1] goto 2
set /p xxx=wrong entry, press any key to return.
endlocal
goto end


1: start notepad

2: start wordpad

:end
@echo on
cls
2
  • Hi, sorry, but the problem is that I am calling the lines from a text file, and they are variable May 13, 2011 at 18:53
  • Ahh, I see. Sorry, I misread your question :-)
    – Amartel
    May 13, 2011 at 19:37
1

Just copy the .txt file to new_batch_file.bat then use the following command to call it:

  • CALL new_batch_file.bat
0

How about this?

dir.menu

# Comments and empty lines are ignored

# Lines starting with a hyphen '-' indicate a group
- Editors
1 Notepad.exe
2 WordPad.exe

# You can use numbers or letters, and even words (without spaces)
- Graphics
P mspaint.exe

menu.bat

@setlocal
@echo off

if not exist ".\dir.menu" echo There is no "dir.menu" file in the current directory. & goto :end

set arg=%~1

if not defined arg goto :show_menu

for /f "tokens=1,* delims= " %%i in (dir.menu) do (
    if not "%%i"=="#" if /i "%arg%"=="%%i" (
        start "x" "%%j"
    )
)

:end
    endlocal
    exit /B

:show_menu
    echo   Menu:
    echo ===================
    for /f "tokens=1,* delims= " %%i in (dir.menu) do (
        if not "%%i"=="#" (
            if "%%i"=="-" (
                echo   %%j
            ) else (
                echo    %%i] %%j
            )
        )
    )
    goto :end

This is a simple, but nice menu system. You can use numbers or letters (or a word, without spaces) as the key. I've kept it very simple so you can see how to interact with it. (For instance, you could create a set of directories as a whole menu system, where the menu items are all inside 'dir.menu' files.)

Sample usage:

>menu
  Menu:             
=================== 
  Editors           
   1] Notepad.exe   
   2] WordPad.exe   
  Graphics          
   P] mspaint.exe   

>menu p
[mspaint is launched]
>

UPDATED: tested and corrected a couple things from my computer, instead of my phone.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .