Browse code

#0000033 file search case-insensitive use "say" instead of "print" for better code readability

René Wagner authored on 30.09.2019 19:20:57
Showing 3 changed files

1 1
old mode 100644
2 2
new mode 100755
... ...
@@ -5,6 +5,7 @@
5 5
 
6 6
 use warnings;
7 7
 use strict;
8
+use feature qw(say);
8 9
 use Cwd;
9 10
 use File::Basename;
10 11
 use File::Find;
... ...
@@ -38,8 +39,8 @@ sub main
38 39
 	my ( $dir ) = @_;
39 40
 	my ( $move_counter ) = 0;
40 41
 
41
-	print "working directory: $dir \r\n";
42
-	print "moving jpeg files to subfolder:\r\n";
42
+	say "working directory: $dir";
43
+	say "moving jpeg files to subfolder: $SUBFOLDER \r\n";
43 44
 	
44 45
 	my $destination_dir = "$dir\/$SUBFOLDER";
45 46
 
... ...
@@ -48,30 +49,36 @@ sub main
48 49
 		mkdir $destination_dir or die "Could not create destination dir $_ : $!";
49 50
 	}
50 51
 	
51
-	my @files = File::Find::Rule->file()->name( '*.jpg', '*.jpeg', '*.JPG', '*.JPEG' )->maxdepth(1)->in( $dir );
52
+	my @files = File::Find::Rule->file()->name( qr/\.jpe{0,1}g$/i )->maxdepth(1)->in( $dir );
52 53
 	
53 54
 	foreach my $file ( @files )
54 55
 	{
55 56
 		print $file .'...';
56 57
 		$move_counter++;
57
-		move($file, "$destination_dir\/".basename($file) ) or warn " move failed: $!";
58
-		print "\r\n";
58
+		if ( move($file, $destination_dir. '/' .basename($file) ) )
59
+		{ 
60
+			say "moved.";
61
+		}
62
+		else
63
+		{
64
+			say "move failed: $!";
65
+		}
59 66
 	}
60 67
 	
61
-	print "\r\nmoved $move_counter jpeg files to subfolder.\r\n";
68
+	say "\r\nmoved $move_counter jpeg files to subfolder.";
62 69
 }
63 70
 
64 71
 sub show_help
65 72
 {
66
-	print $PROGRAM . ' - version ' . $VERSION . "\r\n";
67
-	print "published under BSD 3 - Clause License \r\n";
68
-	print "Copyright(c) 2019 René Wagner\r\n";
69
-	print "https://www.codewolke.net/silkypix-helper/ \r\n";
70
-	print "\r\n";
71
-	print "Moves JPEG files to a designated subfolder, by default 'JPEG'\r\n";
72
-	print "\r\n";
73
-	print "commandline parameters:\r\n";
74
-	print "jpegdivider.pl - create a subfolder 'JPEG' in the current working dir and move jpeg files in there\r\n";
75
-	print "jpegdivider.pl <folder> - create a subfolder 'JPEG' in the given directory and move jpeg files in there\r\n";
76
-	print "jpegdivider.pl -h - show this help\r\n";
73
+	say $PROGRAM . ' - version ' . $VERSION;
74
+	say 'published under BSD 3 - Clause License';
75
+	say 'Copyright(c) 2019 René Wagner';
76
+	say 'https://www.codewolke.net/silkypix-helper/';
77
+	say '';
78
+	say 'Moves JPEG files to a designated subfolder, by default "JPEG"';
79
+	say '';
80
+	say 'commandline parameters:';
81
+	say 'jpegdivider.pl - create a subfolder "JPEG" in the current working dir and move jpeg files in there';
82
+	say 'jpegdivider.pl <folder> - create a subfolder "JPEG" in the given directory and move jpeg files in there';
83
+	say 'jpegdivider.pl -h - show this help';
77 84
 }
... ...
@@ -5,13 +5,14 @@
5 5
 
6 6
 use warnings;
7 7
 use strict;
8
+use feature qw(say);
8 9
 use Cwd;
9 10
 use File::Basename;
10 11
 use File::Find::Rule;
11 12
 use File::Find;
12 13
 use Switch;
13 14
 
14
-my $VERSION = '0.1';
15
+my $VERSION = '0.2';
15 16
 my $PROGRAM = 'Multiversion Sidecar Cleaner';
16 17
 
17 18
 my $delete_flag = 0;
... ...
@@ -36,29 +37,35 @@ sub main
36 37
 	my ( $dir, $delete ) = @_;
37 38
 	my ( $delcounter ) = 0;
38 39
 
39
-	print "working directory: $delete_dir \r\n";
40
-	print 'action           : '. ($delete_flag ? 'delete' : 'list only') . "\r\n \r\n";
41
-	print "files with newer versions:\r\n";
40
+	say "working directory: $delete_dir";
41
+	say 'action           : '. ($delete_flag ? 'delete' : 'list only') . "\r\n";
42
+	say 'files with newer versions:';
42 43
 
43 44
 	# alle Dateien in allen Unterordnern *.spd *.spf suchen
44 45
 
45
-	my @files = File::Find::Rule->file->name( '*.spd', '*.spf' )->in( $dir );
46
+	my @files = File::Find::Rule->file->name( qr/\.sp(d|f)$/i )->in( $dir );
46 47
 	
47 48
 	foreach my $file ( @files )
48 49
 	{
49 50
 		if ( exist_newer_file($file) )
50 51
 		{
51
-			print $file .'...newer file found!';
52
+			print $file .'...';
52 53
 			$delcounter++;
53 54
 			if ( $delete )
54 55
 			{
55
-				unlink $file or warn " Deletion failed: $!";
56
-			}	
57
-			print "\r\n";
56
+				if ( unlink $file )
57
+				{
58
+					say 'deleted.'
59
+				}
60
+				else
61
+				{
62
+					say " deletion failed: $!";
63
+				} 
64
+			}
58 65
 		}
59 66
 	}
60 67
 	
61
-	print "\r\nfound $delcounter duplicate file versions. \r\n";
68
+	say "\r\nfound $delcounter duplicate file versions.";
62 69
 }
63 70
 
64 71
 sub exist_newer_file
... ...
@@ -80,12 +87,11 @@ sub exist_newer_file
80 87
 		}
81 88
 		else
82 89
 		{ 
83
-			$version_regex = '['. ($fileparts[2] + 1) .'...9]';
90
+			$version_regex = '['. ($fileparts[2] + 1) .'-9]';
84 91
 		}
85 92
 		
86
-		my $filefinder_regex = "$fileparts[0].$fileparts[1].$version_regex.$fileparts[3]";
87
-	
88
-		my @version_files = File::Find::Rule->file->name( $filefinder_regex )->maxdepth(1)->in( dirname($original_file) );
93
+		my $filefinder_regex = "$fileparts[0]\\.$fileparts[1]\\.$version_regex\\.$fileparts[3]";
94
+		my @version_files = File::Find::Rule->file->name( qr/$filefinder_regex/i )->in( dirname($original_file) );
89 95
 	
90 96
 		if ( scalar @version_files )
91 97
 		{
... ...
@@ -98,18 +104,18 @@ sub exist_newer_file
98 104
 
99 105
 sub show_help
100 106
 {
101
-	print $PROGRAM . ' - version ' . $VERSION . "\r\n";
102
-	print "published under BSD 3 - Clause License \r\n";
103
-	print "Copyright(c) 2019 René Wagner\r\n";
104
-	print "https://www.codewolke.net/silkypix-helper/ \r\n";
105
-	print "\r\n";
106
-	print "Deletes Silkypix Sidecar files when newer versions of the files are available.\r\n";
107
-	print "This usually happens when a file is opened in a newer version of Silkypix, as the sidecar files are version specific.\r\n";
108
-	print "\r\n";
109
-	print "commandline parameters:\r\n";
110
-	print "duplicate_sidecar_cleaner.pl - list duplicate sidecar file versions in current working directory and subfolders\r\n";
111
-	print "duplicate_sidecar_cleaner.pl -d - delete duplicate sidecar file versions in given directory and subfolders\r\n";	
112
-	print "duplicate_sidecar_cleaner.pl <folder> - list duplicate sidecar file versions in given directory and subfolders\r\n";
113
-	print "duplicate_sidecar_cleaner.pl -d <folder> - delete duplicate sidecar file versions in given directory and subfolders\r\n";
114
-	print "duplicate_sidecar_cleaner.pl -h - show this help\r\n";
107
+	say $PROGRAM . ' - version ' . $VERSION;
108
+	say 'published under BSD 3 - Clause License';
109
+	say 'Copyright(c) 2019 René Wagner';
110
+	say 'https://www.codewolke.net/silkypix-helper/';
111
+	say '';
112
+	say 'Deletes Silkypix Sidecar files when newer versions of the files are available.';
113
+	say 'This usually happens when a file is opened in a newer version of Silkypix, as the sidecar files are version specific.';
114
+	say '';
115
+	say 'commandline parameters:';
116
+	say 'duplicate_sidecar_cleaner.pl - list duplicate sidecar file versions in current working directory and subfolders';
117
+	say 'duplicate_sidecar_cleaner.pl -d - delete duplicate sidecar file versions in given directory and subfolders';	
118
+	say 'duplicate_sidecar_cleaner.pl <folder> - list duplicate sidecar file versions in given directory and subfolders';
119
+	say 'duplicate_sidecar_cleaner.pl -d <folder> - delete duplicate sidecar file versions in given directory and subfolders';
120
+	say 'duplicate_sidecar_cleaner.pl -h - show this help';
115 121
 }
... ...
@@ -5,13 +5,14 @@
5 5
 
6 6
 use warnings;
7 7
 use strict;
8
+use feature qw(say);
8 9
 use Cwd;
9 10
 use File::Basename;
10 11
 use File::Find::Rule;
11 12
 use File::Find;
12 13
 use Switch;
13 14
 
14
-my $VERSION = '0.1';
15
+my $VERSION = '0.2';
15 16
 my $PROGRAM = 'Orphaned Sidecar Cleaner';
16 17
 
17 18
 # parse commandline args
... ...
@@ -38,28 +39,34 @@ sub main
38 39
 	my ( $dir, $delete ) = @_;
39 40
 	my ( $delcounter ) = 0;
40 41
 
41
-	print "working directory: $delete_dir \r\n";
42
-	print 'action           : '. ($delete_flag ? 'delete' : 'list only') . "\r\n \r\n";
43
-	print "files with missing raw:\r\n";
42
+	say "working directory: $delete_dir";
43
+	say 'action           : '. ($delete_flag ? 'delete' : 'list only') ."\r\n";
44
+	say 'files with missing raw:';
44 45
 
45 46
 	# alle Dateien in allen Unterordnern *.spd *.spf suchen
46
-	my @files = File::Find::Rule->file->name( '*.spd', '*.spf' )->in( $dir );
47
+	my @files = File::Find::Rule->file->name( qr/\.sp(d|f)$/i )->in( $dir );
47 48
 	
48 49
 	foreach my $file ( @files )
49 50
 	{
50 51
 		if ( ! exists_raw_filename($file) )
51 52
 		{
52
-			print $file .'...raw file not found!';
53
+			print $file .'...';
53 54
 			$delcounter++;
54 55
 			if ( $delete )
55 56
 			{
56
-				unlink $file or warn " Deletion failed: $!";
57
-			}	
58
-			print "\r\n";
57
+				if ( unlink $file )
58
+				{
59
+					say 'deleted.';
60
+				}
61
+				else
62
+				{
63
+					say " deletion failed: $!";
64
+				} 
65
+			}
59 66
 		}
60 67
 	}
61 68
 	
62
-	print "\r\nfound $delcounter orphaned sidecar files found.\r\n";
69
+	say "\r\nfound $delcounter orphaned sidecar files found.";
63 70
 }
64 71
 
65 72
 sub exists_raw_filename
... ...
@@ -78,17 +85,17 @@ sub exists_raw_filename
78 85
 
79 86
 sub show_help
80 87
 {
81
-	print $PROGRAM . ' - version ' . $VERSION . "\r\n";
82
-	print "published under BSD 3 - Clause License \r\n";
83
-	print "Copyright(c) 2019 René Wagner\r\n";
84
-	print "https://www.codewolke.net/silkypix-helper/ \r\n";
85
-	print "\r\n";
86
-	print "Deletes Silkypix Sidecar files when corresponding RAW files are missing.\r\n";
87
-	print "\r\n";
88
-	print "commandline parameters:\r\n";
89
-	print "orphaned_sidecar_cleaner.pl - list orphaned sidecar file in current working directory and subfolders\r\n";
90
-	print "orphaned_sidecar_cleaner.pl -d - delete orphaned sidecar file in current working directory and subfolders\r\n";	
91
-	print "orphaned_sidecar_cleaner.pl <folder> - list orphaned sidecar file in given directory and subfolders\r\n";
92
-	print "orphaned_sidecar_cleaner.pl -d <folder> - delete orphaned sidecar file in given directory and subfolders\r\n";
93
-	print "orphaned_sidecar_cleaner.pl -h - show this help\r\n";
88
+	say $PROGRAM . ' - version ' . $VERSION;
89
+	say 'published under BSD 3 - Clause License';
90
+	say 'Copyright(c) 2019 René Wagner';
91
+	say 'https://www.codewolke.net/silkypix-helper/ ';
92
+	say '';
93
+	say 'Deletes Silkypix Sidecar files when corresponding RAW files are missing.';
94
+	say '';
95
+	say 'commandline parameters:';
96
+	say 'orphaned_sidecar_cleaner.pl - list orphaned sidecar file in current working directory and subfolders';
97
+	say 'orphaned_sidecar_cleaner.pl -d - delete orphaned sidecar file in current working directory and subfolders';	
98
+	say 'orphaned_sidecar_cleaner.pl <folder> - list orphaned sidecar file in given directory and subfolders';
99
+	say 'orphaned_sidecar_cleaner.pl -d <folder> - delete orphaned sidecar file in given directory and subfolders';
100
+	say 'orphaned_sidecar_cleaner.pl -h - show this help';
94 101
 }