elef has asked for the wisdom of the Perl Monks concerning the following question:
#!/usr/bin/perl use strict; use warnings; use utf8; my $inputfile; print "\nDrag and drop a file here:\n"; chomp ($inputfile = <STDIN>); $inputfile =~ s/^ *[\"\']?([^\"\']*)[\"\']?/$1/; print "\nFilepath with quotes and spaces stripped: >$inputfile<\n"; print "\n--------------------------------------------\n"; print "\nTest 1: no parsing, just checking if file is found:\n"; if (-e "$inputfile") {print "\nOK, file found\n";} else {print "\nERRO +R: file not found\n";} print "\n--------------------------------------------\n\nTest 2, check +ing if file can be opened:\n\n"; open(FILE, "<", "$inputfile") or die "Can't open file: $!"; print "OK, file opened successfully. \n\nPress enter to quit\n"; <STDIN>;
# DRAG & DROP FILES do { print "\n\n-------------------------------------------------"; print "\n\nDrag and drop file 1 here and press enter.\n"; chomp ($file1_full = <STDIN>); # windows doesn't add quotes if there is no space in the path, + linux adds single quotes # strip any leading and trailing spaces and quotes; $1=everyth +ing up to last / or \, $2= everything from there up to the end except + spaces and "'. $file1_full =~ /^ *[\"\']?(.*)[\/\\]([^\"\']*)[\"\']? *$/; $folder = $1; $file1 = $2; $file1 =~ /(.*)\.(.*)/; $f1 = $1; $ext = lc($2); print "\nDrag and drop file 2 here and press enter. (This file + has to be in the same folder as file 1!)\n"; chomp ($file2_full = <STDIN>); $file2_full =~ /^ *[\"\']?(.*)[\/\\]([^\"\']*)[\"\']? *$/; $folder2 = $1; $file2 = $2; $file2 =~ /(.*)\.(.*)/; $f2 = $1; $ext2 = lc($2); print LOG "\nInput files dropped in: $file1 (${file1_full}), $ +file2 (${file2_full})"; unless ("$folder" eq "$folder2") { print "\n\n\nERROR! The two files are not in same folder. Try +again!\n($folder vs ${folder2})\n"; print LOG "\nERROR: The two files are not in same folder. $fol +der, $folder2"; } unless ("$file1_full" ne "$file2_full") { print "\n\n\nERROR! You dragged in the same file twice. Try ag +ain!\n"; print LOG "\nERROR: Same file dropped in twice"; } unless ("$ext" eq "$ext2") { print "\n\n\nERROR! The file extensions don't match. Try again +!\n($ext vs. $ext2)\n"; print LOG "\nERROR: Extensions don't match: $ext vs. $ext2"; } unless (-e "$folder/$file1") { print "\n\n\nERROR! File 1 not found (maybe its path or its fi +lename contains accented letters). Try again!\n(file: $folder/$file1) +\n"; print LOG "\nERROR: File 1 not found; folder: $folder, file: $ +file1"; } unless (-e "$folder2/$file2") { print "\n\n\nERROR! File 2 not found (maybe its path or its fi +lename contains accented letters). Try again!\n(file: $folder2/$file2 +)\n"; print LOG "\nERROR: File 2 not found; folder: $folder2, file: +$file2"; } if ($ext eq "doc") { print "\n\n\nERROR! Doc files are not supported. Convert to do +cx or txt and try again!\n"; print LOG "\nERROR: doc file dropped in"; } $alignfilename = "${f1}-${f2}"; close LOG; open (LOG, ">>:encoding(UTF-8)", "$scriptpath/scripts/log.txt" +) or print "\nCan't create log file: $!\nContinuing anyway.\n"; } until (("$folder" eq "$folder2") && ("$file1_full" ne "$file2_fu +ll") && ("$ext" eq "$ext2") && (-e "$folder/$file1") && (-e "$folder/ +$file2") && ($ext ne "doc"));
|
---|