#!/usr/bin/perl # make-dated-and-timed-folder.pl # Version 1.0 # # by Apollia of Astroblahhh.Com. http://astroblahhh.com/ # # # Completed Feb. 9, 2014. # # Released Feb. 9, 2014. # # # Available here: # # http://astroblahhh.com/software/perl/make-dated-and-timed-folder-v1_0.txt # # # Works in Lucid Puppy Linux 5.2.8, but probably could be modified # to work even in Windows. # # The part that opens an RXVT window is the part most likely to be # incompatible with operating systems that aren't Puppy. # # And then there's the part that uses the Rox-Filer file manager # to open the newly-created folder. # # # Makes a new folder whose name contains the date and time, # in a format like this: # # 2014_2_8 13,14,37 # # Optionally, you can add a prefix or suffix to the folder name. # You can provide the prefix and/or suffix in the source code, # or have the script ask you for them when it runs. # # And, optionally, you can have AM or PM added to the time, # like this: # # 2014_2_8 13,14,37 pm # # The time will still be in 24-hour format, though. # # # ----- # # # Except for parts not by Apollia: # # Copyright (C) 2013 Apollia # # This program is free software: you can redistribute it and/or modify # it under the terms of the GNU Affero General Public License as # published by the Free Software Foundation, either version 3 of the # License, or (at your option) any later version. # # This program is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU Affero General Public License for more details. # # You should have received a copy of the GNU Affero General Public License # along with this program. If not, see http://www.gnu.org/licenses/ . # # # Contact info: http://astroblahhh.com/contact-apollia.shtml ################################################################ # # Some stuff you should leave alone # # This gets the script's location. use Cwd 'abs_path'; use File::Basename qw( dirname ); $this_script_path=abs_path($0); $this_script_parentdirs=dirname( $this_script_path ); print "Running $this_script_path...\n\n\n"; $escaped_script_path=$this_script_path; $escaped_script_path=~s/ /\\ /g; # Makes it possible to use the mkpath function: use File::Path; ################ # # Constants for readability # use constant false => 0; use constant true => 1; # # End of Constants for readability # ################ # End of some stuff you should leave alone # ################################################################ ####################################################################### # # Some stuff you can change # # $full_path_to_folder_in_which_to_create_dated_folder = $this_script_parentdirs; $should_put_am_or_pm_in_time = true; # true|false $prefix_for_folder_name="Default Prefix"; $suffix_for_folder_name="Default Suffix"; # You can set a default prefix and/or suffix for the folder name here. # # Won't be used if you set the script to ask you to type # in a prefix or suffix. $should_ask_for_a_prefix=true; $should_ask_for_a_suffix=true; # true|false # # If these are true, you're prompted to type in a prefix and/or suffix # to be added to the name of the folder that is about to be created. # # Even if you're asked for a prefix or suffix, they're optional # and can be skipped by just pressing the Enter key. # # # A dated folder name without a prefix or suffix looks like this: # # 2014_2_8 13,14,37 pm # # # With a prefix: # # Prefix - 2014_2_8 13,14,37 pm # # # With a suffix: # # 2014_2_8 13,14,37 pm - Suffix # # # With a prefix and suffix: # # Prefix - 2014_2_8 13,14,37 pm - Suffix $file_manager_shell_command_to_use="rox"; $open_dated_folder_when_script_is_finished = true; # true|false # # If true, the script will use the file manager shell command specified in # $file_manager_shell_command_to_use to open the dated folder. $open_container_of_dated_folder_when_script_is_finished = false; # true|false # # If true, the script will open $full_path_to_folder_in_which_to_create_dated_folder # when finished. # End of stuff you can change. # # # Below this point, nothing else in this script needs to be changed. # # ####################################################################### ################ # # If the user must be asked for input (a prefix or suffix for # the folder name), then, here, we open an RXVT window and # re-run the script. $first_argv_arg=$ARGV[0]; if ( $first_argv_arg != 1 ) # If the RXVT window hasn't been opened yet, # then, the first parameter won't be 1. { if ( $should_ask_for_a_prefix || $should_ask_for_a_suffix) { exec ("rxvt -rv +sb -sl 999 -e perl $escaped_script_path 1"); # The exec command kills this script and runs the above shell command, # which re-runs this script in an RXVT window with one parameter - # just a 1 - to make this script get past this point during the re-run. } } # # End of section where we open an RXVT window. # ################ $full_path_to_folder_in_which_to_create_dated_folder = AddSlashIfNeeded($full_path_to_folder_in_which_to_create_dated_folder); if ($should_ask_for_a_prefix) { print "Please type a prefix for the dated folder which is about to be created.\n\n> "; $prefix_for_folder_name = ; chomp $prefix_for_folder_name; } $prefix_for_folder_name=~s/[^A-Za-z0-9., _-]/-/g; # Gets rid of bad characters, replacing them with a dash. print "\n\nPrefix for folder name: $prefix_for_folder_name \n\n"; if ($should_ask_for_a_suffix) { print "\n\nPlease type a suffix for the dated folder which is about to be created.\n\n> "; $suffix_for_folder_name = ; chomp $suffix_for_folder_name; } $suffix_for_folder_name=~s/[^A-Za-z0-9., _-]/-/g; # Gets rid of bad characters, replacing them with a dash. print "\n\nSuffix for folder name: $suffix_for_folder_name \n\n "; Create_Dest_Path_If_Necessary("full_path_to_folder_in_which_to_create_dated_folder"); sub Create_Dest_Path_If_Necessary { $var=$_[0]; if (!-e $$var) { print "Path doesn't exist yet - $$var \nNow attempting to create path\n\n $$var"; $succeeded = mkpath $$var; if ($succeeded) { print "\n\nPath created!"; } } } CheckForDestPathErrors("full_path_to_folder_in_which_to_create_dated_folder"); sub CheckForDestPathErrors { $var=$_[0]; if (!-d $$var) { print "\n\nError: "; print $$var; print " is not a directory!\n"; print "Sorry, the path provided in the $var variable doesn't lead to a directory. Please put a different path in $var, or make $$var into a directory, and run this script again."; End_Script_and_Wait_for_Return(); } } sub AddSlashIfNeeded { $this_string=$_[0]; $lastchar = substr($this_string,length($this_string)-1,1); if ($lastchar ne "/") { $this_string .= "/"; } return $this_string; } sub Make_Dated_Folder_Name { $date = Make_Datestring_for_Folder_Name(); $result=""; if ( $prefix_for_folder_name ) { $result = $prefix_for_folder_name . " - "; } $result = $result . $date; if ($suffix_for_folder_name) { $result = $result . " - " . $suffix_for_folder_name; } return $result; } sub Make_Datestring_for_Folder_Name { ($second, $minute, $hour, $dayOfMonth, $month, $yearOffset, $dayOfWeek, $dayOfYear, $daylightSavings) = localtime(time); $year = 1900 + $yearOffset; $month = $month + 1; $second = sprintf("%02d", $second); $minute = sprintf("%02d", $minute); $hour = sprintf("%02d", $hour); $result = $year . "_" . $month . "_" . $dayOfMonth . " " . $hour . "," . $minute . "," . $second; if ($should_put_am_or_pm_in_time) { if ($hour>=12) { $result = $result . " pm"; } else { $result = $result . " am"; } } return $result; } $dated_folder_name=Make_Dated_Folder_Name(); print "\n\nDated folder name: $dated_folder_name\n\n"; $final_dated_folder_path=$full_path_to_folder_in_which_to_create_dated_folder . $dated_folder_name; Create_Dest_Path_If_Necessary("final_dated_folder_path"); $final_dated_folder_path_to_display=$final_dated_folder_path; if (-e $final_dated_folder_path) { #chmod 0400, $final_dated_folder_path; # Owner:Read permissions only. if ($open_dated_folder_when_script_is_finished) { $command_line="$file_manager_shell_command_to_use $final_dated_folder_path"; @system_args=($file_manager_shell_command_to_use, $final_dated_folder_path); print "\n\n\nOpening dated folder, $final_dated_folder_path...\n # @system_args\n\n"; system (@system_args); } if ($open_container_of_dated_folder_when_script_is_finished) { @system_args=($file_manager_shell_command_to_use, $full_path_to_folder_in_which_to_create_dated_folder); print "\n\n\nOpening folder containing dated folder, $full_path_to_folder_in_which_to_create_dated_folder...\n # @system_args\n\n"; system (@system_args); } } else # Couldn't find the folder at the path where it's now supposed to be. { print "The end of the script was reached, but no dated folder was found where it was supposed to be!\n\nTried to create $final_dated_folder_path but failed."; } print "\n\nScript finished!\n"; print "\nYou can press Return (or Enter) to close this window.\n"; $wait_for_return = ;