Felix Waltl
Felix Waltl

Reputation: 129

Batch - Replace a String in a Loop

i´ve Problems replacing a string in a loop

For example: I have a File called someBlabla.txt which has multiple lines with random sentences. Now i want to read the file line by line in a for loop and replace a specific char of a word by another. I want to use the shown string replace method, means %myVar:to_replace=replace%

Has someone any hints?

Here is my Script:

REM REPLACE EXAMPLE
@echo off
SETLOCAL ENABLEDELAYEDEXPANSION

:MAIN
set file=someBlaBla.txt
set "charToReplace=a"
set "replacer=Z"
for /f "tokens=*" %%a in (%file%) do (
    set "replaced_line=%a:%charToReplace%=%replacer%%"
    echo replacedLine=!replaced_line!
)
exit /b 0

Here is my textfile:

This is a sentence with an a
this is something
there is a car
and so on and so on
some a a a

Upvotes: 1

Views: 698

Answers (0)

Related Questions