I have text file (saved as *.pm but I assume it does not make any difference as it can be open as a text file). The content of the file looks something like:
Header
$VAR1 = {
'Keywords' => {
'X' => '80',
'Target_Path' => 'Example_PAth',
'Y' => 'File_Path',
'X' => '80',
'Y' => 'File_Path',
'X' => '80',
'Y' => 'File_Path',
sdefkjnhksdf koijsef ökiljsdf
sdefkjnhksdf koijsef ökiljsdf
sdefkjnhksdf koijsef ökiljsdf
I want to change Example_PAth to Example_PAth_New using a *.bat file.
For this I have the following code:
@echo off
setlocal disableDelayedExpansion
:Variables
set InputFile=OldFileName.pm
set OutputFile=NewFileName.pm
set "_strFind=Example_PAth"
set "_strInsert=Example_PAth_New"
:Replace
>"%OutputFile%" (
for /f "usebackq delims=" %%A in ("%InputFile%") do (
if "%%A" equ "%_strFind%" (echo %_strInsert%) else (echo %%A)
)
)
Problem: The resulting file is identical to the original one. I assume I am not reading the string properly? Or does anyone have an idea how to fix the code?
Update: I now modified the searched and substituted strings to include the whole line as mentioned by @Squashman. It works perfectly if the searched strings does not include the > character. If I keep the >, the line is detected properly but it will be replaced by a blank line. Any workaround? PS: I can't omit the > character.
'Target_Path' => 'Example_PAth',will be equal toExample_PAth. That is essentially what your code is doing.Example_PAth, just a part of a line. I suggest you to change yourifline by these two:set "line=%%A"andecho !line:%_strFind%=%_strInsert%!, andEnableDelayedExpansion.setlocal disableDelayedExpansionline bysetlocal EnableDelayedExpansionone as I said in my previous comment?