Lkaf Temravet
Lkaf Temravet

Reputation: 993

windows shell : extract command from files and run them

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"

Upvotes: 0

Views: 79

Answers (1)

user7818749
user7818749

Reputation:

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.

Upvotes: 3

Related Questions