ibmi-brunch-learn

Announcement

Collapse
No announcement yet.

Using QSH to see if Files Exist on Remote Server

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • Using QSH to see if Files Exist on Remote Server

    Hi All,

    Let me start by stating I'm no QSHELL expert and most of my current code has come from Googling but I have managed to create a CLLE program that use SFTP via QSH to send and retrieve files from a Windows Server. My problem is that when there are no files to retrieve I get a QSH0005 Exit code 1 message back to the program as if an error has occurred. What I want to do is check if any files exist before attempting to retrieve them. I've found examples searching the IFS and even printing messages but none that simply tells my program a Yes or No files exist. I think it might be using the FIND command but I can't work out the syntax, has anyone got an example they can share ?

    Many Thanks
    Chris

  • #2
    Seems to me that you want to know if there's an SFTP (I assume you mean OpenSSH's sftp program) subcommand that can tell if a file exists before trying to retrieve it. Not sure that this has anything to do with QShell -- at least, not directly.

    I guess it depends on what your sftp script does, exactly. But if all it does is retrieve a file -- well, you'll get an error if the file doesn't exist, so... doesn't that tell you that it didn't exist? What more are you after?

    If you need to do many different steps based on the file's existence, you could write a very short script that only tries to retrieve the file. If that fails, you know it doesn't exist, so then you could choose to run/not run another more complex script based on the result of the first (short) script.

    If what you're doing is simple, though... I don't understand why you're using sftp. Why not use scp? Seems so much easier, no need for a script, etc.

    Comment


    • #3
      Hi Scott,

      Thanks for you reply which I missed at the time as my notifications don't seem to be working.

      Lets me expand, my program is trying to retrieve multiple files named with timestamps (File_20231003_15000012345678) from a remote server and if for any reason there is a failure it will send an email. The problem is when there are no files to pick up the command 'PATH=$PATH:/QOpenSys/bin && sftp -vvv -b $SFTP_SCRIPT $SFTPUSER@SFTP_HOST' returns message QSH0005 with exit code 1 which is picked up by a MONMSG QSH0000 as an error. My FTP script simply does a CD, LCD and MGET File*. So to stop an email going out when no files exist in the folder I am trying to check beforehand.

      If the files were in the IFS​ I can simply do a CHKOUT and see if it fails don't do the processing but on the remote server I believe I need to do a "ls" or "Find" or something else to see if I need to do the "sftp" command at all. Any suggestion the simpler the better would be gratefully received.

      Regards
      Attached Files

      Comment


      • #4
        Using CHKOUT is a bad way to check if a file exists. But it's 100% irrelevant since the files are on a different computer.

        Why not use SCP to get each individual file instead of SFTP? Then if SCP fails, you know the specific file..

        Comment

        Working...
        X