Become a Site Supporter and Never see Ads again!

Author Topic: output each sub-directories files to seperate text file  (Read 3903 times)

0 Members and 1 Guest are viewing this topic.

Offline JasonSobel

  • Trade Count: (8)
  • Needs to get out more...
  • *****
  • Posts: 3327
  • Gender: Male
    • My show list
output each sub-directories files to seperate text file
« on: February 06, 2010, 08:05:06 AM »
I have several hard drives with my music on them.  What I would like to do is create a playlist for each directory.  Since the .m3u playlist format is very simple, and essentially just a directory path and file name for each song, I was thinking that there was probably some DOS script or something to automate this process (and since I have thousands and thousands of shows in thousands of sub-directories, I would very much like to streamline this.

for reference, in the root of each drive, I've got folders for "Phish", "Phish-Side-Projects", and "Other".  and then within each of those folders, each show has its own folder.

so it looks like this:

e:\Phish\ph1992-12-02.35368.flac16
e:\Phish\ph1992-12-04.25827.flac16
e:\Phish\ph1992-12-30.86296.flac16
e:\Phish\ph1992-12-31.500.shnf
e:\Phish\ph1993-02-03.27194.flac16
e:\Phish\ph1993-02-04.20850.shnf
e:\Phish\ph1993-02-04.87504.flac16
...
e:\other\clubdelf2009-11-20.ck930.flac16
e:\other\clubdelf2009-11-20.ck930.flac24
e:\other\david-grisman2009-08-28.flac16
e:\other\dead2009-03-30.98218.flac16
e:\other\dead2009-05-09.akg460.flac16
e:\other\dead2009-05-11.kfog.flac16
e:\other\gd1971-02-18.85478.flac16
e:\other\gd1972-03-23.100000.flac16
e:\other\gd1973-05-13.100239.flac16
...


I've been looking around on the web, and if I run this guy:
dir *.flac /S /B /L > all-flac.txt

I get a list of all the FLAC files in every sub-directory on the whole drive saved into one text file (named "all-flac.txt").
what I want is for the files in each sub-directory to be saved into a separate text file for each sub-directory.  and ideally, the name of each text file would have the same name as the show folder.  so I'd end up with something like this:

ph1992-12-02.35368.flac16.txt
ph1992-12-04.25827.flac16.txt
ph1992-12-30.86296.flac16.txt
ph1992-12-31.500.shnf.txt
ph1993-02-03.27194.flac16.txt
ph1993-02-04.20850.shnf.txt
ph1993-02-04.87504.flac16.txt

and each text file would have the full path and file name for each FLAC file in the directory. (I'd have to run through it all again as well, looking for SHN files).

so, does anyone have any ideas?  I was never that savvy with DOS, but I figure that there has to be an easy way for me to do this, right?

thanks in advance.

Offline if_then_else

  • Trade Count: (0)
  • Taperssection Member
  • ***
  • Posts: 497
Re: output each sub-directories files to seperate text file
« Reply #1 on: February 06, 2010, 01:54:42 PM »
You could use a scripting language like Perl or Python which might be more flexible (and portable) than a batch script. I just wrote a quick Perl script which should suit your purpose and which should work fine on both Windows and Unix. (Feel free to adapt it if necessary.)

Code: [Select]
#!/usr/bin/perl
use File::Find;
use Cwd;
use File::Basename;

if ($#ARGV != 0) {
print "usage: $0 <root_directory_for your_search> \n";
exit;
}
else {
$dir = $ARGV[0];
find(\&edits, $dir);
}

sub edits() {

if ((-f $_)&&($_ =~ m/(\.flac|\.shn)$/i)) {

my $curdir = cwd;
my $currentdir = basename($curdir);

open(file, ">>".$File::Find::dir."/".$currentdir.".m3u");
my $os_filepath;
if ($^O eq q(MSWin32)) {
($os_filepath = $File::Find::name) =~ tr!/!\\!;
}
else {
($os_filepath = $File::Find::name)
}
print file "$os_filepath \n";
close(file);
}
}
« Last Edit: February 08, 2010, 04:54:36 AM by if_then_else »

Offline JasonSobel

  • Trade Count: (8)
  • Needs to get out more...
  • *****
  • Posts: 3327
  • Gender: Male
    • My show list
Re: output each sub-directories files to seperate text file
« Reply #2 on: February 07, 2010, 03:59:54 PM »
thanks for putting this together.  and please excuse my ignorance, but how exactly do I use that Perl script and put it to use?

Offline if_then_else

  • Trade Count: (0)
  • Taperssection Member
  • ***
  • Posts: 497
Re: output each sub-directories files to seperate text file
« Reply #3 on: February 08, 2010, 04:30:41 AM »
thanks for putting this together.  and please excuse my ignorance, but how exactly do I use that Perl script and put it to use?

Download and install ActivePerl (the most widespread Perl distribution for Windows):
http://www.activestate.com/activeperl/downloads/

Save the script (e.g. as index_phish_shows.pl) and run it from the command line via:

Code: [Select]
E:\>perl index_phish_shows.pl e:\Phish


(perl.exe needs to be on your PATH, otherwise specify the full path to perl.exe)

Offline JasonSobel

  • Trade Count: (8)
  • Needs to get out more...
  • *****
  • Posts: 3327
  • Gender: Male
    • My show list
Re: output each sub-directories files to seperate text file
« Reply #4 on: February 08, 2010, 06:22:04 AM »
great,  the part below was all that I needed to know.

E:\>perl index_phish_shows.pl e:\Phish

in my google searches last night, I figured out that I needed to install perl on my computer first (duh), so I went with Strawberry Perl (I usually tend toward open source stuff, so that was the main reason I chose strawberry perl).  but last night, when I was trying to run it, I was just typing in e:\>perl index_phish_shows.pl.  I didn't realize that I needed to type in the directory that I wanted it to run through as well.

anyway, I just ran it per your instructions, and it went through and worked just as it should.
However, I have one last question for you.  This script saves each text file to the same directory where each show is.  so each new text file is in its own directory.
If I wanted to save all the new text files in the same location, so they all went to "E:\Playlists" (or something like that), what would I change in the script?  I assume the changes would be on this line:

      open(file, ">>".$File::Find::dir."/".$currentdir.".m3u");

is that correct?  what would I change it to?  or would I have to change another line?

Thanks again for your help, I really appreciate this!

- Jason


Offline if_then_else

  • Trade Count: (0)
  • Taperssection Member
  • ***
  • Posts: 497
Re: output each sub-directories files to seperate text file
« Reply #5 on: February 08, 2010, 08:47:25 AM »
Commented version of the original script:

Code: [Select]
#!/usr/bin/perl
use File::Find;
use Cwd;
use File::Basename;

# If no argument is specified print out the basic syntax to run the script
if ($#ARGV != 0) {
print "usage: $0 <root_directory_for your_search> \n";
exit;
}
else {
# The script will start in $dir which is the first parameter to the script and then
# search each subdirectory of $dir recursively
$dir = $ARGV[0];
#For each subdirectory of $dir, execute the function &edits
find(\&edits, $dir);
}

sub edits() {
# If the item is a regular file and its file extension matches .flac or .shn
# do the following
if ((-f $_)&&($_ =~ m/(\.flac|\.shn)$/i)) {
#Get the absolute name (full path) of the directory you're in
my $curdir = cwd;
#Get the relative directory name only
my $currentdir = basename($curdir);
# Open a file handle to write
# $File::Find::dir is the full path to the current directory
# (Alternatively one could have used $curdir)
open(file, ">>".$File::Find::dir."/".$currentdir.".m3u");
my $os_filepath;
#If we are on Windows, convert all forward slashes to backslashes
if ($^O eq q(MSWin32)) {
($os_filepath = $File::Find::name) =~ tr!/!\\!;
}
else {
($os_filepath = $File::Find::name)
}
# Print the name of the current audio file to your play list
print file "$os_filepath \n";
close(file);
}
}

If you want to save all the play lists into one directory, just define another variable (like $targetdir). Like this:

Code: [Select]
#!/usr/bin/perl
use File::Find;
use Cwd;
use File::Basename;

my $targetdir;

if ($#ARGV != 1) {
print "usage: $0 <root_directory_for your_search> <target_directory_for_your_playlists>\n";
exit;
}
else {
my $dir = "$ARGV[0]";
$targetdir = "$ARGV[1]";
print "Your target directory for your playlists is $targetdir\n";
find(\&edits, $dir);
}

sub edits() {

if ((-f $_)&&($_ =~ m/(\.flac|\.shn)$/i)) {

my $curdir = cwd;
my $currentdir = basename($curdir);

open(file, ">> $targetdir/$currentdir.m3u");

print "Saving the playlist as $targetdir/$currentdir.m3u \n";

my $os_filepath;
if ($^O eq q(MSWin32)) {
($os_filepath = $File::Find::name) =~ tr!/!\\!;
}
else {
($os_filepath = $File::Find::name)
}
print file "$os_filepath \n";
close(file);
}
}


Run it like this:
Code: [Select]
E:\>perl index_phish_shows.pl e:\Phish e:\Playlists
« Last Edit: February 08, 2010, 09:41:11 AM by if_then_else »

Offline JasonSobel

  • Trade Count: (8)
  • Needs to get out more...
  • *****
  • Posts: 3327
  • Gender: Male
    • My show list
Re: output each sub-directories files to seperate text file
« Reply #6 on: February 08, 2010, 09:41:48 AM »
wonderful!

thanks so much for your help with this, I really appreciate it.  I'll give it a go this afternoon after work, but I suspect that this will do exactly what I'm looking for.

thanks,
Jason

Offline JasonSobel

  • Trade Count: (8)
  • Needs to get out more...
  • *****
  • Posts: 3327
  • Gender: Male
    • My show list
Re: output each sub-directories files to seperate text file
« Reply #7 on: February 08, 2010, 06:07:35 PM »
yes, the modified version to save all the playlists into a single directory worked like a charm, and accomplished exactly what I was looking to do.  Thanks again for the help, you've certainly saved me a LOT of time.

thanks,
Jason

 

RSS | Mobile
Page created in 0.039 seconds with 32 queries.
© 2002-2025 Taperssection.com
Powered by SMF