Need idea for doing automatic iteration, please.
- From: patrikh@xxxxxxxxxxxxxxxxxxxxx (Patrik Hasibuan)
- Date: Fri, 29 Jun 2007 02:55:19 +0700
Dear my friends...
I want my code does an action if it find a directory or file, namely: storing the url/path of a file or a directory onto mysql database.
I am meaning, my code should look up every file/directory name resides under the "$rootdir" iteratively for doing storing onto mysql on each find.
The algorithm For doing find from beginning to the end of the 1 level under $rootdir is still simple, it's only a "while{..}{...}".
But the problem comes when the directory has a/some directory/-es whereas my code should also find them and stores them onto the mysql.
If every directory has a limited level (for instance maximum only 3 level subdirectories below to the bottom) then all I have to do is simply creating nested "while(...)[..] " for 3 level. But in this case of course each directory may unpredictably has hundreds or thousands subdirectories/files in hundreds or thousands levels below to the bottom.
Please suggest me some ideas where I can implement into my codes in order to enable my code to find all subdirectories and files where placed under $rootdir.
A very simple code-sample is very......very.......welcomed.
Here is my current code under below. This code still only can find 1 level, only exactly 1 level under $rootdir.
---------------------
package iterdir;
use kueri;
sub baru{
my $kelas = shift if @_;
print "Nama superkelas: $kelas \n";
return( bless{} );
}
sub bukadir{
my $kelas = shift;
$rootdir="/home/patrikh/sementara/tes";
opendir("dirku", "$rootdir");
$statusp=chdir($rootdir);
if ($statusp){
print "berhasil membuka direktori-->$rootdir\n";
while ($entridir=readdir("dirku")){
print "entridir: $entridir\n";
$sqlku=kueri->baru;
$sqlku->konek;
if (($entridir ne '.') and ($entridir ne '..')){
my $strsql = "insert into tblarsip (location) values ('".$entridir."')";
print "strsql: $strsql\n";
$sth=$kueri::dbh->prepare($strsql);
$sth->execute;
$sth->finish;
}
}
} else{
print "gagal membuka direktori yang diinginkan: $rootdir\n";
exit 1;
}
}
1;
--
Patrik Hasibuan <patrikh@xxxxxxxxxxxxxxxxxxxxx>
Junior Programmer
.
- Follow-Ups:
- RE: Need idea for doing automatic iteration, please.
- From: Wagner, David --- Senior Programmer Analyst --- WGO
- RE: Need idea for doing automatic iteration, please.
- Prev by Date: Adding a line in a file inside many directories
- Next by Date: RE: Need idea for doing automatic iteration, please.
- Previous by thread: Adding a line in a file inside many directories
- Next by thread: RE: Need idea for doing automatic iteration, please.
- Index(es):
Relevant Pages
|