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(pre, past)
{
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.file: temp.folder) {
if (!(extractfileext(temp.file) in 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(0, temp.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.file, false);
}
}
echo("Done! Replaced text a total of" SPC temp.var2 SPC "times!");
}