Batch file help (for statement)
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
Any help would be greatly appreciated
If you know the line number you would like to run, you can use something like this:
for /f "tokens=2* delims=]" %%I in ( ' find /V /N "" file1.txt ^| findstr /B /L "[1]" ' ) do @start "x" %%I
find /V /N "" file1.txt -- Displays all lines from file1.txt that are not "" (so basically any line that is not blank/empty/null), and appends numbers in the form of [#] to the line. This command would output:
[1] notepad
[2] wordpad
findstr /B /L "[1]" -- We now take advantage of having line numbers to extract the correct command. "/B" looks for the string at the beginning of the line. "/L" is a literal search. In this example, we match line #1 which would return "notepad" as %%I
精彩评论