ismaelgv / rnr

A command-line tool to batch rename files and directories
MIT License
474 stars 15 forks source link

[Feature request] Add file names enumeration (counter) option #26

Open suphamster opened 2 years ago

suphamster commented 2 years ago

For example I have some files: sadasd.png, asdjklsa,png, kflsadl.png And I want to rename them to: 1.png, 2.png, 3.png So as far as I know in current state it's impossible to do it? P.S. Yeah, I know there are tons of tools that can do it but I'm interested in rnr only because it has working in cmd undo feature.

ismaelgv commented 1 year ago

@suphamster sorry for the late response. This is something that is not trivial to support with the other current features. I will try to take a look at it.

bmparr commented 1 year ago

In addition to the original request, I’d like to add:

One has files: tmp01.png, tmp02.png, tmp04.png, tmp06.png in a directory.

I want to add the files file1, file2 and file3 to the directory. I would like the holes in the numbering to be used first, then with additional files, they would go to the end of the numbering. Hence adding the files would result in:

tmp01.png, tmp02.png, tmp03.png(renamed file1), tmp04.png, tmp05.png(renamed file2), tmp06.png, tmp07.png(renamed file3).

Motivation? If you’re rsyncing these files somewhere, only the new files need be sent.

Is a handy tool. This addition would be nice. thanks.

ismaelgv commented 1 year ago

@bmparr it is an interesting use case.first, I need to check how the original issue is going be implemented. We could build up from there later on.

krisalyssa commented 1 year ago

I think this could be easier than it appears at first glance.

Step 1 would be to make ReplaceMode.RegExp.replacement a function instead of a String at https://github.com/ismaelgv/rnr/blob/master/src/config.rs#L47. Default it to a function which returns the String currently stored in replacement.

Step 2 would be to call the function instead of using the replacement String at https://github.com/ismaelgv/rnr/blob/master/src/renamer.rs#L77.

These two steps can be done without altering the behavior of the app as it currently stands.

Now we get to the harder part.

Some syntax needs to be defined to indicate that you want a counter to be used in the replacement pattern. For the sake of discussion, let's say that a replacement pattern like {#}.jpg will use a counter.

Step 3 would be to parse the replacement pattern instead of using it verbatim at https://github.com/ismaelgv/rnr/blob/master/src/config.rs#L134. Set replacement to a function which formats a counter into the replacement pattern and returns the resulting String. There would also need to be some persistent state.

I realize that action speaks louder than words, so I'll try to put together some code towards this and PR it.

ismaelgv commented 1 year ago

Thanks for providing some ideas here.

Actually, I think the hard part here is integrating the solution with some possible conflicting files.

Step 1 would be to make ReplaceMode.RegExp.replacement a function instead of a String at https://github.com/ismaelgv/rnr/blob/master/src/config.rs#L47. Default it to a function which returns the String currently stored in replacement.

Step 2 would be to call the function instead of using the replacement String at https://github.com/ismaelgv/rnr/blob/master/src/renamer.rs#L77.

Not sure why it would need these two first steps.

Some syntax needs to be defined to indicate that you want a counter to be used in the replacement pattern. For the sake of discussion, let's say that a replacement pattern like {#}.jpg will use a counter.

I would try to find something that does not create a conflict with the regexp language.

Step 3 would be to parse the replacement pattern instead of using it verbatim at https://github.com/ismaelgv/rnr/blob/master/src/config.rs#L134. Set replacement to a function which formats a counter into the replacement pattern and returns the resulting String. There would also need to be some persistent state.

The persistent state it is needed, but, as I said at the top of the comment, if we are adding this feature we should try to do it smart enough to continue the current numeration.

For example, if file03.txt exists and I am numbering additional three files, it should rename to file01.txt, file02.txt and file04.txt

That last part it is not trivial to add to the current flow, since we need to read the current files and update the counter to skip some files.

Probably, this use case should be included in a new and separate subcommand, so it does not interact with the current behavior.

krisalyssa commented 1 year ago

Not sure why it would need these two first steps.

Because currently the replacement is a static string, and what we're talking about adding support for is making the replacement different for each file it renames.

I would try to find something that does not create a conflict with the regexp language.

This is in the replacement, which doesn't accept regular expressions, right? Only the first command line value (named <EXPRESSION> in the usage text) can be a regexp, right?

if we are adding this feature we should try to do it smart enough to continue the current numeration.

I think that's fine as a stretch goal, but there's plenty of value in implementing a bulk rename with an incrementing counter that doesn't fill gaps.

I can't speak to any of the previous commenters, but what I have in mind (and what brought me to rnr in the first place) is a command-line version of https://manytricks.com/namemangler/ that's cross-platform.

ismaelgv commented 1 year ago

Ok, it seems fair. I think I could give it a try and evaluate how to integrate it with the current options.

I would like to implement the "smart" version, if that is too complex I will go with the simple one.

It would be very helpful if you can test it and provide some feedback if possible.

Edit:

This is in the replacement, which doesn't accept regular expressions, right? Only the first command line value (named in the usage text) can be a regexp, right?

Actually, it accepts capture groups and named capture groups, so we need to be careful here.