Page 1 of 2

text replacer

Posted: 05 Dec 2009 09:56
by Muroph
based on this thread i wrote a more generic script that's been quite useful to me.
just enter a regex like you would in regex rename and it will apply it on the selected files.
the script also saves the regex on an ini file for quick use the next time you run it.

Code: Select all

"Text Replacer v1"
	$sep=getkey(RegExpRenameSep,general);
	end (getinfo(countselected)<1);
	getkey $last,last,text_loop,"script_settings.ini";
	$regex=input("Enter RegEx","<match>$sep<replace>,$last");
	setkey $last,last,text_loop,"script_settings.ini";
	$filelist = getinfo('SelectedItemsPathNames','|');
	$count=1;
	while(1){
		$file=gettoken($filelist,$count,"|");
		if("$file"==""){break;}
		$match=gettoken($regex,1,$sep);
		$rep=gettoken($regex,2,$sep);
		$data=regexreplace(readfile($file),$match,$rep);
		writefile($file,$data);
		$count++;}
	status "Done!",,ready;
get the latest version here.

Re: text replacer

Posted: 09 Dec 2009 12:37
by lukescammell
Quick question, but does this do find and replace on the file CONTENTS or the file NAME?

Re: text replacer

Posted: 09 Dec 2009 14:13
by Muroph
lukescammell wrote:Quick question, but does this do find and replace on the file CONTENTS or the file NAME?
contents.

Re: text replacer

Posted: 02 Jan 2010 07:01
by cpusrvc
I was having a problem using the script. For each file in the selected list, it asked for the text replacement information. I moved these lines:

$regex=input("Enter RegEx","<match>$sep<replace>,$last");
setkey $last,last,text_loop,"script_settings.ini";


from inside the loop routine to just before the line

while(1){

Now it asks for the text strings just once instead of each file.

Re: text replacer

Posted: 04 Jan 2010 02:13
by Muroph
cpusrvc wrote:I was having a problem using the script. For each file in the selected list, it asked for the text replacement information. I moved these lines:

$regex=input("Enter RegEx","<match>$sep<replace>,$last");
setkey $last,last,text_loop,"script_settings.ini";


from inside the loop routine to just before the line

while(1){

Now it asks for the text strings just once instead of each file.
ah!
i noticed that on my script soon after posting it here (it was a copypasta error).
i fixed the script on my pc, but it looks like i forgot to fix the script on the post.
sorry! :oops:

Re: text replacer

Posted: 04 Jan 2010 12:11
by lukescammell
Very cool, I'm interested in your advanced version :)

I don't suppose this advanced version has an option to save a number of regex snippets and then add these to the current operation to run in a specified, reconfigurable sequence does it? :D

Count me in!

Posted: 04 Jan 2010 14:39
by SkyFrontier
Hello, Muroph!
Thank you for the script, I was badly needing such a thing a month ago.
Currently unable to test it properly on my work machine, where that project lays.
But please count me in - it seems that your advanced script will be very useful! :D
-Have a great new year all!

Simple version request

Posted: 04 Jan 2010 15:01
by SkyFrontier
Please don't laugh, but as a script newbie I HAVE to ask:
-would you mind in posting also a simpler version, which I could use along with CKS (meaning "custom keyboard shortcut")?
What I have in mind is: select mixed text files (.txt and .ini, for instance) and applying a text replacing previously written inside the script - no prompts, just "select files - press CKS".
It would be great having those 3 versions at hand on this thread for future references, also.
Many thanks - quickly tested on this machine and it works great (v8.80.0003)!

Text Replacer, v1.1

Posted: 04 Jan 2010 16:02
by SkyFrontier
-Suggestion:
I believe I saw anywhere that XYplorer now has a .txt check. So it would be great having such a check prior to making a file change.
-If it does NOT have such check, or that check doesn't fit on a script, I suggest that a extension check could be made prior to even trying to change a file's content. If it can be achieved, why not having that check included on "normal/standard", "simple" (no GUI, altered manually) and "advanced"?

Re: text replacer

Posted: 08 Jan 2010 22:48
by SkyFrontier
Confirmed - xyPlorer CAN check the nature of a .txt (ANSI, etc). It seems that this thread is dead, so I'll take some chance this weekend and try to get the stuff done - at least the simple version I need. *Geez, I should have learned computing...*

Re: text replacer

Posted: 08 Jan 2010 23:19
by PeterH
I didn't look much into this, but in the first post is said: "...will apply it to selected files" - so you should only select the files you want to change?
If you would not want to pay attention here, you could extend the script to do that. But then you had to tell the script, if only .bat, .txt, both or some more extensions should be edited - as they all are text, and *could* be meant.

I hope I understood correct what you talked about?

By the way - this isn't computing - this is only scripting :lol:
(Beg your pardon - this was a joke :oops: )

Re: text replacer

Posted: 09 Jan 2010 17:06
by SkyFrontier
I can't find where, but recall that XY does not "recognizes" files but it can be told to "write" files in ANSI, Unicode and such. What I said above,
"...a .txt check. So it would be great having such a check prior to making a file change.",
refers to this.
And yes, PeterH, in case XY can't recognize the structure of a file as being a pure .txt file, the other option would be telling it to which extensions apply the changes. BOTH options would be better in such scripts. Errr... where is our scripter??? lol

Re: text replacer

Posted: 09 Jan 2010 20:06
by Muroph
sorry for the late reply.
i'm currently spending my vacations on the beach. 8)
my last post was sent from a lan-house.
lukescammell wrote:Very cool, I'm interested in your advanced version :)

I don't suppose this advanced version has an option to save a number of regex snippets and then add these to the current operation to run in a specified, reconfigurable sequence does it? :D
i'll post the advanced version when i go back home, but it's much less intuitive than the "basic" version.

no snippets yet.
i was working on a system to quickly load some favorite regexes, but i think real snippets would need a more advanced input dialog (maybe using html).
SkyFrontier wrote:Please don't laugh, but as a script newbie I HAVE to ask:
-would you mind in posting also a simpler version, which I could use along with CKS (meaning "custom keyboard shortcut")?
What I have in mind is: select mixed text files (.txt and .ini, for instance) and applying a text replacing previously written inside the script - no prompts, just "select files - press CKS".
i was working on this just before coming to the beach.
wait 1 or 2 weeks. :P
SkyFrontier wrote:I believe I saw anywhere that XYplorer now has a .txt check. So it would be great having such a check prior to making a file change.
-If it does NOT have such check, or that check doesn't fit on a script, I suggest that a extension check could be made prior to even trying to change a file's content. If it can be achieved, why not having that check included on "normal/standard", "simple" (no GUI, altered manually) and "advanced"?
yup, i plan to do that.
and on the advanced version you can already force the script to write using ANSI or unicode.
PeterH wrote:I didn't look much into this, but in the first post is said: "...will apply it to selected files" - so you should only select the files you want to change?
If you would not want to pay attention here, you could extend the script to do that. But then you had to tell the script, if only .bat, .txt, both or some more extensions should be edited - as they all are text, and *could* be meant.
yup, you must select the files you want to change.
it should also work with some non-txt files, but i've never tested them before.


i should be back home by the end of the next week.
please, be patient. :P

Re: text replacer

Posted: 09 Jan 2010 20:33
by admin
BEACH! 8) The word alone is melting some of the snow here...

Re: text replacer

Posted: 21 Jan 2010 18:50
by Muroph
finally, here's the new version.
it's more powerful than the advanced version i mentioned before, and it's much more user friendly.
i'd say it's still in beta stage, tho.
setting are saved to a ini file.
you can change the ini's path and some UI setting near the beginning of the script.

favorites will come when i get some free time again, hopefully along with a better html UI.

how to use it:
- select the files you want to change.
- enter the search and replace regexes.
- select the output mode:
--- original: same as input file (uses SC filetype).
--- auto: let xyplorer decide (i.e. SC writefile default mode).
--- unicode, ascii or binary.
-optionally:
--- add a prefix and/or sufix to the file name (this will create a new file instead of modifying the original).
--- append or change extension (also creates a new file).
--- tell the script to remember (or not) the current operation for the next time you run it.
text_rep3_beta.xys
(9.65 KiB) Downloaded 571 times
admin wrote:BEACH! 8) The word alone is melting some of the snow here...
there wasn't a single sunny day in the 2 weeks i stayed there, tho. :cry: :evil: