Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Deleting Directories from a path by reading information from Database columns

by Anonymous Monk
on Dec 28, 2010 at 20:09 UTC ( #879507=perlquestion: print w/ replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, According to my logic I need to accomplish in this script(Logic: My perl script should check for a certain column(column name STATUS) in a table(table name INFO). If the status column is equal to 'I OR D' it has to look at the data in the" ID ,number column", remember that ID and number and then go to the location "c:\Documents and Settings\user\Files and delete the directories that are named by this ID.).

I was able to connect to oracle and select the required columns and print them, but need help from here. How to remember the data in the "ID " column and go to the location " c:\Documents and Settings\user\Files" and delete these directories.

Current Script: #!/usr/perl/5.8.8/bin/perl -w use strict; use DBI; my $db = DBI->connect( "dbi:Oracle:*****", "*****", "******" ) || die( $DBI::errstr . "\n" ); $db->{AutoCommit} = 0; $db->{RaiseError} = 1; $db->{ora_check_sql} = 0; $db->{RowCacheSize} = 16; my $SEL = "SELECT number,id,status FROM info where status in ('I,D')" +; my $sth = $db->prepare($SEL); $sth->execute(); while ( my @row = $sth->fetchrow_array() ) { foreach (@row) { $_ = "\t" if !defined($_); print "$_\t"; } print "\n"; } END { $db->disconnect if defined($db); } current output: number Id status HDF0008 89456R I HDF0009 34567R D Files: c:\Documents and Settings\user\Files HDF0009 -> 34567R -> folder1 , folder2 .... HDF0008 -> 89456R -> folder1 , folder2.... DIR -> Subdirectory -> older1 , folder2....

Comment on Deleting Directories from a path by reading information from Database columns
Download Code
Re: Deleting Directories from a path by reading information from Database columns
by Generoso (Vicar) on Dec 28, 2010 at 20:27 UTC

    It is not clear what is the relationship between “HDF0009 -> 34567R” and the name of the files you need to delete. Please clarify.

      Hi,

      HDF0009 -> 34567R



      HDF0009->is the top level directory



      34567R -> is the sub directory in HDF0009( and this 34567R again consists of some sub directories)



      so HDF0009 may have other directories(like 12347R, 56789R...)in addition to 34567R. But here we are trying to delete 34567R because this directory contains wrong data.

        Have a look at this Node http://www.perlmonks.org/?node_id=29734

Re: Deleting Directories from a path by reading information from Database columns
by Anonymous Monk on Dec 28, 2010 at 23:13 UTC
    use File::Path 'remove_tree'; remove_tree('path/to/directory');

    It's not clear if this is all you are asking.

Re: Deleting Directories from a path by reading information from Database columns
by tokpela (Chaplain) on Dec 28, 2010 at 23:23 UTC

    Are you looking for a way to delete the directories using the Id directory using the "number" column and base directory (c:\Documents and Settings\user\Files)?

    If so, then check out remove_tree in the File::Path module.

    remove_tree( $dir1, $dir2, ...., \%opts )

    The remove_tree function deletes the given directories and any files and subdirectories they might contain, much like the Unix command rm -r or del /s on Windows.

    use strict; use warnings; use DBI; use File::Path; my $base_directory = 'c:/Documents and Settings/user/Files'; my $db = DBI->connect( "dbi:Oracle:*****", "*****", "*** +***" ) || die( $DBI::errstr . "\n" ); $db->{AutoCommit} = 0; $db->{RaiseError} = 1; $db->{ora_check_sql} = 0; $db->{RowCacheSize} = 16; my $SEL = "SELECT number,id,status FROM info where statu +s in ('I,D')"; my $sth = $db->prepare($SEL); $sth->execute(); while ( my $row = $sth->fetchrow_hashref() ) { my $folder = $row->{number}; # folder my $subfolder = $row->{Id}; # subfolder my $directory = "$base_directory/$folder/$subfolder"; if (-d $directory) { if (remove_tree($directory)) { print "DIRECTORY HAS BEEN DELETED:: [$directory]\n"; } else { print "[Error] UNABLE TO DELETE DIRECTORY: [$directory]\n"; } } } ...

      Hi, I tried your code and it is showing the following errors:
      Use of uninitialized value in concatenation (.) or string at ./db0.pl line 29.

      Use of uninitialized value in concatenation (.) or string at ./db0.pl line 29.

      Undefined subroutine &main::remove_tree called at ./db0.pl line 35.

      line 29 is : my $directory = "$base_directory/$folder/$subfolder";

        You need to import remove_tree, as I said in an earlier post.

        And can you please do the most rudimentary of debugging to see what is undefined? Obviously your data doesn't look like you think it does.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://879507]
Approved by Corion
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others cooling their heels in the Monastery: (4)
As of 2014-09-22 00:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (176 votes), past polls