 |
BorlandTalk.com Borland discussion newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Wieland Scholz Guest
|
Posted: Tue Nov 22, 2005 1:31 pm Post subject: TQuery and deleting a directory |
|
|
Hi all,
I thought the job is done in some minutes but I have massive problems:
In a directory a dBase table is processed using TQuery. The directory should
be deleted later on. I cannot do it!
Here is the code
TQuery* qry = NULL;
int iRet;
AnsiString asDir = "D:\DEVELOP\BCB\TEST\TstQry\test\L72";
AnsiString asSQL = "SELECT * FROM Test.dbf";
SHFILEOPSTRUCT SHFileOpStruct;
char tmp[255];
try
{
qry = new TQuery( NULL );
qry->DatabaseName = asDir;
qry->SQL->Clear();
qry->SQL->Add( asSQL );
qry->Active = true;
// do something
}
__finally
{
qry->Active = false;
delete qry;
qry = NULL;
}
// prepare delete operation
memset( tmp, 0, sizeof(tmp) );
strcpy( tmp, asDir.c_str() );
memset( &SHFileOpStruct, 0, sizeof( SHFileOpStruct ) );
SHFileOpStruct.hwnd = Handle;
SHFileOpStruct.pFrom = tmp;
SHFileOpStruct.wFunc = FO_DELETE;
SHFileOpStruct.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR |
FOF_SILENT;
// delete dir
try
{
iRet = SHFileOperation( &SHFileOpStruct );
if( iRet != 0 )
{
iRet = GetLastError();
}
}
catch(...)
{
}
The specified cannot be deleted as soon as the query is set to active and as
long as the program runs. It seems that a handle used by the query will not
be released but I have no idea what to do here.
Any help is highly welcome.
Thank you in advance
Wieland Scholz
Roentgenanalytik GmbH, Taunusstein
GERMANY
|
|
| Back to top |
|
 |
Antonio Felix Guest
|
Posted: Wed Nov 23, 2005 8:28 am Post subject: Re: TQuery and deleting a directory |
|
|
"Wieland Scholz" <arb-ftl (AT) t-online (DOT) de> wrote:
| Quote: | ...
The specified cannot be deleted as soon as the query is set to active and as
long as the program runs. It seems that a handle used by the query will not
be released but I have no idea what to do here.
Any help is highly welcome.
|
Hi Wieland,
Do you use your own TDatabase or the Auto instantiated one?
When you use a Dataset and you don't have your own TDatabase,
one is auto created for you and it remains opened until you
close your program. I think this is the reason why you cannot
delete the directory.
Instantiate your own TDatabase and Close it before you try
to delete the Directory.
HTH
Antonio
|
|
| Back to top |
|
 |
Wieland Scholz Guest
|
Posted: Mon Nov 28, 2005 11:38 am Post subject: Re: TQuery and deleting a directory |
|
|
Hi Antonio,
this sounds logic and is probably the reason, but...
How can I link the self-created TDatabase instance with the created TQuery?
The TQuery->Database property is read-only.
Here is part of the code:
dbLocal = new TDatabase( this );
dbLocal->DatabaseName = modData.asFolderName;
qryMeasSpectra = new TQuery( this );
qryMeasSpectra->SQL->Clear();
qryMeasSpectra->SQL->Add( asQueryText );
qryMeasSpectra->Active = true;
But as soon as I use
qryMeasSpectra->DatabaseName = modData.asFolderName;
the same happens as before.
Do you have another hint for me?
Thank you in advance.
Wieland
"Antonio Felix" <nomail (AT) nm (DOT) pt> wrote:
| Quote: |
"Wieland Scholz" <arb-ftl (AT) t-online (DOT) de> wrote:
...
The specified cannot be deleted as soon as the query is set to active and as
long as the program runs. It seems that a handle used by the query will not
be released but I have no idea what to do here.
Any help is highly welcome.
Hi Wieland,
Do you use your own TDatabase or the Auto instantiated one?
When you use a Dataset and you don't have your own TDatabase,
one is auto created for you and it remains opened until you
close your program. I think this is the reason why you cannot
delete the directory.
Instantiate your own TDatabase and Close it before you try
to delete the Directory.
HTH
Antonio
|
|
|
| Back to top |
|
 |
Antonio Felix Guest
|
Posted: Mon Nov 28, 2005 2:49 pm Post subject: Re: TQuery and deleting a directory |
|
|
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
| Quote: |
Hi Antonio,
this sounds logic and is probably the reason, but...
How can I link the self-created TDatabase instance with the created TQuery?
The TQuery->Database property is read-only.
|
Yes.
Database is a read-only property that is automatically set when
the database specified by the DatabaseName property is opened.
You need to use the DataBaseName, as you already did.
| Quote: |
Here is part of the code:
dbLocal = new TDatabase( this );
dbLocal->DatabaseName = modData.asFolderName;
qryMeasSpectra = new TQuery( this );
|
Set the DataBaseName prior to the Open Call
qryMeasSpectra->DatabaseName = modData.asFolderName;
| Quote: | qryMeasSpectra->SQL->Clear();
qryMeasSpectra->SQL->Add( asQueryText );
qryMeasSpectra->Active = true;
But as soon as I use
qryMeasSpectra->DatabaseName = modData.asFolderName;
|
You cannot change the DatabaseName when your Query is Active
HTH
Antonio
|
|
| Back to top |
|
 |
Wieland Scholz Guest
|
Posted: Tue Nov 29, 2005 7:55 am Post subject: Re: TQuery and deleting a directory |
|
|
Hi Antonio,
I'm not yet done...
I've changed the code as you has suggested:
dbLocal = new TDatabase( this );
dbLocal->DatabaseName = modData.asFolderName;
qryMeasSpectra = new TQuery( this );
qryMeasSpectra->DatabaseName = modData.asFolderName;
qryMeasSpectra->SQL->Clear();
qryMeasSpectra->SQL->Add( asQueryText );
qryMeasSpectra->Active = true;
.... here comes the true job dealing with the records....
.... and later when the job is done....
if( qryMeasSpectra )
{
qryMeasSpectra->Active = false;
qryMeasSpectra->UnPrepare();
delete qryMeasSpectra;
qryMeasSpectra = NULL;
delete dbLocal;
dbLocal = NULL;
}
Creating the TDatabase instance and TQuery are of no problem. Also all other steps work well. But after
qryMeasSpectra->Active = true;
I've checked the Database property of qryMeasSpectra and I did not found the same address as I got from
dbLocal = new TDatabase( this );
so all in all the result is the same: I cannot delete the directory described by modData.asFolderName.
Sorry for the question but I don't know what to do now.
Best regards
Wieland
"Antonio Felix" <nomail (AT) nm (DOT) pt> wrote:
| Quote: |
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
Hi Antonio,
this sounds logic and is probably the reason, but...
How can I link the self-created TDatabase instance with the created TQuery?
The TQuery->Database property is read-only.
Yes.
Database is a read-only property that is automatically set when
the database specified by the DatabaseName property is opened.
You need to use the DataBaseName, as you already did.
Here is part of the code:
dbLocal = new TDatabase( this );
dbLocal->DatabaseName = modData.asFolderName;
qryMeasSpectra = new TQuery( this );
Set the DataBaseName prior to the Open Call
qryMeasSpectra->DatabaseName = modData.asFolderName;
qryMeasSpectra->SQL->Clear();
qryMeasSpectra->SQL->Add( asQueryText );
qryMeasSpectra->Active = true;
But as soon as I use
qryMeasSpectra->DatabaseName = modData.asFolderName;
You cannot change the DatabaseName when your Query is Active
HTH
Antonio
|
|
|
| Back to top |
|
 |
Antonio Felix Guest
|
Posted: Tue Nov 29, 2005 9:27 am Post subject: Re: TQuery and deleting a directory |
|
|
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
| Quote: |
Hi Antonio,
I'm not yet done...
I've changed the code as you has suggested:
dbLocal = new TDatabase( this );
dbLocal->DatabaseName = modData.asFolderName;
qryMeasSpectra = new TQuery( this );
qryMeasSpectra->DatabaseName = modData.asFolderName;
qryMeasSpectra->SQL->Clear();
qryMeasSpectra->SQL->Add( asQueryText );
qryMeasSpectra->Active = true;
... here comes the true job dealing with the records....
... and later when the job is done....
if( qryMeasSpectra )
{
qryMeasSpectra->Active = false;
qryMeasSpectra->UnPrepare();
delete qryMeasSpectra;
qryMeasSpectra = NULL;
delete dbLocal;
dbLocal = NULL;
}
Creating the TDatabase instance and TQuery are of no problem. Also all other steps work well. But after
qryMeasSpectra->Active = true;
I've checked the Database property of qryMeasSpectra and I did not found the same address as I got from
dbLocal = new TDatabase( this );
so all in all the result is the same: I cannot delete the directory described by modData.asFolderName.
Sorry for the question but I don't know what to do now.
Best regards
Wieland
|
Hi Wieland,
I've tried to replicate your error here, but everything works
as expected.
Is there any chance that you get the Session->NetFileDir
pointing to the Directory you're trying to delete?
If the Net file is created on that directory, the only way to
delete it is by closing the Session.
Try to check the TSession NetFileDir and the PrivateDir at Run Time.
HTH
Antonio
|
|
| Back to top |
|
 |
Antonio Felix Guest
|
Posted: Tue Nov 29, 2005 10:37 am Post subject: Re: TQuery and deleting a directory |
|
|
| Quote: | I've checked the Database property of qryMeasSpectra and I did not found the same address as I got from
|
That's odd you should get DbLocal == TQuery->Database always
true!
Probably you still got a Database opened.
Try to iterate the TSession Databases in order to Close;
for ( int i = 0; i < Session->DatabaseCount; i++ )
Session->Databases[i]->Close();
And than try to delete the Directory.
BR
Antonio
|
|
| Back to top |
|
 |
Wieland Scholz Guest
|
Posted: Wed Nov 30, 2005 2:06 pm Post subject: Re: TQuery and deleting a directory |
|
|
Hi Antonio,
its me again...
Meanwhile I go mad:
I have extract the problem from my program into a simple example program but I still cannot delete the directory in question. I have also tried it on another machine -> same result.
The TDatabase is now used by the query. Before there was another query used thatswhy the reported difference..
Here again in the code fragment
void __fastcall TForm1::FormCreate(TObject *Sender)
{
TQuery* qry = NULL;
TDatabase* dbLocal = NULL;
int iRet;
AnsiString asDir = "D:\test\L72";
AnsiString asSQL = "SELECT * FROM Test.dbf";
SHFILEOPSTRUCT SHFileOpStruct;
char tmp[255];
try
{
dbLocal = new TDatabase( this );
dbLocal->DatabaseName = asDir;
dbLocal->Open();
qry = new TQuery( this );
qry->DatabaseName = asDir;
qry->SQL->Clear();
qry->SQL->Add( asSQL );
qry->Active = true;
// do something
}
__finally
{
qry->Active = false;
dbLocal->Close();
delete qry;
qry = NULL;
delete dbLocal;
dbLocal = NULL;
}
// prepare delete operation
memset( tmp, 0, sizeof(tmp) );
strcpy( tmp, asDir.c_str() );
memset( &SHFileOpStruct, 0, sizeof( SHFileOpStruct ) );
SHFileOpStruct.hwnd = Handle;
SHFileOpStruct.pFrom = tmp;
SHFileOpStruct.wFunc = FO_DELETE;
SHFileOpStruct.fFlags = FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_SILENT;
// delete dir
try
{
iRet = SHFileOperation( &SHFileOpStruct );
if( iRet != 0 )
{
iRet = GetLastError();
}
}
catch(...)
{
}
}
Did I overlook something? I cannot found any error...
Thank you again
Wieland
"Antonio Felix" <nomail (AT) nm (DOT) pt> wrote:
| Quote: |
I've checked the Database property of qryMeasSpectra and I did not found the same address as I got from
That's odd you should get DbLocal == TQuery->Database always
true!
Probably you still got a Database opened.
Try to iterate the TSession Databases in order to Close;
for ( int i = 0; i < Session->DatabaseCount; i++ )
Session->Databases[i]->Close();
And than try to delete the Directory.
BR
Antonio
|
|
|
| Back to top |
|
 |
Antonio Felix Guest
|
Posted: Wed Nov 30, 2005 4:18 pm Post subject: Re: TQuery and deleting a directory |
|
|
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
| Quote: |
Hi Antonio,
its me again...
Meanwhile I go mad:
I have extract the problem from my program into a simple example program but I still cannot delete the directory in question. I have also tried it on another machine -> same result.
|
Hi Wieland,
I've runned your code with a local dir like C:\Test\Test1\Xpto.Dbf
and it runs just fine...except that it only deletes the last
directory branch ( the file xpto.Dbf and the \Test1 dir ).
What's wrong when you run the code?
Did you get any exception?
Any error message?
Do you have FileSystem permitions on the Dir you're trying to
delete?
HTH
Antonio
|
|
| Back to top |
|
 |
Antonio Felix Guest
|
Posted: Wed Nov 30, 2005 6:36 pm Post subject: Re: TQuery and deleting a directory |
|
|
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
| Quote: |
Hi Antonio,
its me again...
|
Wieland,
I've checked your code on a shared Network directory and it
also works just fine!
So, there's nothing wrong with your code!
What's you're OS?
PS: I'm running the test on a Win2K machine with BCB 5 Enteprise
BR
Antonio
|
|
| Back to top |
|
 |
Wieland Scholz Guest
|
Posted: Thu Dec 01, 2005 6:02 am Post subject: Re: TQuery and deleting a directory |
|
|
Hi Antonio,
thank you for your support.
When I run the code in debugger and stopp it just after setting the query to active and then use the ordinary windows explorer to try to delete the directory L72 ( in my example ) I got the windows error message that the dictionary is used by another application and so cannot be deleted. That seems OK because the database and query really occupy it. But I got the same message after all activities stopped. It appears in both the programs attempt using SHFileOperation() and using the windows explorer.
There is no file system restriction because before and after the program is running I can do everything with the directory.
My OS is WinXP Pro SP1 and C++Builder 5 Pro.
Best regards
Wieland
"Antonio Felix" <nomail (AT) nm (DOT) pt> wrote:
| Quote: |
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
Hi Antonio,
its me again...
Wieland,
I've checked your code on a shared Network directory and it
also works just fine!
So, there's nothing wrong with your code!
What's you're OS?
PS: I'm running the test on a Win2K machine with BCB 5 Enteprise
BR
Antonio
|
|
|
| Back to top |
|
 |
Antonio Felix Guest
|
Posted: Thu Dec 01, 2005 4:28 pm Post subject: Re: TQuery and deleting a directory |
|
|
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
| Quote: |
Hi Antonio,
thank you for your support.
When I run the code in debugger and stopp it just after setting the query to active and then use the ordinary windows explorer to try to delete the directory L72 ( in my example ) I got the windows error message that the dictionary is used by another application and so cannot be deleted. That seems OK because the database and query really occupy it. But I got the same message after all activities stopped. It appears in both the programs attempt using SHFileOperation() and using the windows explorer.
There
is no file system restriction because before and after the program is running I can do everything with the directory.
My OS is WinXP Pro SP1 and C++Builder 5 Pro.
|
Hi Wieland,
That's another story! <g>
The program deletes the Directory if runned all the way?
I'll try to check, under the debug, the same operations as
you report.
I'll give you feedback...but probably only tomorrow!
Br
Antonio
|
|
| Back to top |
|
 |
Antonio Felix Guest
|
Posted: Fri Dec 02, 2005 9:57 am Post subject: Re: TQuery and deleting a directory |
|
|
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
| Quote: |
Hi Antonio,
thank you for your support.
When I run the code in debugger and stopp it just after setting the query to active and then use the ordinary windows explorer to try to delete the directory L72 ( in my example ) I got the windows error message that the dictionary is used by another application and so cannot be deleted. That seems OK because the database and query really occupy it. But I got the same message after all activities stopped. It appears in both the programs attempt using SHFileOperation() and using the windows explorer.
There
is no file system restriction because before and after the program is running I can do everything with the directory.
|
Wieland,
That means you still got something pointing to the directory.
Do you use your own TSession component or the automatic one?
If you use the Automatic one then call Session->Close() just after
you close the Query and the Database and before the code that
try to delete the Directory.
....
__finally
{
qry->Active = false;
dbLocal->Close();
delete qry;
qry = NULL;
delete dbLocal;
dbLocal = NULL;
Session->Close(); // <----------
}
...
I've checked and if you have the Session pointing in some way
(NetFileDir, Private Dir...) to the target Dir you got the
OS error message unless you close the Session.
HTH
Antonio
|
|
| Back to top |
|
 |
Wieland Scholz Guest
|
Posted: Mon Dec 05, 2005 8:40 am Post subject: Re: TQuery and deleting a directory |
|
|
Hi Antonio,
thanks for your help.
I've added the Session->Close() call but the situation is the same...
In the Session instance no dir points to the one to be deleted. After closing it both NetFileDir and PrivateDir are NULL, before they are "C:" and the running dir of my program which is not the same as the data dir to be deleted.
....very strange...
Regards
Wieland
"Antonio Felix" <nomail (AT) nm (DOT) pt> wrote:
| Quote: |
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
Hi Antonio,
thank you for your support.
When I run the code in debugger and stopp it just after setting the query to active and then use the ordinary windows explorer to try to delete the directory L72 ( in my example ) I got the windows error message that the dictionary is used by another application and so cannot be deleted. That seems OK because the database and query really occupy it. But I got the same message after all activities stopped. It appears in both the programs attempt using SHFileOperation() and using the windows explorer.
There
is
no file system restriction because before and after the program is running I can do everything with the directory.
Wieland,
That means you still got something pointing to the directory.
Do you use your own TSession component or the automatic one?
If you use the Automatic one then call Session->Close() just after
you close the Query and the Database and before the code that
try to delete the Directory.
...
__finally
{
qry->Active = false;
dbLocal->Close();
delete qry;
qry = NULL;
delete dbLocal;
dbLocal = NULL;
Session->Close(); // <----------
}
..
I've checked and if you have the Session pointing in some way
(NetFileDir, Private Dir...) to the target Dir you got the
OS error message unless you close the Session.
HTH
Antonio
|
|
|
| Back to top |
|
 |
Antonio Felix Guest
|
Posted: Mon Dec 05, 2005 8:59 am Post subject: Re: TQuery and deleting a directory |
|
|
"Wieland Scholz" <rab-ftl (AT) t-online (DOT) de> wrote:
| Quote: |
Hi Antonio,
thanks for your help.
I've added the Session->Close() call but the situation is the same...
In the Session instance no dir points to the one to be deleted. After closing it both NetFileDir and PrivateDir are NULL, before they are "C:" and the running dir of my program which is not the same as the data dir to be deleted.
...very strange...
Regards
Wieland
|
Wieland,
Well, i don't know what to say!
It's very odd!
Br
Antonio
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|