|
|
Subject:
Script to sort directory and move first file to new location
Category: Computers > Programming Asked by: pigskinreferee-ga List Price: $10.00 |
Posted:
11 Nov 2005 12:57 PST
Expires: 11 Dec 2005 12:57 PST Question ID: 592003 |
To put it bluntly, my script writing skills suck. Therefore, I am attempting to solicit someone who can assist me. Prerequisites: 1) FreeBSD 5.4 2) Bash 3.16(1) 3) Perl 5.8.7 4) Php 5.5 5) Ruby The above are all installed on my system. I am running DADA Mail to send out a different announcement nightly. Presently I have to hard code in the location of each message to be sent. DADA offers the ability to send the same file ad-infinitum if desired. I have created a directory where all of the messages are stored that are to be distributed by DADA Mail. I have given them simple date names; i.e., 11-01-05.txt, 11-02-05.txt, etc. These are obviously the dates that they are to be sent out on. I also have a file named 2bSent in the same directory. I move the next file to be sent to this file. That accomplished the deletion of the old sent file as well as removing the file I just move from the directory. My problem is that I do not know how to write a script that I can run from CRON on a daily basis that will accomplish this for me automatically. I have tried using the find command as well as the sort command, but I do not know how to get the first file moved to the new file location. Basically, I have given up. I should also mention, that the script should check to see if there are any new files, and if not, just exit. I usually upload a months supply at a time. I might also add, that the server for this program is located in my home, so I have complete access to it. I could probably explain this better if someone is interested in working with me in this endeavor. -- Ciao |
|
There is no answer at this time. |
|
Subject:
Re: Script to sort directory and move first file to new location
From: bozo99-ga on 11 Nov 2005 15:51 PST |
The lazy man's way tp pick the first file is to use "ls" which by default sorts filenames. Combine that with a date format "2005-12-31.txt" and you'll have the first file picked by "ls -1 | head -1". If I understood the question properly this little bit of Perl may do what you want. #!/usr/bin/perl -w # change to the directory we need # (Obviously put that proper name in place of the one here.) chdir("/tmp/ciao") or die("could not change directory"); # Discover the date in the expected format. # (Check these fields are in the order you want.) # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = (); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($^T); $mon += 1; $year -= 100; $fname=sprintf("%02d-%02d-%02d.txt", $mon, $mday, $year); # Rename that file - if it exists. # if (-f $fname) { printf("Renaming %s\n", $fname); rename($fname,"2bSent"); } exit(0); |
Subject:
Re: Script to sort directory and move first file to new location
From: pigskinreferee-ga on 12 Nov 2005 05:38 PST |
I tried using your Perl program example, but without success. The Perl program was properly installed and I set the permissions to 755 as required. However, nothing happens. The file does run, but no actions are taken. I created a test directory to try the program out in. I loaded it with some sample files. This is the directory structure after issuing 'ls -1'. 11-11-05.htm 11-12-05.htm 11-13-05.htm 11-14-05.txt 11-15-05.txt 11-16-05.txt 11-17-05.txt 11-18-05.txt 11-19-05.txt 11-20-05.txt 11-21-05.txt 11-22-05.txt 11-23-05.txt 11-24-05.txt 11-25-05.txt 11-26-05.txt 11-27-05.txt I named the Perl program 'file_4_dada.sh'. After running the program, nothing has changed in the directory. I did change the 'chdir' line to reflect the proper directory to work on. In fact, at one point I deliberately inserted an incorrect path to see what would happen. The program popped up with a warning message and died. Therefore, I am sure the program is running, but not working as desired. I really do appreciate your help. Since I do not have a very good understanding of Perl, I am not able to correct the problem. |
Subject:
Re: Script to sort directory and move first file to new location
From: bozo99-ga on 12 Nov 2005 12:03 PST |
The script as it currently is constructs a filename using TODAY'S DATE in the order month,day,year and appends ".txt" to it. I was not expecting any of the files to be managed to end in ".htm" as that didn't appear in the question (unless you expected that interpretation of the word "etc"). Given the file listing you show I expect that script to first rename a file on the 14th of November. This new version tries both ".txt" and ".htm" (but not in the same execution) and also reports failure showing what file it was looking for. #!/usr/bin/perl -w # change to the directory we need # (Obviously put that proper name in place of the one here.) chdir("/tmp/ciao") or die("could not change directory"); # Discover the date in the expected format. # (Check these fields are in the order you want.) # ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = (); ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime($^T); $mon += 1; $year -= 100; $fname=sprintf("%02d-%02d-%02d.txt", $mon, $mday, $year); # Rename that TXT file - if it exists. # if (-f $fname) { printf("Renaming %s\n", $fname); rename($fname,"2bSent"); exit(0); } $fname=sprintf("%02d-%02d-%02d.htm", $mon, $mday, $year); # Rename that HTM file - if it exists. # if (-f $fname) { printf("Renaming %s\n", $fname); rename($fname,"2bSent"); exit(0); } printf("File %s not found (tried both .txt and .htm extensions)\n", $fname); exit(0); |
Subject:
Re: Script to sort directory and move first file to new location
From: pigskinreferee-ga on 12 Nov 2005 14:01 PST |
It took me a few minutes to figure out how your script worked, but I finally figured it out. I am assuming that it gets the present date, and then looks for a file with that date format with either an 'htm' or a 'txt' extension. I was not actually expecting the program to be that precise, however that is even better than I had asked for. Now I do not have to worry about synchronizing the files with the actual date, or accidentally running it twice in succession. This program will ignore any old files, which is excellent. No doubt you are aware that it is not possible to insert 'use strict;' into the program. It will fail to compile. Not a big deal for me though. I just needed a simple program that would make life a little easier for me. Anyway, I guess you came up with the answer. I am not sure how you go about being paid since this is the first time I have used this service. I do plan to use it again in the future though. Thank You! |
Subject:
Re: Script to sort directory and move first file to new location
From: bozo99-ga on 12 Nov 2005 16:44 PST |
There's nothing to pay (except the half dollar to google) only the approved researchers (with names in blue clickable links) can give offical answers. "not possible to insert 'use strict;'" Well you could if you added a package name to all the variables (except for the time variable $^T) e.g. change $sec to $'sec etc throughout the script. I tend to think that's not worth the effort. |
If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you. |
Search Google Answers for |
Google Home - Answers FAQ - Terms of Service - Privacy Policy |