http://www.perlmonks.org?node_id=175367

icezero has asked for the wisdom of the Perl Monks concerning the following question:

I need to filter a Filetype. the specification of that files are the date.
For Ex. file a) XXX11072002XXX b) XXX01012002XXX
So, i only want to filter the files from the 11.7 th date.

Replies are listed 'Best First'.
Re: How can I filter a specified File Type.
by particle (Vicar) on Jun 18, 2002 at 14:38 UTC
    Is this from an array or the filesystem?

    If it's the filesystem, and it's unix, try:

    ls ???1107*
    if it's the filesystem, and it's Windows, try:
    dir ???1107*.*
    if it's an array, try:
    my @filtered_files = grep { /^...1107/ } @files;
      Hi, Below mentioned script is filter and display all files whose file type is 33188.
      #!/usr/bin/perl -w use strict; use File::stat; sub dodir { opendir(DIR,$_[0]) or die "Couldn't open $_[0] directory $!"; my $dir=$_[0]; print "\n Directory name is $dir"; if ( $dir !~ /\/$/ ) { $dir .= "/"; } my @List=readdir(DIR); closedir(DIR); splice(@List,0,2); foreach my $file (@List) { $file = $dir.$file; if( -d $file) { dodir($file); } elsif (stat($file)->mode == 33188 ) { print "\n File $file is pre +sent in $dir"; print "\n File type: ",stat($file)->mode; } } } my $dir_name="/home"; if ( -d $dir_name) { dodir($dir_name); }
      ============o/p======
      ./file_op.pl Directory name is /home Directory name is /home/abhosale Directory name is /home/abhosale/.texmf Directory name is /home/abhosale/.texmf/config Directory name is /home/abhosale/.texmf/config/web2c Directory name is /home/abhosale/.texmf/var Directory name is /home/abhosale/.texmf/var/fonts Directory name is /home/abhosale/.texmf/var/fonts/map Directory name is /home/abhosale/.texmf/var/fonts/map/dvipdfm Directory name is /home/abhosale/.texmf/var/fonts/map/dvipdfm/updmap Directory name is /home/abhosale/.texmf/var/fonts/map/dvips Directory name is /home/abhosale/.texmf/var/fonts/map/dvips/updmap Directory name is /home/abhosale/.texmf/var/fonts/map/pdftex Directory name is /home/abhosale/.texmf/var/fonts/map/pdftex/updmap Directory name is /home/abhosale/.texmf/var/web2c File /home/first is present in /home/ File type: 33188 Directory name is /home/myprog File /home/myprog/abc is present in /home/myprog/ File type: 33188 File /home/myprog/abd is present in /home/myprog/ File type: 33188 File /home/myprog/matter is present in /home/myprog/ File type: 33188 File /home/myprog/newfile is present in /home/myprog/ File type: 33188 File /home/myprog/oldfile is present in /home/myprog/ File type: 33188
      Please let me know if any changes are required in this script.
Re: How can I filter a specified File Type.
by icezero (Initiate) on Jun 18, 2002 at 14:46 UTC
    Sorry, my english isnt so good: let me trie to explain it on a other way. i have a Tool, which is reading files from a server. That server contains a lot of files , with different names like upside!! the differences in the name is the date like :11072002. now i just want to grep all filter from yesterday. maybe grep will be helpful! i will try it! thanks for Ur help

    Originally posted as a Categorized Answer.

Re: How can I filter a specified File Type.
by amitbhosale (Acolyte) on Feb 13, 2008 at 12:11 UTC
    Hi, Below mentioned script shows the file name whose file type is 33188.
    #!/usr/bin/perl -w use strict; use File::stat; sub dodir { opendir(DIR,$_[0]) or die "Couldn't open $_[0] directory $!"; my $dir=$_[0]; if ( $dir !~ /\/$/ ) { $dir .= "/"; } my @List=readdir(DIR); closedir(DIR); splice(@List,0,2); foreach my $file (@List) { $file = $dir.$file; if( -d $file) { dodir($file); } elsif (stat($file)->mode == 33188 ) { print "\n File $file is pre +sent in $dir"; print "\n File type: ",stat($file)->mode; } } } my $dir_name="/home"; if ( -d $dir_name) { dodir($dir_name); }
    ==============o/p=======
    $ ./file_op.pl Directory name is /home Directory name is /home/abhosale Directory name is /home/abhosale/.texmf Directory name is /home/abhosale/.texmf/config Directory name is /home/abhosale/.texmf/config/web2c Directory name is /home/abhosale/.texmf/var Directory name is /home/abhosale/.texmf/var/fonts Directory name is /home/abhosale/.texmf/var/fonts/map Directory name is /home/abhosale/.texmf/var/fonts/map/dvipdfm Directory name is /home/abhosale/.texmf/var/fonts/map/dvipdfm/updmap Directory name is /home/abhosale/.texmf/var/fonts/map/dvips Directory name is /home/abhosale/.texmf/var/fonts/map/dvips/updmap Directory name is /home/abhosale/.texmf/var/fonts/map/pdftex Directory name is /home/abhosale/.texmf/var/fonts/map/pdftex/updmap Directory name is /home/abhosale/.texmf/var/web2c File /home/first is present in /home/ File type: 33188 Directory name is /home/myprog File /home/myprog/abc is present in /home/myprog/ File type: 33188 File /home/myprog/abd is present in /home/myprog/ File type: 33188 File /home/myprog/matter is present in /home/myprog/ File type: 33188 File /home/myprog/newfile is present in /home/myprog/ File type: 33188 File /home/myprog/oldfile is present in /home/myprog/ File type: 33188

    Originally posted as a Categorized Answer.

Re: How can I filter a specified File Type.
by icezero (Initiate) on Jun 19, 2002 at 07:23 UTC
    Sorry, my english isnt so good. well , let me try to explain it on a other way. Ive got a tool, which read files from a server. those files have specific filenames. The differences of the filenames are the date, which is in the name included. like file a) xyz11072002.xyz file b) zyx11072002.xyz file c) xyz01012002.xyz Now i just want to filter the files, which include the date of yesterday or a other special day( 11.07.2002). All these files are saved on a extern server. And i want to filter the files bevor i save it , for ex. in an array or an any list. I hope i could explain it.. thanks for ur help greetz IceZero

    Originally posted as a Categorized Answer.