Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

or statement and directory empty statements

Status
Not open for further replies.

Guest_imported

New member
Jan 1, 1970
0
0
0
Hi, i'm quite new to perl and have two quick questions:

1. What's the correct term for the statement below?

if ($dirname eq "bass_tabs" OR $dirname eq "drum_tabs" )

I thought that would work but it doesn't seem to.

2. I've also tried looking for a statement which says if directory empty (no files or other directorys in it) then continue else error.

If you could help me with either of the above, i'd be greatful. Thanx
 
You might want to chomp($dirname); before testing, it could be that a trailing "\n" new line is botching up your match.

This is untested, but this might work:

unless(</path/to/dir/*>){
print &quot;This dir is empty...\n&quot;;
}
else{
print &quot;It's not empty...\n&quot;;
}

Good luck.

--jim
 
Try these:

[tt]
if (($dirname eq &quot;bass_tabs&quot;) || ($dirname eq &quot;drum_tabs&quot;))
[/tt]

For your directory check use !-s.
Meaning File or directory exists and has nonzero size, so you need the opposite as my example shows. =================
Bad Company Music
=================
 
Badco:
Even directories that are empty don't have a size of zero. So that won't work for what he's trying.

punkkid:
I tested my method above. It does work.
 
Hi do you know what's wrong with the coding at the bottom then. Say for the following file structure:

home/html/bandname/bass_tabs
home/html/bandname/drum_tabs
home/html/bandname/guitar_tabs
home/html/bandname/lyrics

$dizin = &quot;home/html/bandname&quot; & $FORM{'sec'}&quot;; would equal &quot;bass_tabs&quot;.

Basically if they I to delete a directory named bass_tabs, drum_tabs, guitar_tabs or lyrics it gives an error (which works fine). But I try to delete the bandname directory then:

$dizin = &quot;home/html&quot; & $FORM{'sec'}&quot;; would equal &quot;bandname&quot;.

What it does then is checks to see if there's any files in the any of the directories. If not it deletes all of the directories. But I seem to get the error &quot;There's still some tabs in the bass_tabs directory.&quot; when there's no files or directories in that directory. I'd be greatful if you can help. Thanx

Here's the coding:

$dirname = &quot;$FORM{'sec'}&quot;;

if ($dirname eq &quot;bass_tabs&quot;) { &mesaj(&quot;ERROR&quot;,&quot;You're not allowed to delete this directory.&quot;); }
if ($dirname eq &quot;drum_tabs&quot;) { &mesaj(&quot;ERROR&quot;,&quot;You're not allowed to delete this directory.&quot;); }
if ($dirname eq &quot;guitar_tabs&quot;) { &mesaj(&quot;ERROR&quot;,&quot;You're not allowed to delete this directory.&quot;); }
if ($dirname eq &quot;lyrics&quot;) { &mesaj(&quot;ERROR&quot;,&quot;You're not allowed to delete this directory.&quot;); }

$dizin_temp = &quot;$dizin/$dirname&quot;;

unless(<$dizin_temp/bass_tabs/*>) { &mesaj(&quot;ERROR&quot;,&quot;There's still some tabs in the bass_tabs directory.&quot;); }
unless(<$dizin_temp/drum_tabs/*>) { &mesaj(&quot;ERROR&quot;,&quot;There's still some tabs in the drum tabs directory.&quot;); }
unless(<$dizin_temp/guitar_tabs/*>) { &mesaj(&quot;ERROR&quot;,&quot;There's still some tabs in the guitar_tabs directory.&quot;); }
unless(<$dizin_temp/lyrics/*>) { &mesaj(&quot;ERROR&quot;,&quot;There's still some tabs in the lyrics directory.&quot;); }

rmdir($dizin_temp/bass_tabs) || &mesaj(&quot;ERROR&quot;,&quot;Directory could not be deleted. Please check for the directory permission and be sure that there is no file in the directory.&quot;);
rmdir($dizin_temp/drum_tabs) || &mesaj(&quot;ERROR&quot;,&quot;Directory could not be deleted. Please check for the directory permission and be sure that there is no file in the directory.&quot;);
rmdir($dizin_temp/guitar_tabs) || &mesaj(&quot;ERROR&quot;,&quot;Directory could not be deleted. Please check for the directory permission and be sure that there is no file in the directory.&quot;);
rmdir($dizin_temp/lyrics) || &mesaj(&quot;ERROR&quot;,&quot;Directory could not be deleted. Please check for the directory permission and be sure that there is no file in the directory.&quot;);
rmdir($dizin_temp) || &mesaj(&quot;ERROR&quot;,&quot;Directory could not be deleted. Please check for the directory permission and be sure that there is no file in the directory.&quot;);
 
What are you trying to do with lines like these:
Code:
$dizin = &quot;home/html&quot; & $FORM{'sec'}&quot;; would equal &quot;bandname&quot;.
$dizin = &quot;home/html/bandname&quot; & $FORM{'sec'}&quot;; would equal &quot;bass_tabs&quot;.
???
If you are attempting to concatenate - then use the '.' and not the '&', that is a bitwise anding operator - I really don't think it's what you want in this case.

And as far as these lines:
Code:
unless(<$dizin_temp/bass_tabs/*>) { &mesaj(&quot;ERROR&quot;,&quot;There's still some tabs in the bass_tabs directory.&quot;); }
unless(<$dizin_temp/drum_tabs/*>) { &mesaj(&quot;ERROR&quot;,&quot;There's still some tabs in the drum tabs directory.&quot;); }
unless(<$dizin_temp/guitar_tabs/*>) { &mesaj(&quot;ERROR&quot;,&quot;There's still some tabs in the guitar_tabs directory.&quot;); }
unless(<$dizin_temp/lyrics/*>) { &mesaj(&quot;ERROR&quot;,&quot;There's still some tabs in the lyrics directory.&quot;); }
Change the 'unless' to 'if' on each line. That will correct the logic.

--jim

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top