topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • Thursday December 12, 2024, 6:08 pm
  • Proudly celebrating 15+ years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Last post Author Topic: remv: Rename files (and directories) with regular expressions  (Read 41249 times)

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #25 on: January 14, 2019, 03:53 PM »
Yes, it does - so you can match subfolders.  :)

Not sure you got what I meant:

Would rename     'test\one\three\one\fred.pdf' to 'test\dne\three\one\frd.pdf' ...

Given the Regex I used it looks like the groups were:
test\o(ne\three\one\fr)e(d)

ie. The complete path was handled as a string instead of each component of the path, (directories and file), handled individually, (test, one, three, one, fred - each having the Regex performed against them).

If that's the way it's supposed to work then, no offence, it's going to be the strangest rename program I've ever seen and make working out the Regex a bit more complex, (although it would allow renaming dirs/files based on parent and/or sub-dirs).

Need more clarification.

Of course, it could of just been a complete fluke that I happened to create a directory structure and choose a Regex that would bring about this confusion ...

I don't know if you noticed but it was suggesting directory name changes even though I hadn't specified the -d parameter.

If they are a part of the path to a regular file, yes. You cannot rename a whole directory without a file in it though.
I could make the non-d call skip subfolders, but then -d would be the same thing as not using -r, or am I mistaken?

I think this is connected with the point above, is the full path handled as a string or are directories and files handled on their individual names?

Code: Text [Select]
  1. ls * | sed -E 's/(.*)text(.*)to(.*)remove(.*)/mv & "$1$2$3$4"/' | sh

Would a recursive version be the equivalent of changing to the next sub-dir and performing the same command?

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #26 on: January 14, 2019, 04:04 PM »
The full path is handled as a string (and split between path and filename and, optionally, extension during processing). I admit that some features can be confusing even to me.  ;D

So here's how it is intended to work:

  • remv "a" "b":
    The current directory is processed, all sub-directories are ignored. Folders won't be touched at all.
  • remv -r "a" "b":
    The current directory and all of its sub-directories are processed. If a path matches, it will be renamed.
    - a.txt will be renamed to b.txt
    - a\some.txt will be renamed to b\some.txt fixed with 1.3.0
    - However, a\ alone won't be renamed to b\ if no files match.
  • remv -dr "a" "b":
    The current directory and all of its sub-directories are processed. If a path matches, it will be renamed.
    - a.txt will be renamed to b.txt
    - a\some.txt will be renamed to b\some.txt
    - a\ alone will also be renamed to b\ if no files match.

 :-[
« Last Edit: January 14, 2019, 05:07 PM by Tuxman »

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #27 on: January 14, 2019, 04:51 PM »
I fixed the -E flag with version 1.2.2. That was a really dumb error.

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #28 on: January 14, 2019, 05:06 PM »
I fixed the somewhat anti-logical behavior of the -d flag with version 1.3.0. It should match the README now less badly.

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #29 on: January 14, 2019, 05:28 PM »
EDIT: You uploaded 1.3.0 while I was typing this, so used 1.3.0.

Still trying to work out what's supposed to happen w.r.t. paths/files using Regex so:

remv -sndrvv "o(.+)e(.+)" d$1$2 test

Acting on:
\test\test2\One\two\three\ozorroee\ofile.text

What do you think should be the expected outcome, (before running remv on it - what do you think the final full path should be)?

BTW, it suggested three (3) renames but did four (4)  ;)


And wait until you try non-greedy Regex:

remv -sndrvv "o(?:.+)e(.+)" d$1$2 test

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #30 on: January 14, 2019, 05:36 PM »
You and your cheeky test cases.
The expected outcome should be a simulation (-n).  :P

Your flags suggest that

\test\test2\One\two\three\ozorroee\ofile.text

should be renamed to

\test\test2\One\twd\three\ozorroee\ofile.txtxt

Or something.  :huh:

Surprise me: Which one have I forgot to log this time?

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #31 on: January 14, 2019, 05:50 PM »
Surprise me:

Surprise!

2019-01-15 10_24_57-K__test.png

remv -sdrvv "o(.+)e(.+)" d$1$2 test
2019-01-15 10_27_21-K__test.png


remv -sdrvv "o(?:.+)e(.+)" d$1$2 test
Code: Text [Select]
  1. K:\>remv -sdrvv "o(?:.+)e(.+)" d$1$2 test
  2.  Renaming           'test\test2\One\two\three\ozorroee\ofile.txt' to 'test\test2\One\twd.txt' ...
  3. Successfully renamed test\test2\One\two\three\ozorroee\ofile.txt to test\test2\One\twd.txt.
  4.  Renaming           'test\test2\One\two\three\ozorroee' to 'test\test2\One\twde' ...
  5. Successfully renamed test\test2\One\two\three\ozorroee to test\test2\One\twde.
  6.  Processed        'test\test2\One\two\three' ...
  7.  Skipping         'test\test2\One\two' ...
  8.  Skipping         'test\test2\One' ...
  9.  Skipping         'test\test2' ...
  10.  [STATS] Renamed 2 files and directories (0 failed, 4 skipped).

2019-01-15 10_48_53-K__test.png

Or something.  :huh:

 :P

OK, so if I've got this right, maybe, the full path is split into two parts only to be acted upon:

  • \test\test2\One\two\three\ozorroee\
  • ofile.text

?

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #32 on: January 14, 2019, 05:52 PM »
Yup, "processed" is a new output in 1.3.0, it means that the rename_file() function had created the target directory already.  :)
(Because if I would just count that as "exists", it'd fail.)

Ha!

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #33 on: January 14, 2019, 06:06 PM »
OK, so if I've got this right, maybe, the full path is split into two parts only to be acted upon:

  • \test\test2\One\two\three\ozorroee\
  • ofile.text

?

Starting with the current version, more or less.

With -dr, the whole path is seen as one string (with or without the file's extension  ;)), regex_replace() is applied to it.
Without -dr, only the second part (= the file name, with or without the file's extension  ;)) will be processed. In fact, the regex_search() function will not even see the parent path anymore.

Code: C++ [Select]
  1. if (bRenameDirectories) {
  2.     // If the user has decided to not rename directories, why would we
  3.     // want to rename paths on the way?
  4.     out_path_stream << parent_path_stream.str();
  5. }

(Stop modifying your postings while I reply to them.  :mad: :-[)

4wd

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 5,644
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #34 on: January 14, 2019, 06:09 PM »
OK, so if I've got this right, maybe, the full path is split into two parts only to be acted upon:

  • \test\test2\One\two\three\ozorroee\
  • ofile.text

?

Starting with the current version, more or less.

With -dr, the whole path is seen as one string (with or without the file's extension  ;)), regex_replace() is applied to it.
Without -d, only the second part (= the file name, with or without the file's extension  ;)) will be processed.

(Stop modifying your postings while I reply to them.  :mad: :-[)

OK, now it makes sense ... was completely at odds with every other rename program I've used  ;D

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #35 on: January 14, 2019, 06:11 PM »
I added a clarification above.  :D

was completely at odds with every other rename program I've used  ;D

I had not tested most of them, I guess. Sorry for breaking your habits?

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #36 on: September 05, 2019, 08:17 PM »
Moved the repo over to my server:
https://code.rosaelefanten.org/remv

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #37 on: October 28, 2020, 05:41 AM »
PSA: As remv can - under some circumstances (e.g. parallel editing) - destroy data, I'll relicense it today with a No Warranty clause. I'm not yet sure which license to choose, but I have a preference for the MIT-0 as of now.

Tuxman

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 2,508
    • View Profile
    • Donate to Member
Re: remv: Rename files (and directories) with regular expressions
« Reply #38 on: June 01, 2022, 09:29 AM »
remv 2.0.0 comes with a new (internal) API - parameters are now passed in a struct - and the new "f" flag to only replace the first match in a name. :)