0

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.

6
  • 1
    So you are thinking that 'Target_Path' => 'Example_PAth', will be equal to Example_PAth. That is essentially what your code is doing. Commented Nov 8, 2018 at 13:53
  • Ok thanks for the clarification. But how can I make it look for the string only? Commented Nov 8, 2018 at 14:07
  • 2
    There is not any line that is equal to Example_PAth, just a part of a line. I suggest you to change your if line by these two: set "line=%%A" and echo !line:%_strFind%=%_strInsert%!, and EnableDelayedExpansion. Commented Nov 8, 2018 at 14:13
  • @Aacini , this has written 'Example_PAth_New' at every single line! Commented Nov 8, 2018 at 15:03
  • Did you changed the setlocal disableDelayedExpansion line by setlocal EnableDelayedExpansion one as I said in my previous comment? Commented Nov 8, 2018 at 16:03

1 Answer 1

1

I modified the original code accordingly to the recommendation I gave 12 hours ago and tested it to prove it works (after the OP said in an Update 10 hours ago that there is not a solution yet).

@echo off
setlocal EnableDelayedExpansion

: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 (
      set "line=%%A"
      echo !line:%_strFind%=%_strInsert%!
   )
)

OutputFile:

Header
$VAR1 = {
          'Keywords' => {
                               'X' => '80',
                               'Target_Path' => 'Example_PAth_New',
                               'Y' => 'File_Path',
                               'X' => '80',
                               'Y' => 'File_Path',
                               'X' => '80',
                               'Y' => 'File_Path',
               sdefkjnhksdf  koijsef ökiljsdf
               sdefkjnhksdf  koijsef ökiljsdf
               sdefkjnhksdf  koijsef ökiljsdf
Sign up to request clarification or add additional context in comments.

3 Comments

it worked. I miss interpreted your comment and inserted your suggestion solution within the if action and did not remove the il/else block
I tried to use your solution with another text file. but it seems it has a problem with !. IT deleted all ! and inserted ECHO is off. at some other positions
I managed to solve the !problem. But the ECHO IS OFF is still written when line is empty

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.