Download files with date modified attribute

0 votes
asked Feb 5, 2013 by Srinu (320 points)
edited Feb 28, 2013

HI,

I want to download files(from any remote folder structure) from remote based on last modified time attribute is this possible with FTP component?

Is this possible to read character count from any type of ms word(word 97,98 2000,......) document while downloading or after download?

Thanks,

Srinu

Applies to: Rebex FTP/SSL

9 Answers

0 votes
answered Feb 5, 2013 by Lukas Matyska (59,010 points)
edited Feb 5, 2013

Filtering based on last modified time is not directly supported, but you can do it simply using the GetItems method like shown below.

To your second question: We are not familiar with the MS Word document formats, so I cannot tell whether it is possible to read character count from the incomplete file. However, using the FTP component you are able to read data of the remote file during the download. There are two options shown:

  1. Download remote file into your stream - (implementation of the Stream.Write method can do the character count detection)

  2. Request download stream and read data manually - you are able to access data continuously when available

.

using (Ftp client = new Ftp())
{
    client.Connect(...);
    client.Login(...);

    foreach (FtpItem item in client.GetItems("/documents/*"))
    {
        // skip non-file items
        if (!item.IsFile)
            continue;

        // skip files with unknown date
        if (item.LastWriteTime.HasValue)
            continue;

        // skip files older that 7 days
        if (item.LastWriteTime < DateTime.Now.Date.AddDays(-7))
            continue;

        // download recently modified files (7 days ago)

        // 1. option - download to a stream
        client.GetFile(item.Path, stream);

        // 2. option - request download stream and read it yourself
        using (Stream down = client.GetDownloadStream(item.Path))
        {
            // read data from the stream
        }
    }

    client.Disconnect();
}
0 votes
answered Feb 13, 2013 by Srinu (320 points)
edited Feb 13, 2013

Hi Thanks For the Answer

Is there any way to know Whether Downloading item is folder or file in ftp_TransferProgress event ?

I want to show progress if folder is downloading,i want to show only if file is downloading

Regards,

Srinu

0 votes
answered Feb 13, 2013 by Lukas Matyska (59,010 points)
edited Feb 13, 2013

Please note that there are two events:

  1. TransferProgress - Raises during a file transfer or directory listing. It is not recommended to use this event, it is considered as old API (hidden in current version). There is easy way how to determine whether file transfer or directory listing is in progress.
  2. TransferProgressChanged - Recommended to use.

You can use the FtpTransferProgressChangedEventArgs.SourceItem.IsDirectory property to determine whether the current item is a directory.

You can also find the FtpTransferProgressChangedEventArgs.TransferState property useful.

0 votes
answered Feb 14, 2013 by Srinu (320 points)
edited Feb 14, 2013

Thanks for the reply

I want to move same folder structure from one remote directory to another with in the same ftp how can i achive this?

My Folder structure looks like this :

/ --> root path
/ --> A, B (directorires)

A --> 1(dir)/2(dir)/3(dir)/a.pdf
A --> 1(dir)/2(dir)/3(dir)/a1.pdf

when i downloaded this files same folder strucutre should be moved to / --> B

i don't want to loop the remote directories for every time

Regards,

Srinu

0 votes
answered Feb 14, 2013 by Tomas Knopp (58,890 points)
edited Feb 14, 2013

Please use Ftp object's Rename method to move the directory "A" into directory "B" like this:

Ftp ftp = new Ftp();
ftp.Connect(...);
ftp.Login(...);
ftp.Rename("/A", "/B/A");
ftp.Disconnect();
0 votes
answered Feb 16, 2013 by Srinu (320 points)
edited Feb 16, 2013

HI,

Source Remote Path : /test/1.tiff

Target Remote path : /Archive/2013/Feb/021613/1.tiff

Or

Source Remote Path : /test/demo/new/1.tiff

Target Remote path : /Archive/demo/new/2013/Feb/021613/1.tiff

If i am giving ftp.Rename(item.Path, Target Remote path) then its is saying No Such File or Directory(550)

Is i am doing mistake?

Regards,

Srinu

0 votes
answered Feb 18, 2013 by Tomas Knopp (58,890 points)
edited Feb 19, 2013

the Ftp object's method Rename is not capable of automatically creating directory /Archive/demo/new/2013/Feb/021613/ if it does not exist. Also if /test/demo/new/1.tiff is not found on the server the above reported exception is reported.

Please try EnhancedFileMove method below which handles these situations automatically.

Ftp ftp = new Ftp();
//Connect & Login

EnhancedFileMove(ftp, "test/1.tiff", "Archive/2013/Feb/021613", "1.tiff", '/');

ftp.Disconnect();

    private void EnhancedFileMove(Ftp ftp, string fromFilePath, string toFilePath, string toFile, char delimiter)
    {
        if(!ftp.FileExists(fromFilePath))
            throw new ApplicationException(string.Format("File {0} does not exist.", fromFilePath));

        if (!ftp.DirectoryExists(toFilePath))
        {
            CreateDirectory(ftp, toFilePath, delimiter);
        }

        ftp.Rename(fromFilePath, string.Format("{0}{1}{2}", toFilePath, delimiter, toFile));
    }

    private void CreateDirectory(Ftp ftp, string remoteDirectory, char delimiter)
    {
        if (remoteDirectory.IndexOf(delimiter) <= 0)
        {
            ftp.CreateDirectory(remoteDirectory);
        }
        else
        {
            int index = remoteDirectory.LastIndexOf(delimiter);
            string path = remoteDirectory.Substring(0, index);
            string dir = remoteDirectory.Substring(index + 1);

            //create recursively path
            CreateDirectory(ftp, path, delimiter);

            ftp.CreateDirectory(remoteDirectory);
        }
    }
commented Feb 19, 2013 by Srinu (320 points)
edited Feb 19, 2013

HI,

Thanks for the solution

Srinu

0 votes
answered Feb 28, 2013 by Srinu (320 points)
edited Feb 28, 2013

Hi,

I have a try with your solution but I am unable to create folder structure in remote.

It is going for

if (remoteDirectory.IndexOf(delimiter) <= 0)
{
   ftp.CreateDirectory("Archive/2013/Feb/021613");
}

(in which Archive is my current dir in that i want to create 2013/Feb/021613 structure)

And throwing error 550

My remote source path: /AAA/BBB/CCC
My remote backup path: /AAA/BBB/DDD

Source File Path : /AAA/BBB/CCC/XXX/YYY/ZZZ/1.pdf

Now i want to download this file and move it to backup folder(/AAA/BBB/DDD) with the filder structure of file path(/AAA/BBB/DDD/XXX/YYY/ZZZ/1.pdf)

Each time i want to check whether every folder exists in remote or not.

My folder structure changes like

source file path: /F/G/3.doc
backup Dir: /Z/Y/X/F/G/3.doc

I need to check if z extxs or not then for Y and then X and then F and so on.

Give me some idea on this.

Regards

Srinu

+1 vote
answered Feb 28, 2013 by Jan Sotola (17,310 points)
edited Feb 28, 2013

I'm afraid there is no "magic" method, which can solve your problem. If you need to move files between folders on the remote server, you should implement it using the following methods:

  • Rename(fromPath, toPath) - renames or moves a single file or a whole directory on the server.
    The target file or directory need not reside within the same folder as the source file or directory.
    However, the target path should exist, the Rename method cannot create a directory.
  • CreateDirectory(remotePath) - creates a directory on remote server.
  • FileExists(remotePath) - detects whether the specified file exists on the server.
  • DirectoryExists(remotePath) - detects whether the specified directory exists on the server.
...