Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > Perl to move files by specific size

Reply
Thread Tools

Perl to move files by specific size

 
 
amar_auluck@hotmail.com
Guest
Posts: n/a
 
      09-16-2005
Hi,

I am new to perl and want to create a script which would move all the
files from folder_1 to folder_2 with size 4,104KB in size.
Folder_1 gets updated with new files of the same size of 4,104KB and at
times a file comes in with a less size 3,100KB then 4,104KB in size.
When this happens I would like to stop the move of the files until I
receive a new good file of 4,104KB in size. And once the good file size
4,104KB is in the folder_1 the script should move the 3,100KB size
file along with the 4,104KB size file.

If any one have any suggestions please help me out.

Thanks.

 
Reply With Quote
 
 
 
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-16-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote in news:1126831610.765562.40210
@g49g2000cwa.googlegroups.com:

> I am new to perl and want to create a script which would move all the
> files from folder_1 to folder_2 with size 4,104KB in size.
> Folder_1 gets updated with new files of the same size of 4,104KB and at
> times a file comes in with a less size 3,100KB then 4,104KB in size.
> When this happens I would like to stop the move of the files until I
> receive a new good file of 4,104KB in size. And once the good file size
> 4,104KB is in the folder_1 the script should move the 3,100KB size
> file along with the 4,104KB size file.


What have you tried?

perldoc -f stat

Please read the posting guidelines.

Sinan

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
 
 
 
Sherm Pendley
Guest
Posts: n/a
 
      09-16-2005
(E-Mail Removed) writes:

> I am new to perl and want to create a script which would move all the
> files from folder_1 to folder_2 with size 4,104KB in size.


Here's a few hints to help get you started.

Use the File::Find module to "walk" down a directory tree and visit each
file. Use the -s file test operator to get a file's size.

See:
perldoc -f -X
perldoc File::Find

Or, if you're not interested in drilling down into subdirectories, you
could use opendir(), readdir(), and closedir():

perldoc -f opendir
perldoc -f readdir
perldoc -f closedir

The File::Copy module is useful for copying or moving files around.

perldoc File::Copy

Give it a try first. If you run into trouble come back and post the code
you've tried, and explain what you expected it to do and what it's doing
instead.

Oh, and if you haven't done so already, please read the posting guide-
lines for this group.

sherm--

--
Cocoa programming in Perl: http://camelbones.sourceforge.net
Hire me! My resume: http://www.dot-app.org
 
Reply With Quote
 
amar_auluck@hotmail.com
Guest
Posts: n/a
 
      09-16-2005
Well, I have created scripts to move file by dates but not by size and
this one is a bit triky. I can create script to move all the files but
the script I am looking for is to stop moving if there is a file found
which is less then 4,104KB in size.

Here is the script which moves index file by date and I need similar
script which woul move all the files and stop moving when a file
arrives in the folder_1 folder with a size less then 4_104KB

use strict ;
use POSIX ;
use File::Copy ;
use File::Basename ;

chdir "C:\\folder_1"
or die "Can't chdir to C:\\folder_1 $!\n" ;

for my $file (<active.VsIdx>)
{ my $datestamp = strftime("%Y.%m.%d.",localtime) ;
my ($name,$path,$suffix) = fileparse($file,"\active.VsIdx") ;

move $file,"C:\\folder_1\\$datestamp$name$suffix"
or warn "Cannot copy $file $!\n" ;
}

 
Reply With Quote
 
A. Sinan Unur
Guest
Posts: n/a
 
      09-16-2005
(E-Mail Removed) wrote in news:1126833803.239767.287970
@g49g2000cwa.googlegroups.com:

[ please quote an appropriate amount of context ]

> Well, I have created scripts to move file by dates but not by size and
> this one is a bit triky. I can create script to move all the files but
> the script I am looking for is to stop moving if there is a file found
> which is less then 4,104KB in size.


Well, you have received a couple of responses. Why don't you try reading
them, and trying to write something instead of trying to get others to
write it for you?

> Here is the script which moves index file by date and I need similar
> script which woul move all the files and stop moving when a file
> arrives in the folder_1 folder with a size less then 4_104KB
>
> use strict ;


use warnings;

> use POSIX ;
> use File::Copy ;
> use File::Basename ;
>
> chdir "C:\\folder_1"
> or die "Can't chdir to C:\\folder_1 $!\n" ;


my $src = $ARGV[0] || 'c:/folder_1';
chdir $src or die "Can't chdir to $src: $!";

opendir my $src_dir, '.' or die "Cannot open dir: $src: $!";

> for my $file (<active.VsIdx>)


I don't get this. How can there be more than one file returned by this
glob pattern?

Also, you probably want scalar context here:

while(my $file = <some.pattern>) {

}

Are you sure you want csh semantics?

I tend to use readdir rather than globs. But that's probably because I
am too lazy to read the docs.

use strict ;
use warnings;

my $src = shift() || 'c:/folder_1';
my $dest = shift() || 'c:/folder_2';

chdir $src or die "Can't chdir to $src: $!";
opendir my $src_dir, '.' or die "Cannot open dir: $src: $!";

while(my $file = readdir $src_dir) {
next unless /\.jpg$/;
# check the size
# move it wherever you want
}
__END__

--
A. Sinan Unur <(E-Mail Removed)>
(reverse each component and remove .invalid for email address)

comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html
 
Reply With Quote
 
Anno Siegel
Guest
Posts: n/a
 
      09-16-2005
<(E-Mail Removed)> wrote in comp.lang.perl.misc:
> Well, I have created scripts to move file by dates but not by size and
> this one is a bit triky. I can create script to move all the files but
> the script I am looking for is to stop moving if there is a file found
> which is less then 4,104KB in size.
>
> Here is the script which moves index file by date and I need similar
> script which woul move all the files and stop moving when a file
> arrives in the folder_1 folder with a size less then 4_104KB


There is a problem with that specification. If files are written
directly in the directory you are watching, *every* file will start out
at less than 4_104KB. Your program will "stop moving" each time a new
file is created. You will need a way of telling when a file is complete
before you check its size.

Anno
--
If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers.
 
Reply With Quote
 
The Guru
Guest
Posts: n/a
 
      09-22-2005
I have writed the script but having difficulties in stoping the move if
the file size is less then 4104 mb in size

my $Filename = "/.log";

# Get file details
if(-f $Filename)
{
my @Stats = stat($Filename);
my $FileSize = $Stats[7];

# Check if file is less then 4104 MB
if($FileSize = 4104000000)
{
move $Filename, "c:\Folder_2\" ;

} elsif($FileSize < 4104000000)

}

Anno Siegel wrote:
> <(E-Mail Removed)> wrote in comp.lang.perl.misc:
> > Well, I have created scripts to move file by dates but not by size and
> > this one is a bit triky. I can create script to move all the files but
> > the script I am looking for is to stop moving if there is a file found
> > which is less then 4,104KB in size.
> >
> > Here is the script which moves index file by date and I need similar
> > script which woul move all the files and stop moving when a file
> > arrives in the folder_1 folder with a size less then 4_104KB

>
> There is a problem with that specification. If files are written
> directly in the directory you are watching, *every* file will start out
> at less than 4_104KB. Your program will "stop moving" each time a new
> file is created. You will need a way of telling when a file is complete
> before you check its size.
>
> Anno
> --
> If you want to post a followup via groups.google.com, don't use
> the broken "Reply" link at the bottom of the article. Click on
> "show options" at the top of the article, then click on the
> "Reply" at the bottom of the article headers.


 
Reply With Quote
 
ekkehard.horner
Guest
Posts: n/a
 
      09-22-2005
The Guru wrote:
> I have writed the script but having difficulties in stoping the move if
> the file size is less then 4104 mb in size
>
> my $Filename = "/.log";
>
> # Get file details
> if(-f $Filename)
> {
> my @Stats = stat($Filename);
> my $FileSize = $Stats[7];
>
> # Check if file is less then 4104 MB
> if($FileSize = 4104000000)


if($FileSize == 4104000000)

> {
> move $Filename, "c:\Folder_2\" ;
>
> } elsif($FileSize < 4104000000)
>
> }
>
> Anno Siegel wrote:
>
>><(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>
>>>Well, I have created scripts to move file by dates but not by size and
>>>this one is a bit triky. I can create script to move all the files but
>>>the script I am looking for is to stop moving if there is a file found
>>>which is less then 4,104KB in size.
>>>
>>>Here is the script which moves index file by date and I need similar
>>>script which woul move all the files and stop moving when a file
>>>arrives in the folder_1 folder with a size less then 4_104KB

>>
>>There is a problem with that specification. If files are written
>>directly in the directory you are watching, *every* file will start out
>>at less than 4_104KB. Your program will "stop moving" each time a new
>>file is created. You will need a way of telling when a file is complete
>>before you check its size.
>>
>>Anno
>>--
>>If you want to post a followup via groups.google.com, don't use
>>the broken "Reply" link at the bottom of the article. Click on
>>"show options" at the top of the article, then click on the
>>"Reply" at the bottom of the article headers.

>
>

 
Reply With Quote
 
The Guru
Guest
Posts: n/a
 
      09-22-2005
I wrote the below script which will move the test.log file if it's
equal to
4202496. But the directory the script is watching contains multipule
type of file names and this script only moves the file for one file.
How can I make it check by the extension .log and not by the file name.
Thank you.

#!/usr/bin/perl -w # force taint checks, and print warnings
use strict; # install all three strictures
$|++; # force auto flush of output buffer

use strict ;
use POSIX ;
use File::Copy ;
use File::Basename ;
use File::stat;


my $Filename = "test.log";

if(-f $Filename)
{
my $sb = stat($Filename);
my $FileSize = $sb->size;
print $FileSize ;

# Check if file is less then 4104 MB
if($FileSize == 4202496)
{
##printf("\nFilename : %s File Size %s\n", $Filename, $FileSize);
move $Filename,"c:\\Folder_2\\";
}
}

ekkehard.horner wrote:
> The Guru wrote:
> > I have writed the script but having difficulties in stoping the move if
> > the file size is less then 4104 mb in size
> >
> > my $Filename = "/.log";
> >
> > # Get file details
> > if(-f $Filename)
> > {
> > my @Stats = stat($Filename);
> > my $FileSize = $Stats[7];
> >
> > # Check if file is less then 4104 MB
> > if($FileSize = 4104000000)

>
> if($FileSize == 4104000000)
>
> > {
> > move $Filename, "c:\Folder_2\" ;
> >
> > } elsif($FileSize < 4104000000)
> >
> > }
> >
> > Anno Siegel wrote:
> >
> >><(E-Mail Removed)> wrote in comp.lang.perl.misc:
> >>
> >>>Well, I have created scripts to move file by dates but not by size and
> >>>this one is a bit triky. I can create script to move all the files but
> >>>the script I am looking for is to stop moving if there is a file found
> >>>which is less then 4,104KB in size.
> >>>
> >>>Here is the script which moves index file by date and I need similar
> >>>script which woul move all the files and stop moving when a file
> >>>arrives in the folder_1 folder with a size less then 4_104KB
> >>
> >>There is a problem with that specification. If files are written
> >>directly in the directory you are watching, *every* file will start out
> >>at less than 4_104KB. Your program will "stop moving" each time a new
> >>file is created. You will need a way of telling when a file is complete
> >>before you check its size.
> >>
> >>Anno
> >>--
> >>If you want to post a followup via groups.google.com, don't use
> >>the broken "Reply" link at the bottom of the article. Click on
> >>"show options" at the top of the article, then click on the
> >>"Reply" at the bottom of the article headers.

> >
> >


 
Reply With Quote
 
Jeff
Guest
Posts: n/a
 
      09-22-2005
The Guru wrote:
> I wrote the below script which will move the test.log file if it's
> equal to
> 4202496. But the directory the script is watching contains multipule
> type of file names and this script only moves the file for one file.
> How can I make it check by the extension .log and not by the file name.


Language barrier maybe...I don't understand exactly what you mean here.

> Thank you.
>
> #!/usr/bin/perl -w # force taint checks, and print warnings


No, -T forces taint checks

> use strict; # install all three strictures
> $|++; # force auto flush of output buffer
>
> use strict ;


Again? You're *very* strict

> use POSIX ;


Why? I don't see you using anything from there.

> use File::Copy ;
> use File::Basename ;


Again, I don't see any of these functions being used

> use File::stat;
>
>
> my $Filename = "test.log";
>
> if(-f $Filename)
> {
> my $sb = stat($Filename);
> my $FileSize = $sb->size;
> print $FileSize ;
>
> # Check if file is less then 4104 MB
> if($FileSize == 4202496)
> {
> ##printf("\nFilename : %s File Size %s\n", $Filename, $FileSize);
> move $Filename,"c:\\Folder_2\\";
> }
> }
>
> ekkehard.horner wrote:
>
>>The Guru wrote:
>>
>>>I have writed the script but having difficulties in stoping the move if
>>>the file size is less then 4104 mb in size
>>>
>>>my $Filename = "/.log";
>>>
>>># Get file details
>>>if(-f $Filename)
>>>{
>>> my @Stats = stat($Filename);
>>> my $FileSize = $Stats[7];
>>>
>>> # Check if file is less then 4104 MB
>>> if($FileSize = 4104000000)

>>
>> if($FileSize == 4104000000)
>>
>>
>>> {
>>> move $Filename, "c:\Folder_2\" ;
>>>
>>> } elsif($FileSize < 4104000000)
>>>
>>>}
>>>
>>>Anno Siegel wrote:
>>>
>>>
>>>><(E-Mail Removed)> wrote in comp.lang.perl.misc:
>>>>
>>>>
>>>>>Well, I have created scripts to move file by dates but not by size and
>>>>>this one is a bit triky. I can create script to move all the files but
>>>>>the script I am looking for is to stop moving if there is a file found
>>>>>which is less then 4,104KB in size.
>>>>>
>>>>>Here is the script which moves index file by date and I need similar
>>>>>script which woul move all the files and stop moving when a file
>>>>>arrives in the folder_1 folder with a size less then 4_104KB
>>>>
>>>>There is a problem with that specification. If files are written
>>>>directly in the directory you are watching, *every* file will start out
>>>>at less than 4_104KB. Your program will "stop moving" each time a new
>>>>file is created. You will need a way of telling when a file is complete
>>>>before you check its size.
>>>>
>>>>Anno
>>>>--
>>>>If you want to post a followup via groups.google.com, don't use
>>>>the broken "Reply" link at the bottom of the article. Click on
>>>>"show options" at the top of the article, then click on the
>>>>"Reply" at the bottom of the article headers.
>>>
>>>

>

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Writing move constructors and move assignment Andrew Tomazos C++ 2 12-12-2011 01:45 PM
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
I am trying to move spaces to a weblistbox and when I move them... Eduardo78 ASP .Net Web Controls 0 11-03-2005 06:06 PM
redirect traffic on specific ip to specific interface mimiseh Cisco 3 06-05-2005 09:14 PM



Advertisments