Multi Commander > Support and Feedback

Find & Replace or MultiScript to edit & output XML?

<< < (2/3) > >>

Mathias (Author):

--- Quote from: Ulfhednar on December 06, 2022, 15:35:58 ---So......  I'm guessing no non-overlapping SaR using script or MS rename tools is possible then...  :(

--- End quote ---

I don't know.

I don't understand what you are doing. So I can't setup a test to test it.


Ulfhednar:
Thanks for the replies.

@AlanJB
I have looked at Notepad++ but I think dnGREP may be the way to go IF I can figure out the scripting of loops for the system the dev has just implemented....
His code example is

--- Code: ---set folder C:\Repos\testFiles\test\scriptTest\
set pathtomatch *.cs
set searchfor string
set replacewith String
search
replace
set searchfor bool
set replacewith Boolean
search
replace
--- End code ---
  which of course doesn't have an ELSE type loop implemented.  Iteration being what I'm looking for.
 
@Mathias
I'm trying to do a non-overlapping search.

My searches are for different terms which may or may not be in the target files.

eg I have 1000 search terms & each file contains 300 possible matches.
I must SAR for each term but it may not find a match because each file is different.
Thus I need to search all terms for every file & replace as found. 

The search process must continue to run through the list if a search term is NOT matched until it hits the end of the search list. 
The current system you have in Find & Replace only works where every search term is present (overlapping search), a non-match halts the process execution.
Great dialog etc but not suitable for this taskwithout a switch to loop past non-matches... so I wondered about MScript as then I might be able to define loops.

ELSE & IF seem to be script terms for this type of loop but don't know if I can apply them here...

If I must write a script to loop 1500+ terms then I'd like some familiarity/hope of getting it right.

Jungle:
Well, S&R works:


--- Code: ---@var $sel_files = GetSourceSelectedPaths();
@var $sel_count = arrayCount($sel_files);
@var $find_text = "aaa\\nbbb";
@var $repl_text = "xxx\\nyyy";

@var $n;
for($n = 0; $n < $sel_count; $n++)
{
  @var $file = $sel_files[$n];
#  @var $new_file = PathGetPathPart($file) ^ PathGetNamePart($file, 1) + "_new" + PathGetFileExtPart($file);
  @var $new_file = "*_new.*";

  MC.Utils.FindAndReplace MODE="Many" FIND={$find_text} REPLACEWITH={$repl_text} FILE="{$file}" TARGET="{$new_file}" SILENT REPLACEALL OVERWRITE
}
--- End code ---

The full automation requires parsing CSV-file, of course. If its structure is known and "stable", it shouldn't be hard.

UPD. Actually, parsing CSV might be quite hard (in MC).

Jungle:
Here's updated version with simple CSV parser. Please, don't use in production! :)

Usage example:
 - paste this script into the MultiScript debugger
 - focus your CSV-file on the target panel
 - switch to source panel and select xml-files
 - return to debugger and run


--- Code: ---# parse csv S&R file
function ParseCSV($file, $separators, $quote_chars)
{
  if ( !(StrLen($separators) == 1) )
  {
    $separators = ",";
  }

  if ( StrLen($quote_chars) == 0 )
  {
    $quote_chars = "\"";
  }

  @var $reg_ex = "^([" + $quote_chars + "])(.*)\\1$";

  @var $lines = LoadArray($file);
  @var $lines_cnt = arrayCount($lines);

  # single-string multiline S&R patterns
  # $pattern[0] - search pattern, $pattern[1] - replace pattern
  @var $pattern[2] = {"", ""};

  @var $i;
  for ($i = 0; $i < $lines_cnt; $i++)
  {
    # $pair[0] - search pattern, $pair[1] - replace pattern
    @var $pair = StrSplit($lines[$i], $separators);

    # invalid line
    if ( !(arrayCount($pair) == 2) )
    {
      continue;
    }

    # append unquoted patterns
    $pattern[0] = $pattern[0] + StrRegExpReplace( $pair[0], $reg_ex, "$2" ) + "\n";
    $pattern[1] = $pattern[1] + StrRegExpReplace( $pair[1], $reg_ex, "$2" ) + "\n";
  }

  return $pattern;
}

###

@var $sar_file    = GetTargetFocusPath();
@var $sar_pattern = ParseCSV($sar_file, ";", "\"`");

if ( ( StrLen($sar_pattern[0]) * StrLen($sar_pattern[1]) ) > 0 )
{
  @var $sel_files = GetSourceSelectedPaths();
  @var $sel_count = arrayCount($sel_files);

  @var $n;
  for($n = 0; $n < $sel_count; $n++)
  {
    @var $file = $sel_files[$n];
    @var $new_file = "*_new.*";

    MC.Utils.FindAndReplace MODE="Many" FIND={$sar_pattern[0]} REPLACEWITH={$sar_pattern[1]} FILE="{$file}" TARGET="{$new_file}" SILENT REPLACEALL OVERWRITE
  }
}
--- End code ---

I haven't stress-tested it.

Ulfhednar:
Thanks for your extensive input Jungle  ;)

As you are a MScript maestro that has definitely given me something to work with!   8)
Once I have tried this out I can start filling in any gaps / quirks I encounter.

BTW I was using the CSV as source for the matches & then editing XML with the SAR output. 
This worked OK as the replacements are all contained by the existing XML structure & didn't impact the XML layout. 

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version