Graal Forums

Graal Forums (https://forums.graalonline.com/forums/index.php)
-   Code Gallery (https://forums.graalonline.com/forums/forumdisplay.php?f=179)
-   -   Mass text replacer in files (https://forums.graalonline.com/forums/showthread.php?t=84178)

xXziroXx 02-13-2009 01:16 PM

Mass text replacer in files
 
This is a tool I made to replace filenames in levels on a large scale, considering half of Maloria's levels were filled with (what I considered) badly named files.
  • Uses
    • Replace filenames
    • Replace links
    • etc...

For example, I wanted to replace all occurrences of the text "malinn_sign.gif" from all levels with the text "mal_malinn-sign.gif":

PHP Code:

replaceTxt("malinn-sign.gif""mal_malinn-sign.gif"); 

Starting scan on 1903 files...
Replaced text 1 times in levels/levels/overworld/mal-g06.nw!
Done! Replaced text a total of 1 times!

  • In order for the script to work in all instances, I had to make a check so it won't replace the sent text if it's found with "mal_" in front of it. This is to avoid a never ending while loop that would break the script and cause major server lag. I suggest changing temp.header to suit your needs.
  • It will only alter the first occurrence of the file found, so if you have duplicate names, it won't touch more then one of them.

For this to work, you need to give (npcserver) rw to the submitted folder, as well as it's subfolders.

PHP Code:

function replaceTxt(prepast)
{
  
temp.header "mal_"// Anti never ending while (...) loop
  
temp.allowedExt = { ".nw" }; // Will skip all files but these
  
temp.folderPath "levels/levels/"// Base folder
  
temp.checkSubs true// Scan through subfolders too?

  
temp.folder.loadFolder(temp.folderPath "*"temp.checkSubs);
  echo(
"Starting scan on" SPC temp.folder.size() SPC "files...");
  
  
temp.var2 NULL;
  for (
temp.filetemp.folder) {
    if (!(
extractfileext(temp.filein temp.allowedExt)) continue;
    
temp.file findfiles(extractfilename(temp.file), true)[0];
    
    
temp.lines "";
    
temp.lines.loadString(temp.file);
    
    
temp.var = NULL;
    while (
temp.lines.pos(pre) > -1) {
      
temp.pos temp.lines.pos(pre);
      if (
temp.lines.substring(temp.pos temp.header.length(), temp.header.length()) == temp.header) break;
      
      
temp.lines temp.lines.substring(0temp.pos) @ past temp.lines.substring(temp.pos pre.length());
      
temp.var ++;
      
temp.var2 ++;
    }
    
    if (
temp.var != NULL) {
      echo(
"Replaced text" SPC temp.var SPC "times in" SPC temp.file "!");
      
temp.lines.savestring(temp.filefalse);
    }
  }
  echo(
"Done! Replaced text a total of" SPC temp.var2 SPC "times!");



:D

James 02-13-2009 04:37 PM

Nice job, this is awesome. :)

cbk1994 02-13-2009 11:11 PM

Awesome, thanks!

[email protected] 02-15-2009 03:19 PM

v.cool :D i like ^^

xXziroXx 10-11-2011 09:06 AM

I don't like looking back and seeing my Code Library entries being really outdated and some not even working anymore, so I'll start updating them I suppose!

To start off, I've made this function more reliable in it's behaviour. The old code tended to randomly skip text to replace.

PHP Code:

function replaceTxt(prepast)
{
  
temp.header "mal_"// Anti never ending while (...) loop
  
temp.allowedExt = { ".nw" }; // Will skip all files but these
  
temp.folderPath "levels/levels/"// Base folder
  
temp.checkSubs true// Scan through subfolders too?
  
  
temp.folder.loadFolder(temp.folderPath "*"temp.checkSubs);
  echo(
"Starting scan on" SPC temp.folder.size() SPC "files...");
  
  
temp.totalLevelChanges NULL;
  for (
temp.filetemp.folder) {
    if (!(
extractfileext(temp.filein temp.allowedExt))
      continue;
    
    
temp.file findfiles(extractfilename(temp.file), true)[0];
    
temp.lines "";
    
temp.lines.loadString(temp.file);
    
    
// Times replaced this level
    
temp.currentLevelChanges NULL;
    while (
temp.lines.pos(pre) > -1) {
      
temp.pos temp.lines.pos(pre);
      if (
temp.lines.substring(temp.pos temp.header.length(), temp.header.length()) == temp.header)
        break;
            
      
temp.lines temp.lines.substring(0temp.pos) @ past temp.lines.substring(temp.pos pre.length());
      
temp.currentLevelChanges ++;
      
temp.totalLevelChanges ++;
    }
    
    if (
temp.currentLevelChanges != NULL) {
      echo(
"Replaced text" SPC temp.currentLevelChanges SPC "times in" SPC temp.file "!");
      
temp.lines.savestring(temp.filefalse);
    }
  }
  echo(
"Done! Replaced text a total of" SPC temp.totalLevelChanges SPC "times!");


If a mod would be so kind to update the first post... <3

Emera 10-11-2011 05:15 PM

1 Attachment(s)
x_x I love you too much...


All times are GMT +2. The time now is 06:08 PM.

Powered by vBulletin® Version 3.8.11
Copyright ©2000 - 2024, vBulletin Solutions Inc.
Copyright (C) 1998-2019 Toonslab All Rights Reserved.