Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 757349
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T15:21:06+00:00 2026-05-14T15:21:06+00:00

I’m using JSch to get files from an SFTP server, but I’m trying to

  • 0

I’m using JSch to get files from an SFTP server, but I’m trying to figure out a way to only get the oldest file, and to make sure that it is not currently being written to. The way I imagine myself doing this is first finding which file in the specified remote folder is oldest. I would then check the file size, wait x seconds (probably about 10, just to be safe) and then check it again. If the file size has not changed, I download the file and process it. However, I have no idea how to do this! If anybody knows how to do this, or knows of something else that supports SFTP that has this built-in (I know Apache Commons does, but only does FTPS), it would be greatly appreciated.

Thanks in advance.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T15:21:07+00:00Added an answer on May 14, 2026 at 3:21 pm

    Turns out that this is entirely possible in JSch, the hardest part is simply finding the documentation. Code I used is below, hopefully somebody else will find it helpful! (I’m sure there are optimizations to be made, I know, I know. There are also variables that are defined elsewhere, but hopefully anybody that needs this will be able to figure them out!)

    public static String oldestFile() {
        Vector list = null;
        int currentOldestTime;
        int nextTime = 2140000000; //Made very big for future-proofing
        ChannelSftp.LsEntry lsEntry = null;
        SftpATTRS attrs = null;
        String nextName = null;
        try {
            list = Main.chanSftp.ls("*.xml");
            if (list.isEmpty()) {
                fileFound = false;
            }
            else {
                lsEntry = (ChannelSftp.LsEntry) list.firstElement();
                oldestFile = lsEntry.getFilename();
                attrs = lsEntry.getAttrs();
                currentOldestTime = attrs.getMTime();
                for (Object sftpFile : list) {
                    lsEntry = (ChannelSftp.LsEntry) sftpFile;
                    nextName = lsEntry.getFilename();
                    attrs = lsEntry.getAttrs();
                    nextTime = attrs.getMTime();
                    if (nextTime < currentOldestTime) {
                        oldestFile = nextName;
                        currentOldestTime = nextTime;
                    }
                }
                attrs = chanSftp.lstat(Main.oldestFile);
                long size1 = attrs.getSize();
                System.out.println("-Ensuring file is not being written to (waiting 1 minute)");
                Thread.sleep(60000); //Wait a minute to make sure the file size isn't changing
                attrs = chanSftp.lstat(Main.oldestFile);
                long size2 = attrs.getSize();
                if (size1 == size2) {
                    System.out.println("-It isn't.");
                    fileFound = true;
                }
                else {
                    System.out.println("-It is.");
                    fileFound = false;
                }
            }
        } catch (Exception ex) {ex.printStackTrace();}
        return Main.oldestFile;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
That's pretty much it. I'm using Nokogiri to scrape a web page what has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
I am reading a book about Javascript and jQuery and using one of the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.