Rename current item with name of current folder

Discuss and share scripts and script files...
jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Rename current item with name of current folder

Post by jacky »

Just a little script that will rename the current item to the name of the current folder, eg. if you're in "D:\Program Files\XYplorer" then the file's new name would be "XYplorer"

Code: Select all

regexreplace $name, <curitem>, "^.+\\([^\\]+?)\\[^\\]+?.*$", "$1";
 rename bat, $name;
Note that the extension remains unchanged. If you want to remove it, or always use one, simply change the second line to something like this:

Code: Select all

 rename bat, "$name.ext/e";
Proud XYplorer Fanatic

lem
Posts: 10
Joined: 27 Mar 2006 05:24

Post by lem »

AWESOME - thank you - perfect timing on this. I knew it was possible to do this now with scripting and was going to try and figure this out to get my feet wet as I've always wanted this command. Lo and behold, go looking for it today and you just posted it a few hours ago. Thanks again for sharing.

admin
Site Admin
Posts: 64857
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Re: Rename current item with name of current folder

Post by admin »

jacky wrote:

Code: Select all

regexreplace $name, <curitem>, "^.+\\([^\\]+?)\\[^\\]+?.*$", "$1";
 rename bat, $name;
Same but shorter, and also works in drives roots (sets name to [drive letter].ext):

Code: Select all

set $name, <curfolder>; 
  rename , $name;

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Post by jacky »

Yep, that's a much better solution. I have to say, before I read your post, I didn't about about <curfolder> :oops: I mean, I'm sure I did at some point, but I had completely forgotten about it!

Good one, thanks :)
Proud XYplorer Fanatic

ivan
Posts: 577
Joined: 02 Apr 2008 12:52
Contact:

Post by ivan »

"If it's not broken, then don't fix it" philosophy falls on death ears. "If it's not perfect, then keep improving" rules ftw! :P

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Post by jacky »

PS: btw you can make it even shorter, of course: ;)

Code: Select all

rename , <curfolder>
Proud XYplorer Fanatic

admin
Site Admin
Posts: 64857
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Post by admin »

jacky wrote:PS: btw you can make it even shorter, of course: ;)

Code: Select all

rename , <curfolder>
Ahhhhhh. :shock: :D Hey, that XY proggy with that scripting stuff really rocks...

ivan
Posts: 577
Joined: 02 Apr 2008 12:52
Contact:

Post by ivan »

jacky wrote:PS: btw you can make it even shorter, of course: ;)

Code: Select all

rename , <curfolder>
It must be noted that this script doesn't result in the same renaming result as the one posted in the first post if there are files that have the same name but different extension in the same folder.

ivan
Posts: 577
Joined: 02 Apr 2008 12:52
Contact:

Post by ivan »

Just as a follow up, is there any way to modify your original (long :wink: ) script to specify how the file should be suffixed if the same file already exists? Thanks a lot :D

jacky
XYwiki Master
Posts: 3106
Joined: 23 Aug 2005 22:25
Location: France
Contact:

Post by jacky »

I think both scripts should result in the same thing, and both use number suffix in case of collision. You can define the format used for that suffix on Configuration - Report : Filename templates
Proud XYplorer Fanatic

ivan
Posts: 577
Joined: 02 Apr 2008 12:52
Contact:

Post by ivan »

And what if I wanted that "suffix" to be just before a certain symbol at the end of a filename (the symbol is a dash if it helps)?

EDIT: I guess what I'm asking is: is it possible for a script to override a preference?

The logic behind it is that a person may not be aware of a preference or needs it tweaking a little.

admin
Site Admin
Posts: 64857
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Post by admin »

ivan wrote:And what if I wanted that "suffix" to be just before a certain symbol at the end of a filename (the symbol is a dash if it helps)?

EDIT: I guess what I'm asking is: is it possible for a script to override a preference?

The logic behind it is that a person may not be aware of a preference or needs it tweaking a little.
Good point. You will get that with the next beta release.

Code: Select all

      E.g., to name items as their containing folder while having full 
      control over the number suffix format and start number, you now 
      can use any of these scripts without fearing collisions:
        ::rename , <curfolder>-<#001>, p;
        ::rename , <curfolder>-<#00001>, p;
        ::rename , <curfolder>-<#23>, p;
This works already today, but collisions with existing files are possible.

ivan
Posts: 577
Joined: 02 Apr 2008 12:52
Contact:

Post by ivan »

admin wrote:Good point. You will get that with the next beta release.
But is it still possible to disable scripts overriding through a preference? Methinks it's not so can we please have some balance so if a script can override a preference then a preference should be able to overpower whether a script is able to stomp all over a preference. I hope I'm making some sense ;)
Windows Vista Business SP1 32-bit, Logitech MX400 mouse, SetPoint 4.60.122
Image

admin
Site Admin
Posts: 64857
Joined: 22 May 2004 16:48
Location: Win8.1, Win10, Win11, all @100%
Contact:

Post by admin »

ivan wrote:
admin wrote:Good point. You will get that with the next beta release.
But is it still possible to disable scripts overriding through a preference? Methinks it's not so can we please have some balance so if a script can override a preference then a preference should be able to overpower whether a script is able to stomp all over a preference. I hope I'm making some sense ;)
No. Try again, please.

eurytos
Posts: 200
Joined: 29 Jan 2008 15:53

Post by eurytos »

admin wrote:
ivan wrote:
admin wrote:Good point. You will get that with the next beta release.
But is it still possible to disable scripts overriding through a preference? Methinks it's not so can we please have some balance so if a script can override a preference then a preference should be able to overpower whether a script is able to stomp all over a preference. I hope I'm making some sense ;)
No. Try again, please.

I *think* he was trying to say that he wants to be able to choose whether a preference can be overwritten by a script.
something like:
preference one: script can overwrite
preference two: do not allow script to overwrite


I could be way off though...

Post Reply