Given a list of filenames, how can you iterate through subdirectories and return a list of where those files exist?

1

3

I have discovered an old playlist that I'd really like to listen to. Unfortunately, it's one that was created for a USB stick that had music copied out of my collection into a flat folder.

What I want to do now is recreate that playlist by matching those filenames to the paths in my main music collection.

As an example, suppose my existing playlist looks like this:

F:\music\Artist 1 - Song 1.mp3
F:\music\Artist 2 - Song 2.mp3
F:\music\Artist 3 - Song 3.mp3

And after processing, I might end up with something like this:

W:\Music Collection\Compilations\Compilations Forever 6\Artist 1 - Song 1.mp3
W:\Music Collection\Artist 2\This is the Album\Artist 2 - Song 2.mp3
W:\Music Collection\Unsorted New Stuff\Artist 3 - Song 3.mp3

The filenames are guaranteed to be the same, the paths will always be different.

There are about 300 tracks on this playlist, so it would be laborious to manually search for each one individually. I've had a look at XCOPY, having read about EXCLUDE, I was hoping there'd be an INCLUDE, but alas that wasn't going to be an option. I'd also read about using a FOR loop inside a batch file, but it's been a long time since I wrote batch files and I'm not quite sure where to begin.

Thanks very much for your help

Iain Fraser

Posted 2014-03-14T00:27:11.063

Reputation: 655

Answers

3

I assume you can get your old playlist in a text file - call it "oldList.txt".

First you need to get a modified list in a text file that contains just the file names, without any path information. Then you can pipe the result of a recursive DIR command into FINDSTR, and use the modified list as literal, case insensitive search strings that must match the end of a full path from the DIR command.

Here is a simple batch script that should do the trick:

@echo off
(for /f "eol=: delims=" %%F in (oldList.txt) do echo \%%~nxF)>searchList.txt
dir /b /s /a-d "W:\Music Collection\*"|findstr /lieg:searchList.txt >newList.txt
del searchList.txt

dbenham

Posted 2014-03-14T00:27:11.063

Reputation: 9 212

1Absolutely beautiful and so fast. Interestingly your solution suffered from the same problem my regex did at first, which was that it would find files ending in a match - and I had a file called "03.mp3", of which there were a number "Artist - Live in 2003.mp3" would be a match for that. However, as you mentioned, it looks at the full path, so all I needed to do was add a slash at the beginning of each line of oldList.txt. Brilliant solution mate. – Iain Fraser – 2014-03-14T01:49:47.830

@IainFraser - Great idea to add the backslash before each file name in the search list. I've edited the answer. – dbenham – 2014-03-14T02:12:10.120

0

Okay, so here's what I did. It was all done with regular expressions. I'm 100% sure there's an easier way to do it than this, but it worked and I only had to do it once so good result.

I used an application called Regex Buddy, but you could conceivably use any half-decent text editor with regex support.

Please note, all of the following Regexs are set so ^ and $ match at line breaks and the regex is processed line-by-line.

I took the original file list:

F:\music\(Artist 1) - Song 1.mp3
F:\music\[Artist 2] - Song 2.mp3
F:\music\Artist 3 - Song 3 {remix}.mp3

and applied the following regex to it:

^.*\\
replace with empty string

Which results in:

(Artist 1) - Song 1.mp3
[Artist 2] - Song 2.mp3
Artist 3 - Song 3 {remix}.mp3

I then applied

[\.\[\]\(\)\{\}]
replace with
\$0

Which results in

\(Artist 1\) - Song 1\.mp3
\[Artist 2\] - Song 2\.mp3
Artist 3 - Song 3 \{remix\}\.mp3

I then applied

$\s
replace with
|

Which results in

\(Artist 1\) - Song 1\.mp3|\[Artist 2\] - Song 2\.mp3|Artist 3 - Song 3 \{remix\}\.mp3

I then applied

.*
replace with
^.*\\($0)$

Which results in

^.*\\(\(Artist 1\) - Song 1\.mp3|\[Artist 2\] - Song 2\.mp3|Artist 3 - Song 3 \{remix\}\.mp3)$

We now have a super long regex which incorporates all the files I'm looking for.

Now open the command prompt, browse to the music director any type the following:

dir /s/b > filelist.txt

When the command has finished, open up filelist.txt and apply the regex we built to it as a search.

Hey presto! All the files in the playlist found. There were of course a few false positives where I had duplicate file names, but it's good enough to get me rocking and didn't take all that much time to achieve (despite how it looks)

Iain Fraser

Posted 2014-03-14T00:27:11.063

Reputation: 655