Operating System - Linux
1748156 Members
4361 Online
108758 Solutions
New Discussion юеВ

Re: extract similar kind of files through perl script

 
Sasirekha
Occasional Contributor

extract similar kind of files through perl script

Hi...

I need extract files and put them in respective folders depending on a config file.

That is...the config file is as below
grap=*.jpg,*.gif,*.bmp
backup=*.~*,*.tmp,*.bak
Source=*.pl,*.php,*.tcl

depending on this the script should scan all the files & subdirectories in an user given directory and create a directory in the name specified in the config file i.e created dir name is as before(leftside) of = in config file and the files scaned and that should be put in that directory is those files with the extension after = .

7 REPLIES 7
H.Merijn Brand (procura
Honored Contributor

Re: extract similar kind of files through perl script

--8<--- untested braindump follows ...
#!/opt/perl/bin/perl

use strict;
use warnings;

use Cwd;
use File::Find;
use File::Copy;

my $config_file = shift;
-f $config_file && -s _ && -r _ or die "$config_file: not a (valid/readable) config file\n";

my $pwd = getcwd;
my %config;
{ @ARGV = ($config_file);
while (<>) {
m/^[;#]/ and next; # Comment
my ($dir, $pat) = m/^\s*(\S+)\s*=\s*(.*)/ or next;
$config{$dir} = [ split m/\s*[,;]\s*/, $pat ];
}
}

foreach my $dir (keys %config) {
-d $dir or mkdir $dir, 0777;
}

find {sub {
(my $tld = $File::Find::dir) =~ s{^(?:\./)?([^/]+)(?:/.*}{};
exists $config{$tld} and return;
foreach my $dir (keys %config) {
foreach my $file (map { glob $_ } @{$config{$dir}}) {
copy $file, "$pwd/$dir";
}
}
}, ".");
-->8---

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
RAC_1
Honored Contributor

Re: extract similar kind of files through perl script

Not perl, but it may help you.

find . -depth \( -name "*.jpg" -o -name "*.gif" -o -name "*.bmp" \) -exec ll -d {} \; > grap.txt
[ -f grap.txt ] && mkdir grap
cat grap.txt | while read line
do
mv ${line} grap/.
done

Same for other types of files.
There is no substitute to HARDWORK
Ralph Grothe
Honored Contributor

Re: extract similar kind of files through perl script

I won't provide a solution
(if you wait a little you probably get a rather thorough one from procura),
just a few hints.

To recursively search directory trees import the Find::File module.
It merely reuires the definition of a small callback sub (mostly an anonymous one even suffices).
On the other hand you can use C-shell like filename globbing from Perl which the glob() function or the <> brackets provide
(though its use is generally deprecated).
Then there are opendir(), readdir(), and closedir().
To create new directories you could use the mkdir() function, and unlink() deletes files.
All are nicely documented in your installation's POD.

Madness, thy name is system administration
Ralph Grothe
Honored Contributor

Re: extract similar kind of files through perl script

Sorry for my noise,
procura already submitted while I was still searching the keys on my keyboard.
Madness, thy name is system administration
Sasirekha
Occasional Contributor

Re: extract similar kind of files through perl script

Hi ...

The code is not working I am getting the following error

"Use of uninitialized value in -f at testfti line 14.
Use of uninitialized value in concatenation (.) or string at testfti line 14.
: not a (valid/readable) config file"

& the folder "graph" is not created.once that
folder is created all the *.jpg,*.gif,*.bmp,etc files should get copied into that folder.
H.Merijn Brand (procura
Honored Contributor

Re: extract similar kind of files through perl script

As the intro said:
--8<--- untested braindump follows ...

So it was just a reflection of my thoughts.
It's up to you to complete it.

I cannot test if I don't have a config file.
My bet is that the config file looks different that your specs

I was quite defensive in my parsing, but if you do not have separators between your patterns, I cannot tell what will happen.

You could start with attaching the config file, so I could suggest some changes.
I was more or less hoping that my framework (in fact almost complete working code) would get you on the rails.

Enjoy, Have FUN! H.Merijn
Enjoy, Have FUN! H.Merijn
Sasirekha
Occasional Contributor

Re: extract similar kind of files through perl script

1.The script should get the directory entered by the user.
2.It should scan all the file's extensions in that User directory.
3.It should match those extensions with the extensions specified in the config file.
4.If match found it should create a directory in the User given directory path,with name specified for that extension in the config file and put that file in the corresponding newly created directory.

The config file contains like below
.jpg,*.gif,*.bmp,etc.. -> graphical type files
*.~*,*.tmp,*.bak,etc.. -> backup type files
*.pl,*.php,*.tcl,etc.. -> source type files
in the user entered directory.
like this ...the extensions might be added up in future for each grap,backup and source.