Nora is a command line utility for renaming multiple files at the same time using regex with advanced control. It uses a simple custom language created only for this purpose. Any suggestion is appricated (using github issues)
If you want to support my work
cargo install nora_rename
Other methods comming soon
Command line usage
nora [OPTIONS] <INPUT> <OUTPUT>
commands | description |
---|---|
-h | --help |
Print help information |
-s | --skip |
Skip the renaming preview and directly rename files |
-V | --version |
Print version information |
-p | --pretty_print |
Pretty print the output for easier reading |
-g | --global |
Removes the global match from the captures |
-l | --path |
Change the base directory to look for files to rename |
-r | --recursive |
Recursively get files in directory |
-d | --depth_limit |
Specify a depth limit, if not, unlimited (used with -r) |
Input is a regex expression. Capture groups can be used (see here for using them in the output)
Named Captrure groups also works (see here for using them in the output)
The output expression is a little language easy to use
Two concepts are important, the unvariable parts and the interpreted parts
The unvariable part won't change when renaming while anything in the interpreted blocks will be interpreted considering this example:
[#1].txt
The unvariable part is the .txt
An interpreted block starts with [
and ends with ]
in this case the interpreted block is [#1]
which contains #1
The interpreted block can have the following expressions
- For loop
- Ternary expression
- Math expression
- String operation
- Identifiers
- String conversion
- Number conversion
Example:
[for x in 0.#cap_count { #x }]
This will go from 0 to #cap_count
which is evaluated as the number of caputre
#x
can be used to get the caputr group with the value of x
if x is 0 it will get #0
(first caputre group) etc
Note: This will often be used with the -g option because otherwise the whole caputres will also be included which will messed up things
Example:
[#1 == 10 ? 1 : 2]
Dependant on the condition, if it's true 1 will be returned otherwise 2 will be returned
The condition operator can be any of these ==
, !=
, <
, <=
, >
, >=
If you you want to do a ternary as a normal if statement to write something if it's true but nothing if not Example:
[#1 == 10 ? "something" : ""]
The skip operator can be used to simplify this Example:
[#1 == 10 ?> "something"]
Example:
[10 + 20]
currently supported operations are +
, -
, *
, /
, **
, //
, (
, )
**
: Power opertaor
//
: Log operator
It is important to note that math expressions will only be interpreted as mathematical expressions when the left paramter is a number
For example:
["10" + 20]
This will give 1020 since it will convert the left to a string and do a concatenation
[10 + "20"]
This will give 30 since the left is a number and it will convert to string to a number automatically
Example:
["hello " + "world"]
It is important to note that concatenation of strings will only occur when the left paramter is a string
For example:
["10" + 20]
This will give 1020 since it will convert the left to a string and do a concatenation
[10 + "20"]
This will give 30 since the left is a number and it will convert to string to a number automatically
Example:
["testes" - "te"]
This will give the ouput stes
which removes the first te
found
Example:
["testes" -- "te"]
This will give the ouput ss
which removes all te
found
Identifers are variables from the interpreter and the regex Example:
[#1]
[foo]
[bar]
[#count]
These are all variables
variables starting with #
are reserved for the interpreted (this means your capture groups shouldn't start with #
)
when using a regex, the capture groups can be used in interpreted block like the following
[#0]
[#1]
...
[#n]
The number represent the capture groups in order.
it is important to note that #0
is the whole regex capture so to use the first capture group, it will be #1
that is on is only true if the global parameter is false, if global is true, #0 will be the first capture group
Using the named capture groups is similar to the normal capture group. The only difference is to ommit the #
at the start
For example, if a named capture group is test
it will be used as follows
[test]
Import Note: When using any capture group or named capture group identifiers, they are all strings by default.
To use mathematical expression when they are the left parameter of tha math epxression it is nessecary to convert them to numbers
Example:
[string(#1)]
Transform the expression between the parenthese to a string
Example:
[number(#1)]
Transform the expression between the parenthese to a number The second argument can also control the number of decimal
[number(10.532, 2)]
Will result in 10.53
nora '(\d+)\..*' '[#1].mkv'
nora '(\d+)\..*' '[number(#1) + 10].txt'
Rename files from (number>.txt to (number + 10).txt only if (number) is 0 if not leave it as (number).txt
nora '(\d+)\..*' '[#1 == 0 ? number(#1) + 10 : #1].txt'
for example if we have this file name 1 2 3 4 5.txt
if we want to remove the spaces, we can use a loop and the -g option to remove the whole captures
with this regex ([^\s]+)\s?([^\s]+)?
it matches all but spaces so we can loop the number of capture group given by #cap_count
to connect them back together
You can look here to see how the regex works https://www.debuggex.com/r/9bUMa4OHscE8TyvR
nora -g '([^\s]+)\s?([^\s]+)?' '[for x in 0.#cap_count { #[x] }]'
The loop will loop from 0 to #cap_count which will be the number of found captures
#[x]
will get the content of the capture group for the value of x so if x is 0 it will be like doing #0
etc
this will result in the file being renamed to 12345.txt
if x doesn't exist or when it tries to index a variable with #0 for example and it doesn't exist it will produce an error
it can be made optional with a ?
like so #[x?]