Using "search & replace" for cleaning multiple spaces?
Using "search & replace" for cleaning multiple spaces?
Hello, I'm new to this forum, I hope I posted this request at the right place...
I would like to make a search and replace option to quick clean some downloaded files, I do not know how to use either scipts or regex options, so the S&R options has been the easiest for me so far. Currently I'm using "[]_.-{};,!> " to clean up the files, but is there a way to include the removal of all excess or double spaces, so that they are also converted to a single space?? I've been looking at the help file, to see if there is a wildcard or special character to target spaces, but had no luck.
Also is there a way to assign a toolbar button for this, to make it a one button click cleaning option??
Thank you for your suggestions.
I would like to make a search and replace option to quick clean some downloaded files, I do not know how to use either scipts or regex options, so the S&R options has been the easiest for me so far. Currently I'm using "[]_.-{};,!> " to clean up the files, but is there a way to include the removal of all excess or double spaces, so that they are also converted to a single space?? I've been looking at the help file, to see if there is a wildcard or special character to target spaces, but had no luck.
Also is there a way to assign a toolbar button for this, to make it a one button click cleaning option??
Thank you for your suggestions.
Re: Using "search & replace" for cleaning multiple spaces?
Hi and welcome.
To do what you want i would use an script like this.
Select one file and execute this script.
(Please note that there is currently no error-handling coded.
Please select one file only and then execute this script.
Please don't test with your real (really important files without any backup) files, test always with an copy)
(To work on several files at once we have to tweak that script later once it do what you want)
To execute that script
see there > How to execute an Script ?
section "A" and there point "1".
To execute an script from an button
read section "B" and there point "4".
To do what you want i would use an script like this.
Select one file and execute this script.
(Please note that there is currently no error-handling coded.
Please select one file only and then execute this script.
Please don't test with your real (really important files without any backup) files, test always with an copy)
(To work on several files at once we have to tweak that script later once it do what you want)
Code: Select all
//get the current selected file base-name into an var:
$a = "<curbase>";
//replace an list of signs each with an blank:
//replacelist(string, searchlist, [replacelist], [separator], [matchcase])
$b = replacelist($a, "[]();!_-.{}", " ");
//replace multible spaces to one single space:
//regexreplace(string, pattern, replacement, [matchcase])
$c = regexreplace($b, "\s\s+", " ");
//see what happens:
msg "$a <crlf>$c <crlf 2>Rename or cancel",1;
//Method one:
//Renames the currently selected List item(s) according to the defined pattern and options:
//rename [mode (b|r|s|k|e)], pattern, [preview (p)], [itemlist], [flags=1]
//Method two:
//Renames a file or folder.
//renameitem(newname, [sourceitem=<curitem>], [flags=0], [numsuffix])
//flags 1: Rename base (keep extension)
renameitem($c,,1);
see there > How to execute an Script ?
section "A" and there point "1".
To execute an script from an button
read section "B" and there point "4".
Re: Using "search & replace" for cleaning multiple spaces?
Wow Stefan!, thank you for your help the script and pointing how to set it as a button. YES its working as it should!
Could you help me to tweak it to work for with either one or multiple files?.
I'm trying to learn as much as I can from your example and the logic of scripts, it is really interesting. Takes me a while to digest it...
If I know it works as I want, and wanted to make the script a one click step, without the confirmation of the rename, do I need to only remove the following part, right?
//see what happens:
msg "$a <crlf>$c <crlf 2>Rename or cancel",1;
How would a it handle a multiple rename collision? would it skip the file, or add a serialized number at the end? I personally prefer to add a number at the end.
Edit: I'm looking at your old post AutoHotkey: Dummy File Creator to understand how serializing works.
Could you help me to tweak it to work for with either one or multiple files?.
I'm trying to learn as much as I can from your example and the logic of scripts, it is really interesting. Takes me a while to digest it...
If I know it works as I want, and wanted to make the script a one click step, without the confirmation of the rename, do I need to only remove the following part, right?
//see what happens:
msg "$a <crlf>$c <crlf 2>Rename or cancel",1;
How would a it handle a multiple rename collision? would it skip the file, or add a serialized number at the end? I personally prefer to add a number at the end.
Edit: I'm looking at your old post AutoHotkey: Dummy File Creator to understand how serializing works.
Re: Using "search & replace" for cleaning multiple spaces?
livelike wrote:Wow Stefan!, thank you for your help the script and pointing how to set it as a button. YES its working as it should!
Could you help me to tweak it to work for with either one or multiple files?.
Please try this code with some test files:
Code: Select all
//get the current selected files base-names into an var:
//report([template], [itemlist], [header], [footer])
//get base plus extension (and add an pipe "|" for the foreach command)
//(Note: we could use {name} too to get the whole name.ext,
//but since i want to split name from extension later on, i use the following trick ....,
// "?" is here just an invalid sign in an file name, so great to use as an delimiter ;-) )
$files = report("{Basename}?{Ext}|",1);
//if nothing is selected:
if ($files==""){ msg "Nothing selected, will quit."; end 1;}
//test what files are selected and if this works:
msg "Debug: (selected files)<crlf>" . replace($files,"?","."),1;
//do something for each item in item-list:
//foreach($variable, listoftokens, [separator=|]) { statement(s); }
foreach($file, $files)
{
//don't work on the last, emtpy object:
if ($file==""){break;}
//split into base and extension (f.ex. to remove the dot only from the base name):
$base = gettoken($file, 1, "?");
$exte = gettoken($file, 2, "?");
$oldn = replace($file,"?", "."); //build the old orig name too
//*** user specific setting, depends on the task you have to do ***///
//replace an list of signs each with an blank:
//replacelist(string, searchlist, [replacelist], [separator], [matchcase])
$base = replacelist($base, "[]();!_-.{}", " ");
//replace multible spaces to one single space:
//regexreplace(string, pattern, replacement, [matchcase])
$base = regexreplace($base, "\s\s+", " ");
//*** end specific ***///
if ( "$base.$exte" == "$oldn" )
{
//skip if nothing is to do because there is no modifications done
}else{
//do the renaming:
//do an preview first:
$doit = 0; //initialize the var to set it to default = 0
//confirm(message) return: 1 on OK, 0 on Cancel
$doit = confirm( "Please check:<crlf 2>Orig: $oldn<crlf>New: $base.$exte<crlf 2>Rename or cancel to skip?" );
//to disable the confirmation, comment-out the line above and enable the line below:
//$doit = 1;
if ($doit==1)
{
//Renames a file or folder:
//renameitem(newname, [sourceitem=<curitem>], [flags=0], [numsuffix])
//numsuffix Number template to auto-suffix on collision.
//Set the start value and use "0"s as placeholders for numerical increments
//(e.g. "-01", starting with "-01", "-02", "-03", etc.)
//or "a"s as placeholders for alphabetical increments (e.g. "-aa", starting with "-aa", "-ab", -ac", etc.)
renameitem("$base.$exte",$oldn,0,"_001");
//renameitem($base, $oldn, 0, "_001");
}
}
}
That's great. If you have any questions search the forum or open an new threat, there are many members who can help you.I'm trying to learn as much as I can from your example and the logic of scripts, it is really interesting. Takes me a while to digest it...
You are absolutely right.If I know it works as I want, and wanted to make the script a one click step, without the confirmation of the rename, do I need to only remove the following part, right?
//see what happens:
msg "$a <crlf>$c <crlf 2>Rename or cancel",1;
Luckily Don have think about this already, see command renameitem(,,,numsuffix)How would a it handle a multiple rename collision? would it skip the file, or add a serialized number at the end? I personally prefer to add a number at the end.
Ohh, that's old stuff and good change i did silly things that time (as i maybe do right now too).Edit: I'm looking at your old post AutoHotkey: Dummy File Creator to understand how serializing works.
HTH?
- - -
EDIT:
added missing quotes:
renameitem("$base.$exte", $oldn , 0, "_001");
Last edited by Stefan on 02 Jul 2011 20:51, edited 2 times in total.
Re: Using "search & replace" for cleaning multiple spaces?
mmmm...found something
After renaming the files, they get added the extension name at the end of each file.
ie.
Original name New Name
test-01.txt test 01txt.txt
test _-02.txt test 02txt.txt
test-03 .txt test 03 txt.txt
I guess this is due that XYplorer, by default when renaming a file (using F2), it only selects the file's name and not the extension, even when it is visible (a great feat. to avoid changing extensions by mistake, and screwing up files).
So what I did, and I do not know if this is the correct way or not (just trial & error), was delete this the last part of the script:
renameitem($base.$exte,$oldn,0,"_001");
(including the dot before $exte)
to this
renameitem($base,$oldn,0,"_001");
and it works now, without duplicating the extension. I also tested the collision options, and its working great. !!!
will keep an eye on this part of the forum to learn more about scripting.
Vielen Dank Stefan!!!, and Don for the collision part,
PS. (I do not speak German )
After renaming the files, they get added the extension name at the end of each file.
ie.
Original name New Name
test-01.txt test 01txt.txt
test _-02.txt test 02txt.txt
test-03 .txt test 03 txt.txt
I guess this is due that XYplorer, by default when renaming a file (using F2), it only selects the file's name and not the extension, even when it is visible (a great feat. to avoid changing extensions by mistake, and screwing up files).
So what I did, and I do not know if this is the correct way or not (just trial & error), was delete this the last part of the script:
renameitem($base.$exte,$oldn,0,"_001");
(including the dot before $exte)
to this
renameitem($base,$oldn,0,"_001");
and it works now, without duplicating the extension. I also tested the collision options, and its working great. !!!
will keep an eye on this part of the forum to learn more about scripting.
Vielen Dank Stefan!!!, and Don for the collision part,
PS. (I do not speak German )
Re: Using "search & replace" for cleaning multiple spaces?
Oh yes, i have overseen this.livelike wrote:mmmm...found something
Code: Select all
renameitem($base.$exte,$oldn,0,"_001");
So extracting and using of $exte was not necessary at all in your case.renameitem(newname, [sourceitem=<curitem>], [flags=0], [numsuffix])
flags 0: [Default] Smart (keep extension unless extension is passed)
and
renameitem( $base, $oldn, 0, "_001" );
is pretty fine.
Good work livelike!
Re: Using "search & replace" for cleaning multiple spaces?
sorry, but I've been trying to add two things Stefan's code, hope somebody help me out
1. an exception so that the dots or periods between two numbers should NOT be replaced (should be left as they are).
This regex code seems to target fine my test in my notepad software (I'm using Editpad Lite),
(?<=\d)(\.*?)(?=\d)
I have not found the way to integrate in the code, so that it would work for one or multiple selected files, and work as an exception rule.
2. as a final step of the code, rename all the selected files using the #126; command (Aaa Aaa.ext)
for this, not sure where to add the #126; command, to be used as the final step of the script.
This is what I would like it to do:
Selected files:
TesTer-_@+5.FILe .v1.1.zip
A.1.3.B.C. TESTer;!-_FILE.v2.1.ZIP
TesTER -_fiLE.D.1.2.R.{}v3.1.ziP
Desired output after all clean up:
Tester 5 File V1.1.zip
A 1.3 B C Tester File V2.1.zip
Tester File D 1.2 R V3.1.zip
thanks
1. an exception so that the dots or periods between two numbers should NOT be replaced (should be left as they are).
This regex code seems to target fine my test in my notepad software (I'm using Editpad Lite),
(?<=\d)(\.*?)(?=\d)
I have not found the way to integrate in the code, so that it would work for one or multiple selected files, and work as an exception rule.
2. as a final step of the code, rename all the selected files using the #126; command (Aaa Aaa.ext)
for this, not sure where to add the #126; command, to be used as the final step of the script.
This is what I would like it to do:
Selected files:
TesTer-_@+5.FILe .v1.1.zip
A.1.3.B.C. TESTer;!-_FILE.v2.1.ZIP
TesTER -_fiLE.D.1.2.R.{}v3.1.ziP
Desired output after all clean up:
Tester 5 File V1.1.zip
A 1.3 B C Tester File V2.1.zip
Tester File D 1.2 R V3.1.zip
thanks
Re: Using "search & replace" for cleaning multiple spaces?
Hi,
Hope you don't mind me posting this, but these are some of the little scripts I use most often to rename large numbers of files. I find them really useful and have them attached to a button on the toolbar. I use one of the functions just about every day and there may be some useful lines here that might help you with files.
Just to add that Stefan was an inspiration to me, along with all the other help I've had on here:
Hope you don't mind me posting this, but these are some of the little scripts I use most often to rename large numbers of files. I find them really useful and have them attached to a button on the toolbar. I use one of the functions just about every day and there may be some useful lines here that might help you with files.
Just to add that Stefan was an inspiration to me, along with all the other help I've had on here:
Code: Select all
// My search and replace button script
"'Uppercase' First Letters" #126;
"And > and, etc." rename "s", " |_|.|And|Or|With|It|To|In|Of|The|—>> | | |and|or|with|it|to|in|of|the| - ", "p";
"Replace _ with spaces" rename "s", "_/ ", "p";
"Fix multiple spaces" rename "s", " | | >> | | ", "p";
"Replace '-' with ' - '" rename "s", "-/ - ", "p";
"Remove content in brackets" rename "r", "[\(\[][^\)\]]*[\)\]] > ", "p";
"Remove numbers" rename "r", "[0-9] > ", "p";
"Replace '- t...' with '- T...'" rename "s", "- t/- T", "p";
"Replace '- i...' with '- I...'" rename "s", "- i/- I", "p";
-
"Edit item names"
#147;// Edit selected items as a list in a text box
-
"Prefix Text" $string = input("Enter the text to prefix:"); rename "b", "$string*", "p"
"Append Text" $string = input("Enter the text to append:"); rename "b", "*$string", "p"
-
"Search and Replace" $find = input("Enter the text to be replaced:");
$replace = input("Enter the text to be inserted:");
rename "s", "$find/$replace", "p"
"Batch Rename" rename "b"
"Regex Rename" rename "r"
-
"Remove characters from start" $n = input("Number of characters to remove:");
rename r, "^(.{$n})(.+) > $2", "p";
"Remove characters from end (file)" $n = input ("Number of characters to remove:");
rename r, "^(.+)(.{$n})\.(.{3,4})$ > $1.$3", "p";
"Remove characters from end (folder)" $n = input ("Number of characters to remove:");
rename r, "^(.+)(.{$n})$ > $1", "p";
"Insert Character(s)" $char = input ("Enter the character to insert:");
$pos = input ("Enter position to enter the character:");
rename "r", "^(.{$pos})(.+)$ > $1$char$2", "p";
Last edited by Biggynuff on 02 Jul 2011 18:39, edited 1 time in total.
Re: Using "search & replace" for cleaning multiple spaces?
Thanks Biggynuff for sharing that!
Please note that the forum have eaten the leading blanks and so your script will not work for newbies without modification.
You may edit your post and add some more spaces to all the not-the-first-line-in-script lines?
livelike, please see if this will aid U:
//preserve dots between digits by masking them first:
FROM:
"XYplorer.Portable.File.Manager.v9.90.001"
TO:
"XYplorer Portable File Manager v9.90.001"
//Output: Xyplorer Portable File Manager V9.90.001
HTH?
--------------
Just out of interest, i did an few tests to build the menu commands to script commands:
My test file is:
XYplorer test File UPPER lower.txt
To change the case to:
XYplorer Test File UPPER Lower.Txt
use menu:
A* A*.* = Title-mode soft (preserve upper-case letters)
or in scripts:
#138;
And for strings in script use:
To change the case to:
Xyplorer Test File Upper Lower.txt
use menu:
Aaa Aa.* = Title-mode strict (all words to title-case)
or in scripts:
#126;
And for strings in script use:
To change the case to:
xyplorer test file upper lower.txt
use menu:
aaa aa.aaa = Lower (All to lower-case)
or in scripts:
#127;
And for strings in script use:
To change the case to:
XYPLORER TEST FILE UPPER LOWER.TXT
use menu:
AAA AA.AAA = Upper
or in scripts:
#128;
And for strings in script use:
To change the case to:
XYplorer Test File UPPER Lower.txt
use menu:
*.aaa = extension to lower
or in scripts:
#136;
And for strings in script use:
To change the case to:
XYplorer Test File UPPER Lower.TXT
use menu:
*.AAA = extension to upper
or in scripts:
#137;
And for strings in script use:
Please note that the forum have eaten the leading blanks and so your script will not work for newbies without modification.
You may edit your post and add some more spaces to all the not-the-first-line-in-script lines?
livelike, please see if this will aid U:
Trylivelike wrote: 1. an exception so that the dots or periods between two numbers should NOT be replaced (should be left as they are).
//preserve dots between digits by masking them first:
FROM:
"XYplorer.Portable.File.Manager.v9.90.001"
TO:
"XYplorer Portable File Manager v9.90.001"
Code: Select all
$base = "XYplorer.Portable.File.Manager.v9.90.001";
//*** user specific setting,
//preserve dots between digits by masking them first:
$base = regexreplace($base, "(\d)\.(\d)", "$1?$2");
//replace an list of signs each with an blank:
//replacelist(string, searchlist, [replacelist], [separator], [matchcase])
$base = replacelist($base, "[]();!_-.{}", " ");
//Replace placeholders back to dots:
$base = replace($base, "?", ".");
text $base;
//Output: XYplorer Portable File Manager v9.90.001
I think that command will works on selected files in the file list view, for re-casing an string by an script try command recase()livelike wrote: 2. as a final step of the code, rename all the selected files using the #126; command (Aaa Aaa.ext)
for this, not sure where to add the #126; command, to be used as the final step of the script.
Code: Select all
//title: to title case (first letter of each word to upper case, other letters to lower case)
$base = recase( $base , "Title");
//*** end specific
HTH?
--------------
Just out of interest, i did an few tests to build the menu commands to script commands:
My test file is:
XYplorer test File UPPER lower.txt
To change the case to:
XYplorer Test File UPPER Lower.Txt
use menu:
A* A*.* = Title-mode soft (preserve upper-case letters)
or in scripts:
#138;
And for strings in script use:
Code: Select all
$name = report("{basename}?{ext}",1);
$base = gettoken( $name, 1, "?");
$exte = gettoken( $name, 2, "?");
$base = recase( $base, "camel" );
text "$base.$exte";
To change the case to:
Xyplorer Test File Upper Lower.txt
use menu:
Aaa Aa.* = Title-mode strict (all words to title-case)
or in scripts:
#126;
And for strings in script use:
Code: Select all
$name = report("{basename}?{ext}",1);
$base = gettoken( $name, 1, "?");
$exte = gettoken( $name, 2, "?");
$base = recase( $base, "title" );
text "$base.$exte";
To change the case to:
xyplorer test file upper lower.txt
use menu:
aaa aa.aaa = Lower (All to lower-case)
or in scripts:
#127;
And for strings in script use:
Code: Select all
$name = recase( "<curname>", "lower");
Text $name;
To change the case to:
XYPLORER TEST FILE UPPER LOWER.TXT
use menu:
AAA AA.AAA = Upper
or in scripts:
#128;
And for strings in script use:
Code: Select all
$name = recase( "<curname>", "upper");
Text $name;
To change the case to:
XYplorer Test File UPPER Lower.txt
use menu:
*.aaa = extension to lower
or in scripts:
#136;
And for strings in script use:
Code: Select all
$name = report("{basename}?{ext}",1);
$base = gettoken( $name, 1, "?");
$exte = gettoken( $name, 2, "?");
$exte = recase( $exte, "lower" );
text "$base.$exte";
To change the case to:
XYplorer Test File UPPER Lower.TXT
use menu:
*.AAA = extension to upper
or in scripts:
#137;
And for strings in script use:
Code: Select all
$name = report("{basename}?{ext}",1);
$base = gettoken( $name, 1, "?");
$exte = gettoken( $name, 2, "?");
$exte = recase( $exte, "upper" );
text "$base.$exte";
Re: Using "search & replace" for cleaning multiple spaces?
Stefan to the rescue again!
I've edited the code in my post above. Hopefully it should be ok but I'm far from being an expert so feel free to let me know if anything needs changing
Biggy
I've edited the code in my post above. Hopefully it should be ok but I'm far from being an expert so feel free to let me know if anything needs changing
Biggy
Re: Using "search & replace" for cleaning multiple spaces?
Thanks Biggynuff for the codes these pretty useful for use and for scripting reference. I really liked the search and replace with the pop up window, and remove from end and start too.
Probably some of these options could be implemented in future versions of XYplorer under the File->rename special, as they are easier to use for newbies (like myself), but that's up to Don and if there is some free time for this.
Stefan, as usual thank you for your help, I thought of masking the files, but figured that if I could somehow make an exception out of the (?<=\d)(\.*?)(?=\d), I would make it cleaner, but I couldn't figure it out.
I inserted the codes you provided in the scripts, it works great, and also re-added the .$exte to the last part of the code (renameitem($base.$exte,$oldn,0,"_001");), as it was returning the test files without an extension.
But the result files, come up without the period before the extension:
from:
TesTer-_@+5.FILe .v1.1.zip
A.1.3.B.C. TESTer;!-_FILE.v2.1.ZIP
TesTER -_fiLE.D.1.2.R.{}v3.1.ziP
to:
Tester 5 File V1.1zip
A 1.3 B C Tester File V2.1zip
Tester File D 1.2 R V3.1zip
I guess I'm messing up something in how the extensions are targeted either of these lines :
$exte = gettoken($file, 2, "?");
$base = regexreplace($base, "(\d)\.(\d)", "$1?$2");
renameitem($base.$exte,$oldn,0,"_001");
Probably some of these options could be implemented in future versions of XYplorer under the File->rename special, as they are easier to use for newbies (like myself), but that's up to Don and if there is some free time for this.
Stefan, as usual thank you for your help, I thought of masking the files, but figured that if I could somehow make an exception out of the (?<=\d)(\.*?)(?=\d), I would make it cleaner, but I couldn't figure it out.
I inserted the codes you provided in the scripts, it works great, and also re-added the .$exte to the last part of the code (renameitem($base.$exte,$oldn,0,"_001");), as it was returning the test files without an extension.
But the result files, come up without the period before the extension:
from:
TesTer-_@+5.FILe .v1.1.zip
A.1.3.B.C. TESTer;!-_FILE.v2.1.ZIP
TesTER -_fiLE.D.1.2.R.{}v3.1.ziP
to:
Tester 5 File V1.1zip
A 1.3 B C Tester File V2.1zip
Tester File D 1.2 R V3.1zip
I guess I'm messing up something in how the extensions are targeted either of these lines :
$exte = gettoken($file, 2, "?");
$base = regexreplace($base, "(\d)\.(\d)", "$1?$2");
renameitem($base.$exte,$oldn,0,"_001");
Code: Select all
//get the current selected files base-names into an var:
//report([template], [itemlist], [header], [footer])
//get base plus extension (and add an pipe "|" for the foreach command)
//(Note: we could use {name} too to get the whole name.ext,
//but since i want to split name from extension later on, i use the following trick ....,
// "?" is here just an invalid sign in an file name, so great to use as an delimiter ;-) )
$files = report("{Basename}?{Ext}|",1);
//if nothing is selected:
if ($files==""){ msg "Nothing selected, will quit."; end 1;}
//test what files are selected and if this works:
//msg "Debug: (selected files)<crlf>" . replace($files,"?","."),1;
//do something for each item in item-list:
//foreach($variable, listoftokens, [separator=|]) { statement(s); }
foreach($file, $files)
{
//don't work on the last, emtpy object:
if ($file==""){break;}
//split into base and extension (f.ex. to remove the dot only from the base name):
$base = gettoken($file, 1, "?");
$exte = gettoken($file, 2, "?");
$oldn = replace($file,"?", "."); //build the old orig name too
//*** user specific setting, depends on the task you have to do ***///
//*** INSERTED CODE 1 ****
//preserve dots between digits by masking them first:
$base = regexreplace($base, "(\d)\.(\d)", "$1?$2");
//replace an list of signs each with an blank:
//replacelist(string, searchlist, [replacelist], [separator], [matchcase])
//*** INSERT CHARACTERS TO BE REPLACED ****
$base = replacelist($base, "[];!@+_-.{}", " ");
//replace multible spaces to one single space:
//regexreplace(string, pattern, replacement, [matchcase])
$base = regexreplace($base, "\s\s+", " ");
//*** INSERTED CODE 2 ****
//Replace placeholders back to dots:
$base = replace($base, "?", ".");
//*** end specific ***///
//*** INSERTED CODE 3 ****
//title: to title case (first letter of each word to upper case, other letters to lower case)
$base = recase( $base , "Title");
//*** end specific
if ( "$base.$exte" == "$oldn" )
{
//skip if nothing is to do because there is no modifications done
}else{
//do the renaming:
//do an preview first:
$doit = 0; //initialize the var to set it to default = 0
//confirm(message) return: 1 on OK, 0 on Cancel
//$doit = confirm( "Please check:<crlf 2>Orig: $oldn<crlf>New: $base.$exte<crlf 2>Rename or cancel to skip?" );
//to disable the confirmation, comment-out the line above and enable the line below:
$doit = 1;
if ($doit==1)
{
//Renames a file or folder:
//renameitem(newname, [sourceitem=<curitem>], [flags=0], [numsuffix])
//numsuffix Number template to auto-suffix on collision.
//Set the start value and use "0"s as placeholders for numerical increments
//(e.g. "-01", starting with "-01", "-02", "-03", etc.)
//or "a"s as placeholders for alphabetical increments (e.g. "-aa", starting with "-aa", "-ab", -ac", etc.)
renameitem($base.$exte,$oldn,0,"_001");
}
}
}
Re: Using "search & replace" for cleaning multiple spaces?
I didn't try an regex here, because regex are slow by nature.livelike wrote:Stefan, as usual thank you for your help, I thought of masking the files, but figured that if I could somehow make an exception out of the (?<=\d)(\.*?)(?=\d), I would make it cleaner, but I couldn't figure it out.
Your regex means:
(?<=\d) = positive lookbehind for an digit
(\.*?) = match literal dot, none-or-more of them non-greedy
(?=\d) = positve lookahead for an digit
"If" it works, --i didn't have test it--, you may want to use
(\.) to match one single dot, or
(\.+?) to match one-or-more non-greedy
but not
(\.*) because that would match even if there is no dot at all.
* means: none-or-more,
+ means: one-or-more
And because i think you want to reuse what is matched by your lookarounds,
you have to put the regex in parentheses there two:
(?<=(\d))(\.)(?=(\d)) , $1$3
But since XY returns -AFAIR- the original input if the regex didn't work,
you have to make an test first if your regex would really match.
Or after, like: if ($afterRE != $original ) { ...continue... }
Now $exte again?livelike wrote: I inserted the codes you provided in the scripts, it works great, and also re-added the .$exte to the last part of the code (renameitem($base.$exte,$oldn,0,"_001");), as it was returning the test files without an extension.
But the result files, come up without the period before the extension:
Well, i have shown it you wrong anyway, sorry.
From "Scripting for beginners..."
Code: Select all
Concatenate strings
Code:
The dot is used for concatenation like with PHP:
Example:
$TEST = "File name";
various test:
msg $TEST.ext; //===> results in 'File nameext'
msg $TEST."ext"; //===> results in 'File nameext'
msg $TEST.".ext"; //===> results in 'File name.ext'
msg "$TEST.ext"; //===> results in 'File name.ext'
Tip, so always use quotes "...".
So try:
renameitem( "$base.$exte", $oldn, 0, "_001" );
instead of our:
renameitem($base.$exte,$oldn,0,"_001");
by quoting $base.$exte, which will preserve the dot from its concatenating job and use it as literal dot.
BTW:
I don't know without testing if you even need to quote the "$oldName" just in case there are blanks?
But i think it could not harm:
renameitem( "$base.$exte", "$oldn", 0, "_001" );
Hope i have make not anymore mistakes now.
.
Re: Using "search & replace" for cleaning multiple spaces?
THANK YOU AGAIN Stefan, seriously, I really appreciate all the time and patience you dedicated in helping me out, and all the help you have provided and keep providing in the forum.
I tried and and its working like a charm (including renaming collisions, leaving periods between numbers, etc.) this is a really time saver!
I tried and and its working like a charm (including renaming collisions, leaving periods between numbers, etc.) this is a really time saver!
Re: Using "search & replace" for cleaning multiple spaces?
Stefan wrote: ↑10 Jun 2011 22:23
To execute that script
see there > How to execute an Script ?
section "A" and there point "1".
To execute an script from an button
read section "B" and there point "4".
I have been reading through this and with your code and maybe I am overthinking things but you say on that webpage you link to that there is a Section A and B and then points but I am confused as I searched that whole page and couldn't find even the word Section but once nor know what the different points are that you mention looking for. Could you clarify at all? And im sorry if this is a stupid question, i am just not seeing what you mean possibly because of my issues with adhd...
Re: Using "search & replace" for cleaning multiple spaces?
Im sorry if i ask too many questions but I have been trying to figure out how to have a button that i can press to remove all the "."s in a filename and replace them with a " " space that i have selected, I have been trying for a minute here and even tried to get chatgpt to help me by asking:
"User
could use tell me using this information what the script i need to write to place in a button that will replace all selected file names "." with " " minus the extension? Tour of Xyplorer Scripting:
https://www.xyplorer.com/tour.php?page=scripting
how to use and execute scripts: viewtopic.php?t=6560
How to make a button to run an existing script: viewtopic.php?t=9583
Script Exchange for Xyplorer Scripting: viewforum.php?f=7"
I never really got it working yet, the current xys file i have is
and i have this in the button is :
but not working, anyone willing to help me understand why its not so i can change and fix it?
"User
could use tell me using this information what the script i need to write to place in a button that will replace all selected file names "." with " " minus the extension? Tour of Xyplorer Scripting:
https://www.xyplorer.com/tour.php?page=scripting
how to use and execute scripts: viewtopic.php?t=6560
How to make a button to run an existing script: viewtopic.php?t=9583
Script Exchange for Xyplorer Scripting: viewforum.php?f=7"
I never really got it working yet, the current xys file i have is
Code: Select all
foreach($file, <get SelectedItemsPathNames>, , "e") {
$name = gettoken($file, -1, "\\");
$ext = gettoken($name, -1, ".");
$baseName = gettoken($name, 1, -2, ".");
$newBaseName = replace($baseName, ".", " ");
if ($ext != "") {
$newName = $newBaseName . "." . $ext;
} else {
$newName = $newBaseName;
}
renameitem($file, $newName, "c");
}
Code: Select all
load "C:\\Users\\damie\\AppData\\Roaming\\XYplorer\\Scripts\\Replace dot with space.xys";