Board index » cppbuilder » List of files

List of files


2004-04-21 08:43:31 PM
cppbuilder89
How is-it possible to do:
dir *.txt
and get the files list in a buffer ?
Sam
 
 

Re:List of files

"sam" < XXXX@XXXXX.COM >wrote:
Quote
How is-it possible to do:
dir *.txt
and get the files list in a buffer ?
The best choice depends on your needs. It may be enough to use
ShellExecute with a batch file and then use TStringList::LoadFromFile
to load the data.
Otherwise you're going to have to pipe the data directly into
a control:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
//create pipe for the console stdout
SECURITY_ATTRIBUTES sa;
ZeroMemory(&sa,sizeof(SECURITY_ATTRIBUTES));
sa.nLength=sizeof(SECURITY_ATTRIBUTES);
sa.bInheritHandle=true;
sa.lpSecurityDescriptor=NULL;
HANDLE ReadPipeHandle;
HANDLE WritePipeHandle; // not used here
if(!CreatePipe(&ReadPipeHandle,&WritePipeHandle,&sa,0))
RaiseLastWin32Error();
//Create a Console
STARTUPINFO si;
ZeroMemory(&si,sizeof(STARTUPINFO));
si.cb=sizeof(STARTUPINFO);
si.dwFlags=STARTF_USESHOWWINDOW|STARTF_USESTDHANDLES;
si.wShowWindow=SW_HIDE;
si.hStdOutput=WritePipeHandle;
si.hStdError=WritePipeHandle;
PROCESS_INFORMATION pi;
ZeroMemory(&pi,sizeof(PROCESS_INFORMATION));
if(!CreateProcess("the dos command or your Program.exe",NULL,NULL,NULL,true,0,NULL,NULL,&si,&pi))
RaiseLastWin32Error();
//Read from pipe
char Data[1024];
for (;;)
{
DWORD BytesRead;
DWORD TotalBytes;
DWORD BytesLeft;
//Check for the presence of data in the pipe
if(!PeekNamedPipe(ReadPipeHandle,Data,sizeof(Data),&BytesRead,
&TotalBytes,&BytesLeft))RaiseLastWin32Error();
//If there is bytes, read them
if(BytesRead)
{
if(!ReadFile(ReadPipeHandle,Data,sizeof(Data)-1,&BytesRead,NULL))
RaiseLastWin32Error();
Data[BytesRead]='\0';
Memo1->Lines->Add(AnsiString(Data));
}
else
{
//Is the console app terminated?
if(WaitForSingleObject(pi.hProcess,0)==WAIT_OBJECT_0)break;
}
}
CloseHandle(pi.hThread);
CloseHandle(pi.hProcess);
CloseHandle(ReadPipeHandle);
CloseHandle(WritePipeHandle);
}
~ JD
 

Re:List of files

"sam" < XXXX@XXXXX.COM >wrote in message
Quote
How is-it possible to do:
dir *.txt
and get the files list in a buffer ?
Use the VCL's FindFirst() and FindNext() functions, ie:
TStringList *FileNames = new TStringList;
TSearchRec sr;
if( FindFirst("c:\\path to search\\*.txt", faAnyFile, sr) == 0 )
{
do {
FileNames->Append(sr.Name);
}
while( FindNext(sr) == 0 );
FindClose(sr);
}
// use FileNames as needed...
delete FileNames;
Gambit
 

{smallsort}

Re:List of files

"Remy Lebeau \(TeamB\)" < XXXX@XXXXX.COM >wrote:
Quote
Use the VCL's FindFirst() and FindNext() functions,
Duh
~ JD
 

Re:List of files

JD wrote:
Quote
Otherwise you're going to have to pipe the data directly into
a control:

void __fastcall TForm1::Button1Click(TObject *Sender)
{
//create pipe for the console stdout
[snip]
Code coming straight from my site without mentioning it.
JD, you're not a nice guy!
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, C++BuilderX, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

Re:List of files

Michel Leunen wrote:
Quote
Code coming straight from my site without mentioning it.
Unmistakingly the same.
Quote
JD, you're not a nice guy!
It would have been nice if he had quoted you. But he could
have simply forgotten it.
For me it takes a bit more to consider JD not a nice guy.
Hans.
 

Re:List of files

Hans Galema wrote:
Quote
For me it takes a bit more to consider JD not a nice guy.
Consider I was upset and I have to admit that my words were quite a bit
excessive.
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, C++BuilderX, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------
 

Re:List of files

Michel Leunen < XXXX@XXXXX.COM >wrote:
Quote
Code coming straight from my site without mentioning it.
JD, you're not a nice guy!
Just because it's on your site doesn't mean that that's where
it came from - and in fact it didn't. I know so because I
seggregate code collected from newsgroups such as this one
and code collected from other sites.
Do you think that it's possible that you may have posted that
block of code here before?
Beside the fact that I've never visited your site, I don't
post code from other sites because of copyrite issues.
At first I saved salutations with the samples but the collection
grew and it's just one more thing to add and maintain with the
db. So ....
Allow me to appoligize to you and everyone else who's samples
I have posted and will post without credits.
~ JD
 

Re:List of files

JD wrote:
Quote
Do you think that it's possible that you may have posted that
block of code here before?
The possibility exists, yes.
Quote
Beside the fact that I've never visited your site,
you should :-)
Quote
Allow me to appoligize to you and everyone else who's samples
I have posted and will post without credits.
No need for apologies. There is no harm. After all, the intent of my
site is to help other BCB users and that's what you've done posting the
code even if giving credit would have been appreciated. Appreciated but
not necessary. And I understand that knowing where a code snippet is
coming from is hard and not always doable.
Sorry for having been so harsh.
Michel
--
----------------------------------------
Michel Leunen
mailto: see my homepage.
C++Builder, C++BuilderX, BCC5.5.1 Web site:
www.leunen.com/
----------------------------------------