Selective Rename Script

Discuss and share scripts and script files...
Post Reply
phatmankerr
Posts: 186
Joined: 26 Dec 2005 19:35

Selective Rename Script

Post by phatmankerr »

Anybody point me in the right direction to achieve the following (did have a look at Jacky's example scripts but they are beyond my skill level),

Basically want to rename the selected files based on their current name e.g.

if current file name contains _DNM then remove _DNM
else
if current file name doesnt contain _DNM then add _DNM to end of filename (before extension) - suffix

So if selected files are...

test_DNM.txt
test1_DNM.txt

renamed to

test.txt
test1.txt

If selected files are...

test.txt
test1.txt

renamed to

test_DNM.txt
test1_DNM.txt

Any ideas much appreciated :? .

Thanks

Phat

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Selective Rename Script

Post by serendipity »

phatmankerr wrote:Anybody point me in the right direction to achieve the following (did have a look at Jacky's example scripts but they are beyond my skill level),

Basically want to rename the selected files based on their current name e.g.

if current file name contains _DNM then remove _DNM
else
if current file name doesnt contain _DNM then add _DNM to end of filename (before extension) - suffix

So if selected files are...

test_DNM.txt
test1_DNM.txt

renamed to

test.txt
test1.txt

If selected files are...

test.txt
test1.txt

renamed to

test_DNM.txt
test1_DNM.txt

Any ideas much appreciated :? .

Thanks

Phat
The below script is not optimized perfectly but is tested and works for me:

Code: Select all

//Remove _DNM if present and add _DNM if absent
   global $g_files, $g_count,$g_total;
   $g_files= getinfo("SelectedItemsPathNames", "|");
   replace $g_files, $g_files, "<curpath>\","";
   filter $g_files;
   sel a;
   $g_total= getinfo ("CountSelected");
   $g_count=1;
   sub _eval;

"_eval"
   global $g_files, $g_count,$g_total;
   sel $g_count;
   $difference = $g_total - $g_count;
   $check = $difference < 0 ? "_end" : "_start";
   sub $check;

"_start"
   global $g_files, $g_count;
   focus;
   #102;
   $clip = <clipboard>;
   $isDNM = $clip Like "*_DNM*" ? "_DNM" : "_notDNM";
   sub $isDNM; 

"_DNM"
   global $g_files, $g_count;
   rename b, "1111*";
   rename s, "_DNM/";
   $g_count= $g_count+1;
    sub _eval;

"_notDNM"
   global $g_files, $g_count;
   rename b, "0000*";
   rename b, "*_DNM";
   $g_count= $g_count+1;
   sub _eval;

"_end"
   sel a;
   rename s, "1111/";
   sel a;
   rename s, "0000/";
   filter;
   status "Done";


phatmankerr
Posts: 186
Joined: 26 Dec 2005 19:35

Re: Selective Rename Script

Post by phatmankerr »

Serendipity

Many thanks for that, it appears to do the job, but I get a Warning Every time I run it on the sub_eval; line

It says 'The current script appears to be recursive' - 'The stack size is 3.'

Is that how it should be ?

It will let me hit ok several times and the desired outcome is achieved.

Cheers

Phat

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Selective Rename Script

Post by serendipity »

phatmankerr wrote:Serendipity

Many thanks for that, it appears to do the job, but I get a Warning Every time I run it on the sub_eval; line

It says 'The current script appears to be recursive' - 'The stack size is 3.'

Is that how it should be ?

It will let me hit ok several times and the desired outcome is achieved.

Cheers

Phat
Oh, I am sorry. I should asked you if the recursion checker is off. That recursive check is to prevent endless looping scripts which will lead to XY crash. But if you are sure the script is not looping then you can disable it. So run the below code once from address bar or catalog and then your script should work without warnings.

Code: Select all

::#182; setkey "1", "ScriptRecursionWarningOff", "Settings", <xyini>; #190;

phatmankerr
Posts: 186
Joined: 26 Dec 2005 19:35

Re: Selective Rename Script

Post by phatmankerr »

Serendipity,

You are a star - works almost perfectedly :D

Is there anyway to include a check that at least one file is selected as i accidentaly ran it without a selected file and it renamed 300 files in the current tab (just run again to reverse the effect but was slightly alarming to watch the script change 300 files in a couple of seconds - although also quite impressive !).

Thanks again for all your help

Phat

serendipity
Posts: 3360
Joined: 07 May 2007 18:14
Location: NJ/NY

Re: Selective Rename Script

Post by serendipity »

phatmankerr wrote:Serendipity,

You are a star - works almost perfectedly :D

Is there anyway to include a check that at least one file is selected as i accidentaly ran it without a selected file and it renamed 300 files in the current tab (just run again to reverse the effect but was slightly alarming to watch the script change 300 files in a couple of seconds - although also quite impressive !).

Thanks again for all your help

Phat
No Problem, :D . Here is the updated version:

Code: Select all

//
   global $g_files, $g_count,$g_total;
   $selected= getinfo ("CountSelected");
   $check = $selected < 1 ? "_nothing" : "_filter";
   sub $check;
 
"_filter"
   global $g_files, $g_count,$g_total;
   $g_files= getinfo("SelectedItemsPathNames", "|");
   replace $g_files, $g_files, "<curpath>\","";
   filter $g_files;
   sel a;
   $g_total= getinfo ("CountSelected");
   $g_count=1;
   sub _eval;

"_eval"
   global $g_files, $g_count,$g_total;
   sel $g_count;
   $difference = $g_total - $g_count;
   $check = $difference < 0 ? "_end" : "_start";
   sub $check;

"_start"
   global $g_files, $g_count;
   focus;
   #102;
   $clip = <clipboard>;
   $isDNM = $clip Like "*_DNM*" ? "_DNM" : "_notDNM";
   sub $isDNM; 

"_DNM"
   global $g_files, $g_count;
   rename b, "1111*";
   rename s, "_DNM/";
   $g_count= $g_count+1;
   sub _eval;

"_notDNM"
   global $g_files, $g_count;
   rename b, "0000*";
   rename b, "*_DNM";
   $g_count= $g_count+1;
   sub _eval;

"_end"
   sel a;
   rename s, "1111/";
   sel a;
   rename s, "0000/";
   filter;
   status "Done";

"_nothing"
   msg "Select at least one item";


TheQwerty
Posts: 4373
Joined: 03 Aug 2007 22:30

Re: Selective Rename Script

Post by TheQwerty »

serendipity wrote:Oh, I am sorry. I should asked you if the recursion checker is off. That recursive check is to prevent endless looping scripts which will lead to XY crash. But if you are sure the script is not looping then you can disable it. So run the below code once from address bar or catalog and then your script should work without warnings.

Code: Select all

::#182; setkey "1", "ScriptRecursionWarningOff", "Settings", <xyini>; #190;
Ignore me if you already knew this but you can control this setting on a per-script level as well by using:

Code: Select all

Setting("AllowRecursion", 1);

phatmankerr
Posts: 186
Joined: 26 Dec 2005 19:35

Re: Selective Rename Script

Post by phatmankerr »

Serndipity,

Thanks for all your help with this :D

Now works exactly as I envisioned.

TheQwerty - serendipity may well have know, but it's new info for me - will probably include this in serendipity's script (and turn recursion checking back on - just in case) - Thanks

Phat

Post Reply