0

I have loads of files that I want to rename using a batch file, they are music files in this case and start with artist name, album then song number an finally the song name.

I want, if possible without installing any package, to rename the files by removing the artist and album name.

Such as changing:

Nahko And Medicine For The People - HOKA - 15 Build A Bridge.wav

to

15 Build A Bridge.wav

First thought:

Is it possible to recognise a char as a digit? if so then substring the name from the digit to the end?

Alternatively

Check the common substring on other files and remove it.

In above example the common substring would be:

Nahko And Medicine For The People - HOKA -

Note: If the album only has 9 music's I dont mind removing a possible 0 from 09 but hopefully this wouldn't remove the 0 if there are more than 10 since I want the numbers to order the songs in order and music 2 would be after music 19 in such situation

7
  • Welcome to SU! Take the site tour & familiarize yourself with how SU operates. Also, see How to ask a good question including thoroughly [researching before asking and sharing what you found and why it didn’t meet your needs](superuser.com/help/…
    – Blindspots
    Mar 14, 2023 at 18:48
  • Better give good examples input files and expected renamed files Mar 14, 2023 at 18:49
  • I have given that..
    – Barreto
    Mar 14, 2023 at 18:49
  • Not clear enough Mar 14, 2023 at 18:51
  • 1
    One problem you are going to encounter is if using batch, it will barf when you come across ANY filename that contains an ampersand (like hall & oats, echo & the bunnymen, or Nahko & Medicine For The People). I am a lover of batch but when it barfs, you might know why. Batch is not a good language for string parsing. Sure, you can do it, but it is going to hurt. Mar 14, 2023 at 23:52

3 Answers 3

2

Change C:\Temp to folder with your songs and run this in PowerShell:

Get-ChildItem -Path "C:\Temp" -Recurse -Include "*.wav" | Rename-Item -NewName { $_.Name -replace '.*\b(\d+.*)','$1' }
6
  • thanks for the help, I am making a batch file, this syntax cant be used in regular cmd? I have several folders, and i can access all music files, i just want to understand how to manipulate the string with the name.
    – Barreto
    Mar 14, 2023 at 23:17
  • 1
    This is powershell script. It could not be executed in cmd/batch directly, but you can either create not .bat, but .ps1 file, or execute this script like so: powershell -Command " Get-ChildItem -Path ""C:\Temp"" ... " (all double quotes in original command should be doubled)
    – markalex
    Mar 14, 2023 at 23:30
  • 1
    Yes, although not IN batch, comes installed with windows and can be run FROM batch. I approve. Mar 14, 2023 at 23:56
  • I have solved the issue in the regular .bat, posted an answer here, but i am curious about powershell and i will try this approach one day since is not the first time its recommended over simple cmd. anyway thanks for your help
    – Barreto
    Mar 15, 2023 at 2:38
  • @SeñorCMasMas, change C:\Temp refers to the fact, that in my snippet files renamed in C:\Temp, but OP most likely has songs in some other folder.
    – markalex
    Mar 15, 2023 at 7:19
1

This is along the lines of what I was looking for:

  • recognise a digit to separate a string in two,
  • keep it simple without dependencies and external tools.

the following block is the relevant excerpt from the script, where %%c is the full name of the song.

::with delayed expation enabled
setlocal EnableDelayedExpansion

::divide the full name into two tokens at the first digit
for /f "tokens=2,* delims=-" %%a in ('echo "%%c" ^| findstr /r "^[0-9]*"') do (
      set name=%%b 
)
rename "%%c" "!name:~0,-3!" 

Note: I dont fully understand why I had two whitespace or invisible char at the end of the file name requiring the removal of two characters from the result (the third is to remove the " leftover)

Example:

Nahko And Medicine For The People - HOKA - 19 The Wolves Have Returned (feat. Trevor Hall, Xavier Rudd, Leah Song & Joseph).wav

Result

19 The Wolves Have Returned (feat. Trevor Hall, Xavier Rudd, Leah Song & Joseph).wav

-1

Using Perl's rename:

$ rename 's/.*\b(\d+.*)/$1/' ./*.wav
$ ls
15 Build A Bridge.wav

The regular expression matches as follows:

Node Explanation
.* any character except \n (0 or more times (matching the most amount possible))
\b the boundary between a word char (\w) and something that is not a word char
( group and capture to $1:
\d+ digits (0-9) (1 or more times (matching the most amount possible))
.* any character except \n (0 or more times (matching the most amount possible))
) end of $1
13
  • but here you are assuming i write each name of file and each wanted name? i edited the question btw.
    – Barreto
    Mar 14, 2023 at 18:43
  • Post edited to work on all *.wav Mar 14, 2023 at 18:52
  • what does this mean s/.*\b(\d+.*)/$1/ ? is it checking for digits in the name? And do I need to install Perl's rename ?
    – Barreto
    Mar 14, 2023 at 18:56
  • Yes, \d is to match digits. And yes, you have to install it. Which OS? Mar 14, 2023 at 18:58
  • I am on windows 10, although I didn't specify on the question I would like to keep it as simple as possible (not requiring additional installs) but seems neat solution
    – Barreto
    Mar 14, 2023 at 18:59

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .