Board index » delphi » How to know if TIdFTP commands fail???

How to know if TIdFTP commands fail???


2008-06-23 02:32:31 AM
delphi118
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
 
 

Re:How to know if TIdFTP commands fail???

"Bo Berglund" <XXXX@XXXXX.COM>writes
Quote
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.
Quote
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.
Quote
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.
Quote
And after changing the current dir, how can I check the
dir on the server.
I do not understand what you are asking.
Gambit
 

Re:How to know if TIdFTP commands fail???

On Sun, 22 Jun 2008 21:40:04 -0700, "Remy Lebeau \(TeamB\)"
<XXXX@XXXXX.COM>writes:
Quote

"Bo Berglund" <XXXX@XXXXX.COM>writes
news:XXXX@XXXXX.COM...

>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?
Quote
>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');
Quote

>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
 

Re:How to know if TIdFTP commands fail???

"Bo Berglund" <XXXX@XXXXX.COM>writes
Quote
So if there is no exception then the call worked to satisfaction?
Yes.
Quote
Means that I have to put in a lot of try/excepts in the code, right?
Not as many as you think.
Quote
So I can do this then:
FTP.Put('c:\projects\test.bin', '/mydir/subdir/test.bin');
Yes.
Quote
and
FTP.Get('/mydir/subdir/test.bin');
You are missing a parameter:
FTP.Get('/mydir/subdir/test.bin', 'c:\projects\test.bin');
Quote
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.
Quote
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.
Quote
But I found TIdFTP.RetrieveCurrentDir, which apparently
returns the value of the working dir on the server.
TIdFTP.RetrieveCurrentDir() issues a 'PWD' command.
Quote
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???
Yes.
Quote
If I want to check if a file exists on the server I guess
I can use the List command.
Yes.
Quote
If I use it like this:
FTP.List('myfilename');
what will happen then if the file does not exist?
It won't be included in the returned listing.
Quote
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.
Quote
or will the ListResult property merely be empty?
If no exception is raised, then yes.
Gambit