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

How can i refresh a directory while in a script. Is there any command or built in functionalities for refreshing a directory? eg: If in a script, i am deleting a file from the directory using unlink function,in the next line i have to refresh the directory.

Replies are listed 'Best First'.
Re: How to Refresh a directory using PERL?
by johngg (Canon) on Nov 16, 2007 at 16:02 UTC
    You can use rewinddir. Given a directory dirA containing fileA to fileE the script below goes through the directory until it reaches and unlinks dirA/fileB then does a rewinddir then shows directory contents again. Note that fileB no longer appears.

    use strict; use warnings; my $dir = q{dirA}; opendir my $dh, $dir or die qq{opendir: $dir: $!\n}; while ( my $file = readdir $dh ) { if ( $file eq q{fileB} ) { print qq{unlinking $dir/$file ... }; unlink qq{$dir/$file} or die qq{unlink: $dir/$file: $!\n}; print qq{done\n}; last; } else { print qq{$dir/$file\n}; } } print qq{+++++++++++\n}; rewinddir $dh; while ( my $file = readdir $dh ) { print qq{$dir/$file\n}; }

    Here's the output.

    dirA/. dirA/.. dirA/fileA unlinking dirA/fileB ... done +++++++++++ dirA/. dirA/.. dirA/fileA dirA/fileC dirA/fileD dirA/fileE

    I hope this is of use.

    Cheers,

    JohnGG

Re: How to Refresh a directory using PERL?
by jettero (Monsignor) on Nov 16, 2007 at 14:57 UTC
    You probably have to tell IE (or the file explorer, or whatever) to do that? Otherwise I'm not even sure what you mean. I doubt there's a way to do it besides maybe something like Win32::GUIRobot.

    -Paul

Re: How to Refresh a directory using PERL?
by cengineer (Pilgrim) on Nov 16, 2007 at 15:42 UTC
    Re-read the contents of the directory