In batch, string replacement and substring extraction are done using the SET
command. Look it up using set /?
or help set
. However, when using SET
, there are some characters in the string that could be problematic, including !
, since this solution uses delayed expansion. There are methods that don't use delayed expansion, but without a real example of the strings you're working with, I'm not sure whether you need them or not.
String replacement looks like this.
%VARIABLE:original=replacement%
With delayed expansion, which allows the variable values to be determined for each iteration in the for
, instead of once at the beginning for the entire for
block, replace the %
with !
.
Look at HELP SET
for examples of substring expansion; there are a number of them depending on whether you index from the start or end of the string.
Here is one solution according to the sample data you provided and my comments above. For each line in test1.txt, it processes it and outputs it to output.txt.
@echo off & setlocal enabledelayedexpansion
for /f "delims=" %%a in (test1.txt) do (
set line=%%a
set line=!line:My Line is : =!
if "!line:~-1!"=="." set line=!line:~0,-1!
>> output.txt echo !line!
)