简体   繁体   中英

Change a string within a line from a text file using batch

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM