N
N
Nikolai Chuprik2020-10-14 13:29:22
cmd/bat
Nikolai Chuprik, 2020-10-14 13:29:22

How to rename files by regular expression?

I lived my life, but I still don’t know how to cmd / bat. Help.

There are a lot of files with the name of the type,
2456_32_название_файла.jpg
they need to be renamed as a package to
2456-3.2 название файла.jpg

. We need to do two replays:

1. /_/g to " "- replace the underscores with spaces

2. /^(\d{4})\s(\d)(\d)\s(.*)$/ to "$1-$2.$3 $4"- write the version through a dot

It's not actually a regular expression (I know them well, I'll write anything), but how to do it in bat file?

Answer the question

In order to leave comments, you need to log in

3 answer(s)
W
wisgest, 2020-10-15
@wisgest

The task is solved entirely by the internal means of batch files without the use of external utilities. There are no regular expressions in them, but it is possible to get a substring, as well as replace all occurrences of one substring with another - read the command help SET.
It is also possible to parse a string into tokens according to a given delimiter using a command FORwith the key /F.

S
Sergey c0re, 2020-10-14
@erge

If directly on Windows Batch Scripting:
CMD / BAT - Batch rename files (.jpg) in different folders (BAT)?
https://stackoverflow.com/questions/21162119/regul...
If it doesn't matter, then use PowerShell:
www.outsidethebox.ms/17857
Or install bash and something like this:
https://stackoverflow.com/questions/ 11809666/renam...
in pure bash, something like this:

find . -type f -name "Friends*" -execdir bash -c 'mv "$1" "${1/\w+\s*-\s*(\d)x(\d+).*$/S0\1E\2.srt}"' _ {} \;

PS: example pulled from the link above, not applicable to your case.

S
shurshur, 2020-10-14
@shurshur

The simplest thing is to take this perl module to CPAN . In the composition there is a ready-made script for mass renaming by regular expression.
Or write a script in any other familiar language (python, ruby, etc.) that has support for regular expressions - the task does not look very complicated.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question