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 Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Nested IF statements

Status
Not open for further replies.

lpollock

MIS
Joined
Nov 4, 2003
Messages
9
Location
US
I'm really new to PERL so forgive me if this is a stupid question. I'm trying to loop through a directory and test each file for two different criteria before I print any file name matching those criteria to a txt file.

The criteria are that it cannot be a directory, and it be older than X number of days. The code I have below will catch the mod date just fine, but it only finds and excludes the first directory it encounters.

foreach (readdir(DIR)) {
$mod = -M "$osapi/$_";
if (!(-d $_)){
if ($mod >= 1) {
print LOG "$osapi/$_\n";
}
}
}

Any help would be greatly appreciated.

Lane
 
Never mind. I figured this out.

Thanks
 
lpollock,

sorted, so post your solution

--Paul
 
Instead of testing to see if the variable was not a directory

if (!(-d $_)){

I just tested to verify that it was a file.

foreach (readdir(OSDIR)) {
$mod1 = -M "$osapi/$_";
if (-f "$osapi/$_"){
if ($mod1 >= 1) {
print OSLOG "$osapi/$_\n";
}
}
}

I'm using this code to build a txt file that I can feed to winrar to archive old log files.
 
Your original code would have been okay, too, if you'd qualified the file name, i.e
if (!(-d "$osapi/$_")){
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top