Verify if filename exists

Hi,

I have a variable returned from Oracle SQL Function which holds file names.

I would like to test if all the file names mentioned in the string exists in a directory. If all the files exists print "exists", even if one file does not exists print "Does not exists".

e.g.

 l_FILENAMES="file1 file2 file3 file4"

Regards,
Pointers

What have you tried?

You could:-

ls -1 $l_FILENAMES

Those that exist will be displayed and those that do no will return an error.

If you want to be a bit smarter, you can:-

for file in $l_FILENAMES
do
   if [ -f $file ]
   then
      printf "File %s exists" "$file"
   else
      printf "File %s is missing" "$file"
   fi
done

You can adjust this to not print anything but keep a note if any does not exist and then display your choice of message at the end.

I hope that this helps. Let us know how you get on and if you get stuck I'm sure we can suggest something else.

Robin

You could also try:

ls $l_FILENAMES >/dev/null 2>&1 && echo exists || echo 'Does not exist'

Since you are using Oracle anyway, Oracle has a built in package called utl_file. You can use the FGETATTR
Procedure in utl_file to check if the file exists and handle the errors if a file does not exist. You just need to
have a database directory point to where the files are.

UTL_FILE

1 Like