Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: filter the files in a folder on the basis of some variable present in them

by Ratazong (Monsignor)
on Jun 10, 2015 at 08:16 UTC ( [id://1129817]=note: print w/replies, xml ) Need Help??


in reply to filter the files in a folder on the basis of some variable present in them

Hi reciter!

The code below shows how I handle checks to be applied on more than one file:

  • I use File::Find to loop through all files
  • With the sub checkFile I do some basic checking on each of the files (e.g. if it is empty)
  • With sub analyze I open a relevant file and process it; if you use the same approach, you'll move most of your code into that function
HTH, Rata

use File::Find; use File::stat; find(\&checkFile, $path); # loop through all files in my directory #--------------------------------- check to be applied on each file -- +----------- sub checkFile { my $fullfilename = $File::Find::name; my $filesize = stat($File::Find::name)->size; my $filename = $_; # --- check for relevance based on expected filenames if ((! ($filename =~ /_obj_pp_comp.txt/ )) && (! ($filename =~ /ob +j.doc/ ))) { return; } if ($filesize == 0) { print ("<!-- \t\tERROR: file $fullfilename is empty. -->\n"); + return; } if (! -f $filename) { print ("<!-- \t\tERROR: file $fullfilename is a directory. -->\ +n"); return; } analyze($fullfilename); } #--------------------------------- work to be done on each relevant fi +le ------------- sub analyze { ... }

Replies are listed 'Best First'.
Re^2: filter the files in a folder on the basis of some variable present in them
by reciter (Novice) on Jun 10, 2015 at 10:32 UTC

    Hi Ratazong, I am a novice to perl thats why I do so much blunder. I tried another way to filter files. Instead using x2 z2 and all as variable I tried to search them as pattern. but it also having problem can you please check and tell what can I do so it may start working.

    #usr/bin/perl use strict; use warnings; use File::Find; use File::stat; my $path='e:\testfolder'; find(\&checkFile, $path); # loop through all files in my directory #--------------------------------- check to be applied on each file -- +----------- sub checkFile { my $fullfilename = $File::Find::name; my $filesize = stat($File::Find::name)->size; my $filename = $_; # --- check for relevance based on expected filenames if ((! ($filename =~ /_obj_pp_comp.txt/ )) && (! ($filename =~ /ob +j.doc/ ))) { return; } if ($filesize == 0) { print ("<!-- \t\tERROR: file $fullfilename is empty. -->\n"); + return; } if (! -f $filename) { print ("<!-- \t\tERROR: file $fullfilename is a directory. -->\ +n"); return; } analyze($fullfilename); } #--------------------------------- work to be done on each relevant fi +le ------------- sub analyze { my @files = $fullfilename; my $pat='/^x2=0.[6-9][0-9][0-9][0-9][0-9]\n'; my $pat1='/^z2=0.[6-9][0-9][0-9][0-9][0-9]\n'; my $pat3='/^some_t2=0.[6-9][0-9][0-9][0-9][0-9]\n'; foreach my $file (@files) { foreach my $file1(@files) { foreach my $files2(@files) { if (($file=~/^$pat/) && ($file1=~/^$pat1/) && ($file2= +~/^$pat3/)) { print "$file IS VALID\n"; system 'mv $file E:\test\some' } else print "not relevant\n" } } } }

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1129817]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-18 02:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found