text replacer

Discuss and share scripts and script files...
Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

text replacer

Post 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.
Last edited by Muroph on 20 Mar 2010 00:37, edited 2 times in total.
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: text replacer

Post by lukescammell »

Quick question, but does this do find and replace on the file CONTENTS or the file NAME?
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: text replacer

Post by Muroph »

lukescammell wrote:Quick question, but does this do find and replace on the file CONTENTS or the file NAME?
contents.
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

cpusrvc
Posts: 56
Joined: 02 Jan 2010 06:51
Location: S. Florida, USA
Contact:

Re: text replacer

Post 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.

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: text replacer

Post 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:
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

lukescammell
Posts: 744
Joined: 28 Jul 2006 13:15
Location: Kent, UK
Contact:

Re: text replacer

Post 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
Used to update to the latest beta every day. Now I have children instead…
Windows 10 Pro x64 (everywhere except phone…)

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Count me in!

Post 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!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Simple version request

Post 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)!
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Text Replacer, v1.1

Post 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"?
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: text replacer

Post 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...*
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

PeterH
Posts: 2599
Joined: 21 Nov 2005 20:39
Location: Germany

Re: text replacer

Post 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: )

SkyFrontier
Posts: 2341
Joined: 04 Jan 2010 14:27
Location: Pasárgada (eu vou!)

Re: text replacer

Post 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
New User's Ref. Guide and Quick Setup Guide can help a bit! Check XYplorer Resources Index for many useful links!
Want a new XYperience? XY MOD - surfYnXoard
-coz' the aim of computing is to free us to LIVE...

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: text replacer

Post 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
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

admin
Site Admin
Posts: 50922
Joined: 22 May 2004 16:48
Location: Cologne, Win 8.1, Win 10
Contact:

Re: text replacer

Post by admin »

BEACH! 8) The word alone is melting some of the snow here...
FAQ | XY News RSS | XY Twitter | Stay home! 🎸 Or wear a mask! 😷

Muroph
Posts: 561
Joined: 21 Aug 2007 16:13

Re: text replacer

Post 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 428 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:
My shared scripts:
TeraCopy Integration, Tag Manager, Comments Menu, Text Replacer, Image and Media Tools, Checksum Calculator, Video Calculator
only 5 URLs are allowed on the sig...

Post Reply