CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Help with a Script that will rename a file when it's not longer being written to

 



dpman39
Novice

Feb 18, 2016, 9:01 AM

Post #1 of 12 (2172 views)
Help with a Script that will rename a file when it's not longer being written to Can't Post

Gurus,

I need a script that will rename a file when it's not longer being written to.

Step 1: Check and make sure the file isn't being written to.
Step 2: Once file isn't being written to , rename all files in /opt/LAS/tranfer/export/.* to .complete

Any help would be greatly appreciated.


Code
#!/usr/bin/perl 

my $dir='/opt/LAS/transfer/export';

open FILE , "ls $dir |" || die "unable to list contents of $dir:$!\n";

while <FILE> {
chmop();
print "processing $dir/$_\n";
if ( localtime() - stat($dir/$_)[9] >= 90 ) {
#file has it been modified in 90 seconds
rename "$dir/$_", "$dir/$_.complete";
}

}
close(FILE);


I receive syntax errors when I execute in a shell script. What am I doing wrong?

syntax error at ./mod_export.sh line 7, near "while <FILE>"
syntax error at ./mod_export.sh line 10, near ")["
Execution of ./mod_export.sh aborted due to compilation errors.


FishMonger
Veteran / Moderator

Feb 18, 2016, 9:57 AM

Post #2 of 12 (2170 views)
Re: [dpman39] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

I saw this Q of yours on EE and was working on a response, but I've been tied up on work issues. That script which arnold gave you, besides the syntax errors, is very poorly written and I'd suggest dropping it.

I'm still tied up on work issues, but will work up something when I can, provided someone else doesn't respond first.


(This post was edited by FishMonger on Feb 18, 2016, 9:59 AM)


dpman39
Novice

Feb 18, 2016, 10:50 AM

Post #3 of 12 (2163 views)
Re: [FishMonger] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

Thanks a lot FishMonger!


FishMonger
Veteran / Moderator

Feb 18, 2016, 1:43 PM

Post #4 of 12 (2150 views)
Re: [dpman39] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

Do you need to verify the write status of each file before renaming it, or only a single file needs to be checked before renaming all files?

Do you want them moved to a new directory in addition to renaming?


(This post was edited by FishMonger on Feb 18, 2016, 1:44 PM)


dpman39
Novice

Feb 18, 2016, 1:45 PM

Post #5 of 12 (2146 views)
Re: [FishMonger] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

I need to verify the write status of each file. Only need the files renamed not moved.


FishMonger
Veteran / Moderator

Feb 18, 2016, 2:22 PM

Post #6 of 12 (2140 views)
Re: [dpman39] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

This is untested so it may need a slight tweak and can be extended if needed.

Code
#!/usr/bin/perl 

use warnings;
use strict;
use File::Basename;

my $dir = '/opt/LAS/transfer/export';

LOOP: while (1) {
my @files = grep(! /\.complete$/, <$dir/*>);
@files or do {
print "All files processed...done\n";
last LOOP;
};

foreach my $file (@files) {
if ( localtime() - (stat($file))[9] >= 90 ) {
my $new_name = "$dir/" . basename($file) . '.complete';
print "renaming $file to $new_name\n";
rename $file, $new_name;
}
}
sleep 90;
}



(This post was edited by FishMonger on Feb 18, 2016, 2:24 PM)


dpman39
Novice

Feb 18, 2016, 2:39 PM

Post #7 of 12 (2137 views)
Re: [FishMonger] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

Thanks FishMonger!

There seems to be a problem with this line


Code
      if ( localtime() - (stat($file))[9] >= 90 ) {


Error:
Argument "Thu Feb 18 17:33:11 2016" isn't numeric in subtraction (-)


FishMonger
Veteran / Moderator

Feb 18, 2016, 5:25 PM

Post #8 of 12 (2129 views)
Re: [dpman39] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

Try this (untested):

Code
if ( time() - (stat($file))[9] >= 90 ) {


If that doesn't produce the desired result, I'll need to run a test.


FishMonger
Veteran / Moderator

Feb 18, 2016, 5:37 PM

Post #9 of 12 (2128 views)
Re: [dpman39] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

The reason you received that error is because the localtime function will return 2 different values depending on context. If it's called in list context, it will return a list of 12 values and we would need to do an array slice like we're doing on the stat function in order to extract the desired time value. If called in scalar context, like we did in that conditional, it will return a formatted date string.

The time() function returns an integer.


FishMonger
Veteran / Moderator

Feb 19, 2016, 6:35 AM

Post #10 of 12 (2099 views)
Re: [dpman39] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

After thinking about it, I should make a couple adjustments.

1) The use of the File::Basename module is not needed and should be removed.
2) The 90 second value we're using probably should be reduced.
3) Error handling should be added to the rename statement.

I will make those adjustments and post it in the EE thread.


dpman39
Novice

Feb 19, 2016, 7:52 AM

Post #11 of 12 (2093 views)
Re: [FishMonger] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

Thank you so much for your help!


FishMonger
Veteran / Moderator

Feb 19, 2016, 8:00 AM

Post #12 of 12 (2091 views)
Re: [dpman39] Help with a Script that will rename a file when it's not longer being written to [In reply to] Can't Post

You're welcome

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives