Submit Hint Search The Forums LinksStatsPollsHeadlinesRSS
14,000 hints and counting!


Click here to return to the 'Sort files into date-labeled subfolders using Perl' hint
The following comments are owned by whoever posted them. This site is not responsible for what they say.
Sort files into date-labeled subfolders using Perl
Authored by: jaysoffian on Feb 25, '08 02:21:15PM
Why use the mtime, which could easily be incorrect, instead of the (IMO) more reliable date that's part of the filename itself?

BTW, it would've been a trivial modification to Organize iChat archive files by sender to refile by date, but this will do it off the filename and will also refile chats that were previously sorted by sender into 10.5-style date folders.


#!/usr/bin/perl -w

use strict;

my $PREVIEW = 1; # set to 0 to actually do it

sub mkdir_if_needed
{
	my ($dir) = @_;
	return if -e $dir;
	print "mkdir $dir\n";
	return if $PREVIEW;
	mkdir $dir;
}

sub rename_if_needed
{
	my ($src, $dst) = @_;
	return if -e $dst;
	print "$src -> $dst\n";
	return if $PREVIEW;
	rename $src, $dst;
}

# could use File::Find, but we're lazy
open(FIND, "find '$ENV{HOME}/Documents/iChats' -name '*.chat' -o -name '*.ichat'|");

while(<FIND>) 
{
	chomp;
	my ($base) = m|([^/]*)$|; 
	my ($date) = /on (\d{4}-\d\d-\d\d) at/;
	next unless $base and $date; 
	my $dstdir = "$ENV{HOME}/Documents/iChats/$date";
	my $dst = "$dstdir/$base"; 
	mkdir_if_needed($dstdir);
	rename_if_needed($_, $dst);
}


[ Reply to This | # ]
Sort files into date-labeled subfolders using Perl
Authored by: glgray on Mar 01, '08 06:19:06AM

Thanks for posting this script. I ran it and it work quite well except for 118 chats that didn't get filed. All these chats are dated October 25, 2003 or earlier. Is there some reason you can think of that it didn't organize these?

Thanks.



[ Reply to This | # ]