Home > Enterprise >  FTP stops working at consistent, but unexplained intervals? (Matlab)
FTP stops working at consistent, but unexplained intervals? (Matlab)

Time:01-18

The Problem

I'm using the (simplified) code below to get information about a repo I have on box.com.

The script should:

  1. access my remote repo via FTP
  2. get a list of all the directories in the repo
  3. iterate through each directory in the list, where in each iteration it: 3a) enters the directory and gets some information about the files there 3b) stores the information in an array 3c) goes back to the root with "../"

I found the following works for about half the directories in my repo (70 of 134):

ftpobj = ftp("ftp.box.com","myUname","myPassword","TLSMode","strict");

% Get dir list
dirList = dir(ftpobj);
numDirs =  size(dirList,1);

% Setup out Array 
clearvars outArray
outArray = ["directory" "numFiles"]; 

for i = 1:numDirs
    % Select and Move to subfolder
    folder = dirList(i,1).name; 
    cd(ftpobj, folder);
    
    % Get a filelist for dir
    files = dir(ftpobj);
    numFiles = size(files,1);
 
    % Determine Output and add to OutArray
    outLine = [folder numFiles];
    outArray = [outArray; outLine];

    disp(i   " of "   numDirs   " done");
    cd(ftpobj, "../"); % Move back to Root
end 

But it drops out around halfway through on the cd(ftpobj,"../"); line, saying:

"ftp://ftp.box.com//2021-11-04/../" is nonexistent or not a directory.

I've Tried:

If I run the cd(ftpobj,"../"); command again in the terminal, it works fine. I can manually increase i and go by step by step again no problem - just not in the loop.

I've tried adding in a try catch over the whole loop, but it still stops working when it gets past 2021-11-04 (dir 71)!

I tried adding in an if statement to the code which skips out the problem directory (i==71), and it instead just tripped up on the next one (dir 72). I then tried changing the for statement to for i = 72:numDirs, without clearing the workspace, and it tripped out again.

A possible solution? I cleared the workspace completely and ran the whole code again but with for i = 72:numDirs and it carried on perfectly to the end. So I guess I can run this loop in two halves but that seems hacky?

Could it be a bandwidth limit/ structure limit or something? I haven't been able to find anything about that though?

CodePudding user response:

I managed to find a reasonable work-around. All I've done is reset the ftpobj every 30 queries by putting the following in my loop. It's gone through 4 repos and analysed over 700 directories so far without issue so while a bit weird (and possibly slower?) I think it's a viable solution!

if mod(i,30) == 0 % reset the ftp every 30
            disp("resetting FTP object at "  i);
            ftpobj = ftp("ftp.box.com","myUname","myPassword","TLSMode","strict");
end
  •  Tags:  
  • Related