Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

deleting all files in adirectory

thlwarethlware Member Posts: 13
Hi All
I am trying to delete all files This directory(c/:delet), first i list all my files just to comfirm indeed files are available on the specified directory.The problem is there is no error reported but the files that i'm atempting to delete still exists.Any suggestions welcomed....I have attched my code and a sample of one of the file..
"SMS Impi.20030404.memLog.log"
#code start
$dir = "c:/delet";
opendir(GATE,$dir) || die "delet not found $dir: $!";
foreach $name (sort readdir(GATE)){ # go through the list..
print "$name
"; # prints ., .., system.ini, and so on
unlink<*.log>
}
closedir(GATE);


Chz

Comments

  • JonathanJonathan Member Posts: 2,914
    Hi,

    Not entirely sure, but might you have to:-

    chdir $dir;

    Before entering the loop, so unlink is looking in the right folder. I think now it would look in the one the script is it... I'm pretty certain that it isn't bound to the currently opened directory.

    Hope this helps,

    Jonathan




    ###
    ('Is Perl Just Another Hack?' =~ /^w+ (w+) (w+) (w+)(er) (w+)?$/) and print "$2 $3$4 $1 $5$4";
    ###
    http://www.incrahost.com/ :: Great Hosting!
    ###

  • thlwarethlware Member Posts: 13
    [b][red]This message was edited by thlware at 2003-5-5 1:27:23[/red][/b][hr]
    Thanks Jonathan..
    Sorry I have been out in the office for a while...anyways i am stuck with coding again... here is my story.

    I need to delete files that are older than today meaning the last 24hrs,
    I wanted to use system time and date,but when i type time or date on the command promt the system gives me current time and ask me to promt a new one so i dont think this will work , or do i have to use regular expressions ? So i decided to ask you guys.
    Here is the file(This file are on the same directory) format bellow...

    SMS Umphathi.20030505.memLog.log
    SMS Umphathi.20030504.memLog.log
    SMS Umphathi.20030503.memLog.log
    SMS Umphathi.20030502.memLog.log
    SMS NKosi.20030505.memLog.log
    SMS NKosi.20030503.memLog.log

    i.e i only want to delet all above files except the one's dated today
    SMS NKosi.20030505.memLog.log
    SMS Umphathi.20030505.memLog.log

    Any help will be appreciated
    thanks in advance
    Chz
    Tlware



    : Hi,
    :
    : Not entirely sure, but might you have to:-
    :
    : chdir $dir;
    :
    : Before entering the loop, so unlink is looking in the right folder. I think now it would look in the one the script is it... I'm pretty certain that it isn't bound to the currently opened directory.
    :
    : Hope this helps,
    :
    : Jonathan
    :
    :
    :
    :
    : ###
    : ('Is Perl Just Another Hack?' =~ /^w+ (w+) (w+) (w+)(er) (w+)?$/) and print "$2 $3$4 $1 $5$4";
    : ###
    : http://www.incrahost.com/ :: Great Hosting!
    : ###
    :
    :



  • JonathanJonathan Member Posts: 2,914
    : I need to delete files that are older than today meaning the last 24hrs,
    : I wanted to use system time and date,but when i type time or date on
    : the command promt the system gives me current time and ask me to
    : promt a new one so i dont think this will work , or do i have to use
    : regular expressions ? So i decided to ask you guys.
    : Here is the file(This file are on the same directory) format
    : below...
    I suggest that you get the time/date like this:-

    [code]my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());[/code]

    You can then assemble today's date.

    [code]$year += 1900;
    $mon++;
    my $today = $year . ($mon > 9 ? $mon : "0$mon") . ($mday > 9 ? $mday : "0$mday");[/code]

    Then for each filename (if the filename is in $filename) you'd do this:-

    [code]unlink $filename if ($filename !~ /$today/);[/code]

    Hope this helps,

    Jonathan

    ###
    # Example Of Perl 6 Syntax.
    push @will, my Power $button;
    my $hardware is Useless but Valuable;
    do ($nothing) while $i.work and print $stuff;
    push (@will, my Off $button) and die "with me";

  • thlwarethlware Member Posts: 13
    #!/bin/perl
    $dir = "D:\play\today\";
    $filename = "*.log";
    #$mypath = "$dir$filename";
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtim
    (time());

    $today = localtime(time());

    print "$today
    ";

    $year += 1900;
    $mon++;
    my $today = $year . ($mon > 9 ? $mon : "0$mon") . ($mday > 9 ? $mday : "0$mday");

    #unlink $filename if ($filename !~ /$today/);

    opendir (DIR, $dir) or die "Can't open directory $dir
    ";
    if ($filename !~ /$today/){
    $cmd = ("del /Q $filename");
    system $cmd;
    }else{
    print "All files :$filename are dated:$today
    ";
    }
    close(DIR);

    Sorry guys i will be brief this program is not working the way i expected it to.
    When i copy and paste files to D:/play/today/ is suppose not to delete those files becuase they will be all dated today but to my suprise it does delete the files dated today..when i say today i mean this date 05/05/2003 on the program printed above..any suggestions are welcomed..
    chz
    Guys










    : [b][red]This message was edited by thlware at 2003-5-5 1:27:23[/red][/b][hr]
    : Thanks Jonathan..
    : Sorry I have been out in the office for a while...anyways i am stuck with coding again... here is my story.
    :
    : I need to delete files that are older than today meaning the last 24hrs,
    : I wanted to use system time and date,but when i type time or date on the command promt the system gives me current time and ask me to promt a new one so i dont think this will work , or do i have to use regular expressions ? So i decided to ask you guys.
    : Here is the file(This file are on the same directory) format bellow...
    :
    : SMS Umphathi.20030505.memLog.log
    : SMS Umphathi.20030504.memLog.log
    : SMS Umphathi.20030503.memLog.log
    : SMS Umphathi.20030502.memLog.log
    : SMS NKosi.20030505.memLog.log
    : SMS NKosi.20030503.memLog.log
    :
    : i.e i only want to delet all above files except the one's dated today
    : SMS NKosi.20030505.memLog.log
    : SMS Umphathi.20030505.memLog.log
    :
    : Any help will be appreciated
    : thanks in advance
    : Chz
    : Tlware
    :
    :
    :
    : : Hi,
    : :
    : : Not entirely sure, but might you have to:-
    : :
    : : chdir $dir;
    : :
    : : Before entering the loop, so unlink is looking in the right folder. I think now it would look in the one the script is it... I'm pretty certain that it isn't bound to the currently opened directory.
    : :
    : : Hope this helps,
    : :
    : : Jonathan
    : :
    : :
    : :
    : :
    : : ###
    : : ('Is Perl Just Another Hack?' =~ /^w+ (w+) (w+) (w+)(er) (w+)?$/) and print "$2 $3$4 $1 $5$4";
    : : ###
    : : http://www.incrahost.com/ :: Great Hosting!
    : : ###
    : :
    : :
    :
    :
    :
    :

  • JonathanJonathan Member Posts: 2,914
    : $dir = "D:\play\today\";
    : $filename = "*.log";
    : #$mypath = "$dir$filename";
    : my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtim
    : (time());
    :
    : $today = localtime(time());
    :
    : print "$today
    ";
    :
    : $year += 1900;
    : $mon++;
    : my $today = $year . ($mon > 9 ? $mon : "0$mon") . ($mday > 9 ? $mday : "0$mday");
    :
    : #unlink $filename if ($filename !~ /$today/);
    :
    : opendir (DIR, $dir) or die "Can't open directory $dir
    ";
    : if ($filename !~ /$today/){
    : $cmd = ("del /Q $filename");
    : system $cmd;
    : }else{
    : print "All files :$filename are dated:$today
    ";
    : }
    : close(DIR);
    :
    : Sorry guys i will be brief this program is not working the way i
    : expected it to.
    : When i copy and paste files to D:/play/today/ is suppose not to
    : delete those files becuase they will be all dated today but to my
    : suprise it does delete the files dated today..when i say today i
    : mean this date 05/05/2003 on the program printed above..any
    : suggestions are welcomed..

    Yeah, your code is way off. It's hardly a surprise that it deletes all files if you run this at the shell:-

    del /Q *.log

    Which is what you are doing.

    Something more like this is what you want:-

    [code]#Work out today's date as it will appear in the filename.
    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
    $year += 1900;
    $mon++;
    my $today = $year . ($mon > 9 ? $mon : "0$mon") . ($mday > 9 ? $mday : "0$mday");

    #Open directory.
    my $dir = "D:\play\today\";
    chdir $dir;
    opendir (DIR, $dir) or die "Can't open directory $dir
    ";

    #Loop through and delete files.
    while (readdir(DIR)) {
    unlink if ($_ !~ /$today/);
    }

    #Close dir.
    closedir DIR;
    [/code]

    Try something along those lines.

    Jonathan


    ###
    # Example Of Perl 6 Syntax.
    push @will, my Power $button;
    my $hardware is Useless but Valuable;
    do ($nothing) while $i.work and print $stuff;
    push (@will, my Off $button) and die "with me";

  • thlwarethlware Member Posts: 13
    Thanks Jonathan !
    Much appreciated..
    chz


    : : $dir = "D:\play\today\";
    : : $filename = "*.log";
    : : #$mypath = "$dir$filename";
    : : my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtim
    : : (time());
    : :
    : : $today = localtime(time());
    : :
    : : print "$today
    ";
    : :
    : : $year += 1900;
    : : $mon++;
    : : my $today = $year . ($mon > 9 ? $mon : "0$mon") . ($mday > 9 ? $mday : "0$mday");
    : :
    : : #unlink $filename if ($filename !~ /$today/);
    : :
    : : opendir (DIR, $dir) or die "Can't open directory $dir
    ";
    : : if ($filename !~ /$today/){
    : : $cmd = ("del /Q $filename");
    : : system $cmd;
    : : }else{
    : : print "All files :$filename are dated:$today
    ";
    : : }
    : : close(DIR);
    : :
    : : Sorry guys i will be brief this program is not working the way i
    : : expected it to.
    : : When i copy and paste files to D:/play/today/ is suppose not to
    : : delete those files becuase they will be all dated today but to my
    : : suprise it does delete the files dated today..when i say today i
    : : mean this date 05/05/2003 on the program printed above..any
    : : suggestions are welcomed..
    :
    : Yeah, your code is way off. It's hardly a surprise that it deletes all files if you run this at the shell:-
    :
    : del /Q *.log
    :
    : Which is what you are doing.
    :
    : Something more like this is what you want:-
    :
    : [code]#Work out today's date as it will appear in the filename.
    : my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime(time());
    : $year += 1900;
    : $mon++;
    : my $today = $year . ($mon > 9 ? $mon : "0$mon") . ($mday > 9 ? $mday : "0$mday");
    :
    : #Open directory.
    : my $dir = "D:\play\today\";
    : chdir $dir;
    : opendir (DIR, $dir) or die "Can't open directory $dir
    ";
    :
    : #Loop through and delete files.
    : while (readdir(DIR)) {
    : unlink if ($_ !~ /$today/);
    : }
    :
    : #Close dir.
    : closedir DIR;
    : [/code]
    :
    : Try something along those lines.
    :
    : Jonathan
    :
    :
    : ###
    : # Example Of Perl 6 Syntax.
    : push @will, my Power $button;
    : my $hardware is Useless but Valuable;
    : do ($nothing) while $i.work and print $stuff;
    : push (@will, my Off $button) and die "with me";
    :
    :

Sign In or Register to comment.