-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrename-tcx.pl
executable file
·69 lines (58 loc) · 1.48 KB
/
rename-tcx.pl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#!/usr/bin/perl -w
#
# Loops through all .tcx/.TCX files in the current directory, and renames them to YYYY-MM-DD-HHMMSS-Sport.tcx,
# where "Sport" is "Biking", "Other", etc as in the file.
#
# Makes "YYYY/MM" directories and moves the files into them based on their date.
#
# Made as a quick hack, and I hope this is useful for someone.
# Christian Løverås ([email protected])
use strict;
use File::Copy;
opendir(DIR, ".");
my @files = grep(/\.tcx$/i, readdir(DIR));
closedir(DIR);
my $file = undef;
my $sport;
my $file_new = undef;
my $year = undef;
my $month = undef;
my $cmd = undef;
my $counter = 0;
foreach $file (@files) {
print "$file\n";
unless (open(F, $file)) {
warn $!;
next;
}
while (<F>) {
next unless /Activity Sport/;
chomp;
# Get activity type
$_ =~ s/^.*Sport="(.*)".*/$1/;
$file_new = $_;
print "\t$file_new\n";
# Get date
$_ = <F>;
chomp;
$_ =~ s/.*(\d{4})-(\d{2})-(\d{2})T(\d{2}):(\d{2}):(\d{2}).*/$1-$2-$3-$4$5$6/;
$year = "$1";
$month = "$2";
$file_new = $_ . "-$file_new" . ".tcx";
}
close(F);
# Create directory/directories
unless (-d "$year") {
mkdir "$year", 0755;
}
unless (-d "$year/$month") {
mkdir "$year/$month", 0755;
}
# If this file seems to be a duplicate, add "duplicate" suffix.
if (-e "$year/$month/$file_new") {
$file_new .= "-duplicate";
}
# Move and rename file
print "\tmove($file $year/$month/$file_new)\n";
move("$file", "$year/$month/$file_new");
}