getDirectory

getDirectory

Hello, this is my little , “get all files and folders” in a particular directory path sub routine. Personally I place this in a separate required file and just call it when I need it. I like to place little scripts like this in a folder called /library/ and call subs as needed.

Going into this sub you need to populate $openPath with the full system path to the directory you wish to retrieve the directory content. You may use $skip_folder_? (1-4) and $skip_file_? (1-4) with either files or folders you want to ignore bringing back in the results.

Results are in @folderSize, @folderName, @fileSize & @fileName. If the directory was not found $pathError will come back with a ‘1′ or as in ‘true’.

For example:

[ccb lang='perl' theme='coryinline' ]if ($pathError){ &exitErrorNotice;}[/ccb]

Files and folders come back alpha sorted automatically. If you would like to see the file sizes please refer to my getFileSize sub routine.

[cc lang="perl"]
sub getDirectory {

chdir();
chdir($openPath);
$pathFound=eval(opendir(DIR, $openPath));
foreach (sort readdir(DIR)) {
next unless (-d $_);
next if (m!^\.\.?$!);
next if ($_ eq $skip_folder_1 or
$_ eq $skip_folder_2 or
$_ eq $skip_folder_3 or
$_ eq $skip_folder_4);
push(@folderSize, -s $_);
push(@folderName, $_);
}
rewinddir(DIR);
foreach (sort readdir(DIR)) {
next unless (-f $_);
next if (m!^\.\.?$!);
next if ($_ eq $skip_file_1 or
$_ eq $skip_file_2 or
$_ eq $skip_file_3 or
$_ eq $skip_file_4);
push(@fileSize, -s $_);
push(@fileName, $_);
}
closedir(DIR);

if (!$pathFound){$pathError = 1;}

}
[/cc]

About the Author

Programmer