Checking for presence of any files


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting Checking for presence of any files
# 1  
Old 03-05-2009
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
# 2  
Old 03-05-2009

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

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

This User Gave Thanks to cfajohnson For This Post:
# 3  
Old 03-05-2009
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.
# 4  
Old 03-05-2009
Quote:
Originally Posted by phudgens
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?

Right.

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

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

This User Gave Thanks to cfajohnson For This Post:
# 5  
Old 03-05-2009
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.
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Checking for the absence of files

Hi All, I am trying to put a condition to check if two required files are not present in a particular directory and alert accordingly. cd /root F1=sample1 F2=sample2 if ] then echo "One of the files is not present" else echo "Both the files are present" fi Though both F1 and... (7 Replies)
Discussion started by: swasid
7 Replies

2. Shell Programming and Scripting

Identifying presence and name of new file(s)?

I have an HP-UX server that runs a script each night. The script connects to an SFTP server and downloads all xml files (if any are present) from a certain folder, and then deletes the files from the SFTP server. So sometimes it will download a new file, sometimes it will download 2 or 3 new... (4 Replies)
Discussion started by: lupin..the..3rd
4 Replies

3. Shell Programming and Scripting

Checking in a directory how many files are present and basing on that merge all the files

Hi, My requirement is,there is a directory location like: :camp/current/ In this location there can be different flat files that are generated in a single day with same header and the data will be different, differentiated by timestamp, so i need to verify how many files are generated... (10 Replies)
Discussion started by: srikanth_sagi
10 Replies

4. Shell Programming and Scripting

checking co-presence of Var. - Shell or Perl

Hey fellas, I've posted this problem a few days back and I received just one post which was in PHP that I have no idea about! (Thanks to DGPickett) It would be so nice if you can help me with this in Shell or Perl. Here is the story: I have a big table with variables and observations. I... (9 Replies)
Discussion started by: @man
9 Replies

5. Shell Programming and Scripting

Checking dir and files!

Hi Guys, I wrote a code which checks for the text and directory. If the path is incorrect than it should echo that particular path not found. I can create a different if statments but I want to do this in just one if statment with the use of "or" (||). I have succssfully executed this code but now... (2 Replies)
Discussion started by: dixits
2 Replies

6. Shell Programming and Scripting

Cross checking two files

i have two files which contains some fields separated by columns as 03/29/1999 08:48:12 02 172.16.114.50 03/29/1999 09:08:00 480 172.16.112.100 Both of the files do have the same format I want the script which will take two such... (3 Replies)
Discussion started by: vaibhavkorde
3 Replies

7. Shell Programming and Scripting

Checking for presence of any files

Is there code in Cshell scripting to check for the presence of any files in the current directory (and only the current directory)? I tried: if (-r *) then ... but Cshell doesn't like that. Thanks, Paul Hudgens (0 Replies)
Discussion started by: phudgens
0 Replies

8. Shell Programming and Scripting

Checking for the presence of a string within another string

How to check if a string in contained in another string ? Like Whether the String "brown" is contained in "A quick brown fox jumps over a lazy the dog" (1 Reply)
Discussion started by: hidnana
1 Replies

9. Shell Programming and Scripting

checking entries between files

I need to write a script for: I have two files where I need to check entries and sort of compare: file1: data01 data02 data03 data04 data05 . . . data81 file2: /vol/vx/data01 /vol/vx/data02 /vol/vx/data03 /vol/vx/data04 /vol/vx/data05 . (8 Replies)
Discussion started by: za_7565
8 Replies

10. Shell Programming and Scripting

testing for presence of files in ftp

I have a shell script which GETS a file, but I only want to run this script if $FILE exists on the remote host, don't know correct syntax.....please help ftp -n $HOST <<END_SCRIPT quote USER $USER quote PASS $PASSWD bin get $FILE rename export.prn export.prn.$TIMESTAMP !cat... (0 Replies)
Discussion started by: walterja
0 Replies
Login or Register to Ask a Question