Test if directory is not empty
Is there a simple test to determine if a directory is empty or not?
Or do you have to opendir, readdir, or something similar with a full
block.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Test if directory is not empty
Harry Putnam wrote:
> Is there a simple test to determine if a directory is empty or not?
>
> Or do you have to opendir, readdir, or something similar with a full
> block.
>
>
That depends on what you mean by empty. On Linux, even empty
directories have . and ..
Why do you want to test for this? If all you want to do is remove
directories, use rmtree() from File::Path. It will remove the
directory, all its files and sub-directories.
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Test if directory is not empty
Shawn H Corey <shawnhcorey [at] gmail.com> writes:
Harry Putnam wrote:
>> Is there a simple test to determine if a directory is empty or not?
>>
>> Or do you have to opendir, readdir, or something similar with a full
>> block.
>>
>>
>
> That depends on what you mean by empty. On Linux, even empty
> directories have . and ..
No actual files
> Why do you want to test for this? If all you want to do is remove
> directories, use rmtree() from File::Path. It will remove the
> directory, all its files and sub-directories.
I'm writing a script that searches files under a hierarchy for
specific regex.
If the regex is found, these files are then symlinked into another
directory.
I want to know if the receiving directory is empty of actual files.
Since if it is not; then first the user must be asked if she wants to
continue, if so, then a subfunction must be employed to ensure nothing
is clobbered.
The subfunction compares incoming filenames to any filenames
present in the receiving directory and adds a numeric extension if
there are any matches; to guarantee a unique filename.
That code is a little more involved since some of the filenames present
may well already have extensions
But all I'm asking here is for the easiest way to see if the receiving
directory is empty.
The code I've devised seems pretty clunky (I haven't tested the script
yet since I'm writing several other parts right now, so not even sure
it works).
I suspected that `find' would find at least (.) and possibly (..) like
long `ls -l' does... what I saw is that it finds only (.) if the dir
is empty.
So I was asking how to do that test in case there is a better way.
------- --------- ---=--- --------- --------
[...]
use strict;
use warnings;
use FIle::Find;
[...]
my [at] dircontent;
find(
sub {
## Collect anything that is not a single dot (.)
## For later comparision to files found in search
if(! /^\.$/) {push [at] dircontent , $_;
}
}, $def_dir
);
## if `dircontent' is 0, it means only (.) was found and we
## can skip this
if( [at] dircontent > 0){
print " Default directory <$def_dir> is not empty
if we proceed, any clashing filenames will be re-written
adding a numeric extension to make the name uniq so nothing
gets overwritten.
proceed [y/n] > ";
my $ans = <STDIN>;
if ($ans !~ /^y$/){
print "Aborting by user request . . .\n";
exit;
}
}
[...]
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Test if directory is not empty
>>>>> "HP" == Harry Putnam <reader [at] newsguy.com> writes:
HP> But all I'm asking here is for the easiest way to see if the receiving
HP> directory is empty.
HP> The code I've devised seems pretty clunky (I haven't tested the script
HP> yet since I'm writing several other parts right now, so not even sure
HP> it works).
HP> I suspected that `find' would find at least (.) and possibly (..) like
HP> long `ls -l' does... what I saw is that it finds only (.) if the dir
HP> is empty.
way overkill.
HP> So I was asking how to do that test in case there is a better way.
HP> ------- --------- ---=--- --------- --------
HP> [...]
HP> use strict;
HP> use warnings;
HP> use FIle::Find;
spelling. case matters at least on unix when loading a file. this means
you are on winblows where it doesn't matter. but it will matter even
then when you import stuff as the package name will not be the same.
use File::Slurp::read_dir. it will return a list of files in a dir and
it filters out . and .. for you. so a test for an empty dir is simple:
if ( my [at] files = read_dir( $dir ) ) {
# process [at] files
# remember to prefix $dir/ to the file names
}
else {
# empty dir
}
uri
--
Uri Guttman ------ uri [at] stemsystems.com -------- http://www.sysarch.com --
----- Perl Code Review , Architecture, Development, Training, Support ------
--------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Test if directory is not empty
--000e0cd70d14241892048139b96c
Content-Type: text/plain; charset=ISO-8859-1
On Sun, Mar 7, 2010 at 11:37 AM, Harry Putnam <reader [at] newsguy.com> wrote:
> I want to know if the receiving directory is empty of actual files.
> Since if it is not; then first the user must be asked if she wants to
> continue, if so, then a subfunction must be employed to ensure nothing
> is clobbered.
>
> The subfunction compares incoming filenames to any filenames
> present in the receiving directory and adds a numeric extension if
> there are any matches; to guarantee a unique filename.
> That code is a little more involved since some of the filenames present
> may well already have extensions
>
The "-e" test will tell you if the target file already exists. For example:
if (-e $copyToFile) {
# Add extension here...
}
--
Robert Wohlfarth
--000e0cd70d14241892048139b96c--
Re: Test if directory is not empty
Robert Wohlfarth <rbwohlfarth [at] gmail.com> writes:
> The "-e" test will tell you if the target file already exists. For example:
> if (-e $copyToFile) {
> # Add extension here...
> }
Nice... much better. That test would come in handy instead of comparing the
whole list of files to the current one incoming. It would find the
case where an extension was needed right off.
Still need to know if the directory is empty right up front though.
And should really print a list of whats in it, Before asking user if
they want to continue.
Its at least as likely that user would sooner clear the
directory... rather than fuss with extensions or comparisons.
I didn't mention it in first posts but this directory will be used
again and again for symlinking... in most cases whats in the directory
will not be important... Whereas whats coming in will, and so better
off being cleared so the incoming files can retain there exact names.
There may be usage where a series of searchs will be done and all
symlinks will be needed, hence the worry about unique names.
So it needs to be up to the user but most often any files in the
directory will not be of concern.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Test if directory is not empty
"Uri Guttman" <uri [at] StemSystems.com> writes:
>>>>>> "HP" == Harry Putnam <reader [at] newsguy.com> writes:
>
> HP> But all I'm asking here is for the easiest way to see if the receiving
> HP> directory is empty.
>
> HP> The code I've devised seems pretty clunky (I haven't tested the script
> HP> yet since I'm writing several other parts right now, so not even sure
> HP> it works).
>
> HP> I suspected that `find' would find at least (.) and possibly (..) like
> HP> long `ls -l' does... what I saw is that it finds only (.) if the dir
> HP> is empty.
>
> way overkill.
>
> HP> So I was asking how to do that test in case there is a better way.
> HP> ------- --------- ---=--- --------- --------
> HP> [...]
>
> HP> use strict;
> HP> use warnings;
> HP> use FIle::Find;
>
> spelling. case matters at least on unix when loading a file. this means
> you are on winblows where it doesn't matter. but it will matter even
> then when you import stuff as the package name will not be the same.
No, not windows. Gentoo linux...
Notice the elided marks showing that more code lies between... the
uppercase `I' was just a typo. I cut an pasted the function, but
just typed in the `use' things as an after thought since someone here
was sure to feel the need to tell me I needed use strict, use
warning.... hehe.
In the actual script it is correctly spelled.
> use File::Slurp::read_dir. it will return a list of files in a dir and
> it filters out . and .. for you. so a test for an empty dir is simple:
>
> if ( my [at] files = read_dir( $dir ) ) {
>
> # process [at] files
> # remember to prefix $dir/ to the file names
> }
> else {
> # empty dir
> }
Thanks, that's nice to know about. It would save a little work there.
However, apparently I don't have it installed; may get to that later.
perldoc File::Slurp
No documentation found for "File::Slurp".
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Test if directory is not empty
Harry Putnam wrote:
> So it needs to be up to the user but most often any files in the
> directory will not be of concern.
Well, if you're not worried about hidden files, you could use glob:
my $count = scalar glob( "$dir/*" );
--
Just my 0.00000002 million dollars worth,
Shawn
Programming is as much about organization and communication
as it is about coding.
I like Perl; it's the only language where you can bless your
thingy.
Eliminate software piracy: use only FLOSS.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/
Re: Test if directory is not empty
Shawn H Corey <shawnhcorey [at] gmail.com> writes:
> Harry Putnam wrote:
>> So it needs to be up to the user but most often any files in the
>> directory will not be of concern.
In fact, thinking about that.... the script should not only ask
permission, but should probably should offer to clear the directory
before making any symlinks, since that is most often what will be
desired.
> Well, if you're not worried about hidden files, you could use glob:
>
> my $count = scalar glob( "$dir/*" );
ahh.. cool. Leave File::Find out of it for that part. Certainly not
needed there.. Good plan I think.
And you are right. Hidden files aren't a problem at all. Or at least
not any more than they ever are.
None of the incoming will ever by dot files, its aimed at Mail/News
files in 1 message per file format, with numeric names only.
Thanks... and thanks to all posters on this.
I've got something from each post that will make the script better.
--
To unsubscribe, e-mail: beginners-unsubscribe [at] perl.org
For additional commands, e-mail: beginners-help [at] perl.org
http://learn.perl.org/