Discussion:
How to know if TIdFTP commands fail???
(too old to reply)
Bo Berglund
2008-06-22 18:32:31 UTC
Permalink
I am exploring the use of TIdFTP to handle some files on a FTP server.
I need to do the following functions:
- Check if a file exists
- Delete an existing file
- Rename an existing file
- Change the current dir on the FTP server
- Transfer a file to and from the FTP server

The TIdFTP component has the following methods that seemingly will be
useful:

procedure List(ADest: TStrings; const ASpecifier: string = ''; const
ADetails: boolean = true);

procedure Delete(const AFilename: string);

procedure Rename(const ASourceFile: string; const ADestFile: string);

procedure ChangeDir(const ADirName: string);

procedure Get(const ASourceFile: string; const ADestFile: string;
const ACanOverwrite: boolean = false; AResume: Boolean = false);
overload;

procedure Put(const ASourceFile: string; const ADestFile: string = '';
const AAppend: boolean = false); overload;

The first problem I see here that all these methods are *procedures*
so HOW can I know if the operation failed???

Also when specifying a file to operate on should it include a path or
just a simple file name in the current dir?
If a path, then how should it be formatted?

And after changing the current dir, how can I check the dir on the
server.

I am using the latest Indy10 snapshot on Delphi 7.
--
Bo Berglund
Remy Lebeau (TeamB)
2008-06-23 04:40:04 UTC
Permalink
Post by Bo Berglund
The first problem I see here that all these methods are
*procedures* so HOW can I know if the operation failed???
An exception will be raised. Most of Indy's methods in general raise an
exception on error/failure.
Post by Bo Berglund
Also when specifying a file to operate on should it include
a path or just a simple file name in the current dir?
Either way is fine. The FTP protocol supports both.
Post by Bo Berglund
If a path, then how should it be formatted?
That is server-specific. Some servers use Unix-style paths. Others use
Windows-style paths. When you change to a specific directory, the server
reports back how the full path to that directory is formatted.
Post by Bo Berglund
And after changing the current dir, how can I check the
dir on the server.
I do not understand what you are asking.


Gambit
Bo Berglund
2008-06-23 06:58:08 UTC
Permalink
On Sun, 22 Jun 2008 21:40:04 -0700, "Remy Lebeau \(TeamB\)"
Post by Remy Lebeau (TeamB)
Post by Bo Berglund
The first problem I see here that all these methods are
*procedures* so HOW can I know if the operation failed???
An exception will be raised. Most of Indy's methods in general raise an
exception on error/failure.
So if there is no exception then the call worked to satisfaction?
Means that I have to put in a lot of try/excepts in the code, right?
Post by Remy Lebeau (TeamB)
Post by Bo Berglund
Also when specifying a file to operate on should it include
a path or just a simple file name in the current dir?
Either way is fine. The FTP protocol supports both.
So I can do this then:
FTP.Put('c:\projects\test.bin', '/mydir/subdir/test.bin');
and
FTP.Get('/mydir/subdir/test.bin');
Post by Remy Lebeau (TeamB)
Post by Bo Berglund
And after changing the current dir, how can I check the
dir on the server.
I do not understand what you are asking.
I was merely trying to find a command mimicking pwd on Unix to tell me
what the FTP connection thinks is the current dir on the server after
the directory change. This was what I think I needed in order to check
that the ChangeDir actually worked so I can read/write the file..
But I found TIdFTP.RetrieveCurrentDir, which apparently returns the
value of the working dir on the server.

New:
--------
I just noted that there is a blank default for the target file:
procedure Put(const ASourceFile: string; const ADestFile: string = '';
const AAppend: boolean = false);

Does this mean that I can do this:
FTP.Put('c:\projects\test.bin');
and it will create a file 'test.bin' in the current dir on the FTP
server???

If I want to check if a file exists on the server I guess I can use
the List command. But it comes in three different versions...
If I use it like this:
FTP.List('myfilename');
what will happen then if the file does not exist? Will there be an
exception or will the ListResult property merely be empty?
--
Bo Berglund
Remy Lebeau (TeamB)
2008-06-23 17:10:33 UTC
Permalink
Post by Bo Berglund
So if there is no exception then the call worked to satisfaction?
Yes.
Post by Bo Berglund
Means that I have to put in a lot of try/excepts in the code, right?
Not as many as you think.
Post by Bo Berglund
FTP.Put('c:\projects\test.bin', '/mydir/subdir/test.bin');
Yes.
Post by Bo Berglund
and
FTP.Get('/mydir/subdir/test.bin');
You are missing a parameter:

FTP.Get('/mydir/subdir/test.bin', 'c:\projects\test.bin');
Post by Bo Berglund
I was merely trying to find a command mimicking pwd on Unix
to tell me what the FTP connection thinks is the current dir on
the server after the directory change.
Use the TIdFTP.RetrieveCurrentDir() method.
Post by Bo Berglund
This was what I think I needed in order to check that the
ChangeDir actually worked
No. Again, if ChangeDir() had failed, an exception would be raised instead.
You do, however, need to use RetrieveCurrentDir() to get the correct
formatting for the server-side path of the directory.
Post by Bo Berglund
But I found TIdFTP.RetrieveCurrentDir, which apparently
returns the value of the working dir on the server.
TIdFTP.RetrieveCurrentDir() issues a 'PWD' command.
Post by Bo Berglund
procedure Put(const ASourceFile: string; const ADestFile: string = '';
const AAppend: boolean = false);
FTP.Put('c:\projects\test.bin');
and it will create a file 'test.bin' in the current dir on the FTP
server???
Yes.
Post by Bo Berglund
If I want to check if a file exists on the server I guess
I can use the List command.
Yes.
Post by Bo Berglund
FTP.List('myfilename');
what will happen then if the file does not exist?
It won't be included in the returned listing.
Post by Bo Berglund
Will there be an exception
That depends on the particular server, as well as your Indy version. Some
servers report an error when an empty listing is generated. Some report a
success with blank data instead. In the case of an error being returned,
older versions of Indy did not check for that condition, and so an exception
would be raised.
Post by Bo Berglund
or will the ListResult property merely be empty?
If no exception is raised, then yes.


Gambit

Loading...