0

How to extract a set of commands from a bat file respecting certain pattern and run them in bach?

example : file.txt

export TOOLCHAIN="multi" # Version of toolchain (optional) # Location of toolchain (optional for WINDOWS) export TOOLPATH="D:/Tools/compiler/GHS/GHS_COMPILER/PPC" 

in this example, I'm looking for a command which could

  1. find all the lines starting with export
  2. extract the command
  3. run the command

output:

set TOOLCHAIN="multi" set TOOLPATH="D:/Tools/compiler/GHS/GHS_COMPILER/PPC" 
1
  • 1
    Simple: for /F "tokens=1*" %%a in ('findstr "^export" file.txt') do set %%b BTW your description is confusing; you want not to "run the command", but to "set a variable with the command"... Commented Nov 20, 2018 at 14:15

1 Answer 1

3

Maybe this?:

@echo off setlocal enabledelayedexpansion for /f "delims=" %%i in ('type file.txt ^|findstr /B "export"') do ( set "output=%%i" set myCMD=!output:export=set! echo !myCMD! ) 

Simply loop through type command of file.txt then findstr the word export in the beginning of the line, and replace with set.

This will simply echo the full command, you can remove echo when happy with the results to actually run set command.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.