Checking for presence of any files

I have tried the following script to check for the presence of any files in a folder:

if (-r *) then
goto ZipMiscFiles
else
echo ""
echo " No Miscellaneous files found. Exiting program."
echo ""
exit
endif

The -r works fine with the wildcard in combo with other letters such as:

if (-r *[Jj][Ss]) ...

It does not work with the asterisk alone. How do I check for the presence of files without using ls, which prints an error message if no files are present?

Thanks,
Paul Hudgens

is_file()
{
 [ -f "$1" ] || return 1
}

is_file ZipMiscFiles/* || {
echo ""
echo " No Miscellaneous files found. Exiting program."
echo ""
exit
}
1 Like

Is ZipMiscFiles being used as a directory name? In code previous to what I've shown above, I determine the existence of and cd into a directory called "Miscellaneous". I'm thinking in that case that your code might instead be:

is_file ./* || {

meaning that it is to look in the current directory (and only the current directory even if there are other directories beneath it). Am I right?

Thanks,
Paul H.

Right.

Here's a more robust version of the is_file function:

is_file () 
{ 
    for f in "$@"
    do
        [ -f "$f" ] && return;
    done;
    return 1
}
1 Like

Thanks very much. I'm assuming that if I wanted to narrow my search I could do something like the following:

is_file ./*.[Jj][Ss]* || {

and the current directory (and only the current directory) would be searched for files containing JS (or js) in the suffix.

THanks,
Paul H.