Ticket #874: mythrename-subdirectories.patch
File mythrename-subdirectories.patch, 3.7 KB (added by , 18 years ago) |
---|
-
contrib/mythrename.pl
20 20 use DBI; 21 21 use Getopt::Long; 22 22 use File::Path; 23 use File::Basename; 24 use File::Find; 23 25 24 26 # Some variables we'll use here 25 27 our ($dest, $format, $usage, $underscores, $live); … … 66 68 67 69 /var/video/show_names/ 68 70 71 WARNING: ALL symlinks within the destination directory and its 72 subdirectories (recursive) will be removed when using the --link option. 73 69 74 --live 70 75 71 Include live tv recordings, affects both linking and renaming. This assumes 72 that recgroup = "LiveTV" for live tv recordings. 76 Include live tv recordings, affects both linking and renaming. 73 77 74 78 default: do not link/rename live tv recordings 75 79 … … 98 102 \%a = am/pm 99 103 \%A = AM/PM 100 104 \%- = separator character 105 \%F = directory/folder (path separator) 101 106 102 107 * For end time, prepend an "e" to the appropriate time/date format code 103 108 above; i.e. "\%eG" gives the 24-hour hour for the end time. … … 108 113 109 114 * A suffix of .mpg or .nuv will be added where appropriate. 110 115 116 * To separate links into subdirectories, include the \%F format specifier 117 between the appropriate fields. For example, "\%T\%F\%S" would create 118 a directory for each title containing links for each recording named 119 by subtitle. You may use any number of subdirectories in your format 120 specifier. If used without the --link option, "\%F" will be replaced 121 with the "\%-" separator character. 122 111 123 --separator 112 124 113 125 The string used to separate sections of the link name. Specifying the … … 240 252 # Bad path 241 253 die "$dest is not a directory.\n" unless (-d $dest); 242 254 # Delete any old links 243 foreach my $file (<$dest/*>) { 244 next unless (-l $file); 245 unlink $file or die "Couldn't remove old symlink $file: $!\n"; 246 } 255 find sub { if (-l $_) { 256 unlink $_ or die "Couldn't remove old symlink $_: $!\n"; 257 } 258 }, $dest; 259 # Delete empty directories (should this be an option?) 260 # Let this fail silently for non-empty directories 261 finddepth sub { rmdir $_; }, $dest; 247 262 } 248 263 249 264 # Prepare a database queries … … 339 354 # Literals 340 355 $fields{'%'} = '%'; 341 356 ($fields{'-'} = $separator) =~ s/%/%%/g; 357 $fields{'F'} = $dest ? "\0" : "$separator"; 342 358 # Make the substitution 343 359 my $keys = join('|', sort keys %fields); 344 360 my $name = $format; … … 352 368 $name =~ s/(?:(?:$safe_sep)+\s*)+(?=[^\d\s])/$separator /sg; 353 369 $name =~ s/^($safe_sep|$safe_rep|\ )+//s; 354 370 $name =~ s/($safe_sep|$safe_rep|\ )+$//s; 371 $name =~ s/\0($safe_sep|$safe_rep|\ )+/\0/s; 372 $name =~ s/($safe_sep|$safe_rep|\ )+\0/\0/s; 355 373 # Underscores? 356 374 if ($underscores) { 357 375 $name =~ tr/ /_/s; 358 376 } 377 # Folders 378 $name =~ s/\0/\//sg; 359 379 # Get a shell-safe version of the filename (yes, I know it's not needed in this case, but I'm anal about such things) 360 380 my $safe_file = $info{'basename'}; 361 381 $safe_file =~ s/'/'\\''/sg; … … 375 395 } 376 396 $name .= $suffix; 377 397 # Create the link 398 my $directory = dirname("$dest/$name"); 399 unless (-e $directory) { 400 mkpath($directory, 0, 0755) 401 or die "Failed to create $directory: $!\n"; 402 } 378 403 symlink "$video_dir/".$info{'basename'}, "$dest/$name" 379 404 or die "Can't create symlink $dest/$name: $!\n"; 380 405 if (defined($verbose)) {