PC Review


Reply
Thread Tools Rate Thread

Delete all files with "a pattern"

 
 
Viviana Vc
Guest
Posts: n/a
 
      12th May 2004
Hi all,

I would like to delete from a directory all the files that match: bar*.*
I know that I could do for instance: system("del bar*.*"), but this will
bring up the command prompt window and as my app is a winmain app this
wouldn't be nice. I could use DeleteFile, but you can not use wildcards
in this one.

How could I do this using Windows functions?

Thanks in advance,
Viv
 
Reply With Quote
 
 
 
 
El Cascador !!!
Guest
Posts: n/a
 
      12th May 2004
Hello,

You can either use "SHFileOperation" with FO_DELETE flag set in the
SHFILEOPSTRUCT structure or browse the directory deleting files that match
your expression using "PathMatchSpec" function.

Regards,
Arno

"Viviana Vc" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
> Hi all,
>
> I would like to delete from a directory all the files that match: bar*.*
> I know that I could do for instance: system("del bar*.*"), but this will
> bring up the command prompt window and as my app is a winmain app this
> wouldn't be nice. I could use DeleteFile, but you can not use wildcards
> in this one.
>
> How could I do this using Windows functions?
>
> Thanks in advance,
> Viv



 
Reply With Quote
 
Andreas Hadler
Guest
Posts: n/a
 
      12th May 2004
Viviana Vc <(E-Mail Removed)> wrote:

>Hi all,
>
>I would like to delete from a directory all the files that match: bar*.*
>I know that I could do for instance: system("del bar*.*"), but this will
>bring up the command prompt window and as my app is a winmain app this
>wouldn't be nice. I could use DeleteFile, but you can not use wildcards
>in this one.
>


STARTUPINFO si;
PROCESS_INFORMATION pi;

ZeroMemory( &si, sizeof(si) );
si.dwFlags |= STARTF_USESHOWWINDOW ;
si.wShowWindow = SW_HIDE ;
si.cb = sizeof(si);

ZeroMemory( &pi, sizeof(pi) );

if ( CreateProcess( NULL, // Name of Application
"del bar*.*", // command line
NULL, // process attributes
NULL, // Thread attributes
FALSE, // no inheritable handle
0, // Creation flags
NULL, // use callers environment
NULL, // use current directory
&si,
&pi
)
)
{
DWORD dwExitCode = 1 ; // preset: error
DWORD dwWaitResult ;
dwWaitResult = WaitForSingleObject( pi.hProcess, INFINITE ) ;
if ( WAIT_OBJECT_0 == dwWaitResult
&& GetExitCodeProcess( pi.hProcess, &dwExitCode )
&& 0 == dwExitCode
)
{
// success
}
else
{
// failed
}
CloseHandle( pi.hProcess );
CloseHandle( pi.hThread );
}
else
{
// failed
}

Andreas
--
If C++ has taught me one thing, it's this: Just because the system is
consistent doesn't mean it's not the work of Satan.
-- Andrew Plotkin
 
Reply With Quote
 
AlexS
Guest
Posts: n/a
 
      12th May 2004
Hi, Viviana

Have a look at Directory.GetFiles method. You can get list of files using
mask and then delete them using standard File.Delete method.

HTH
Alex

"Viviana Vc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all,
>
> I would like to delete from a directory all the files that match: bar*.*
> I know that I could do for instance: system("del bar*.*"), but this will
> bring up the command prompt window and as my app is a winmain app this
> wouldn't be nice. I could use DeleteFile, but you can not use wildcards
> in this one.
>
> How could I do this using Windows functions?
>
> Thanks in advance,
> Viv



 
Reply With Quote
 
Sten Westerback
Guest
Posts: n/a
 
      13th May 2004

"Viviana Vc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Hi all,
>
> I would like to delete from a directory all the files that match: bar*.*
> I know that I could do for instance: system("del bar*.*"), but this will
> bring up the command prompt window and as my app is a winmain app this
> wouldn't be nice. I could use DeleteFile, but you can not use wildcards
> in this one.
>
> How could I do this using Windows functions?


Use FindFirstFile() and it's siblings to get the file names and then
DeleteFile().

- Sten



 
Reply With Quote
 
Viviana Vc
Guest
Posts: n/a
 
      13th May 2004
Thanks for the answer. I have tried using SHFileOperation and it works,
but I still have a problem.

I want to delete the files named: file.log.1, file.log.2, ...,
file.log.x, _but_ not to delete: file.log

If I use "file.log.*" in pFrom, also the file.log will be deleted. How
can I avoid this?

Thx in advance,
Viv

On Wed, 12 May 2004 17:59:22 +0200, "El Cascador !!!"
<(E-Mail Removed)> wrote :

>Hello,
>
>You can either use "SHFileOperation" with FO_DELETE flag set in the
>SHFILEOPSTRUCT structure or browse the directory deleting files that match
>your expression using "PathMatchSpec" function.
>
>Regards,
>Arno
>
>"Viviana Vc" <(E-Mail Removed)> a écrit dans le message de
>news:(E-Mail Removed)...
>> Hi all,
>>
>> I would like to delete from a directory all the files that match: bar*.*
>> I know that I could do for instance: system("del bar*.*"), but this will
>> bring up the command prompt window and as my app is a winmain app this
>> wouldn't be nice. I could use DeleteFile, but you can not use wildcards
>> in this one.
>>
>> How could I do this using Windows functions?
>>
>> Thanks in advance,
>> Viv

>


 
Reply With Quote
 
Andreas Hadler
Guest
Posts: n/a
 
      13th May 2004
Viviana Vc <(E-Mail Removed)> wrote:

>Thanks for the answer. I have tried using SHFileOperation and it works,
>but I still have a problem.
>
>I want to delete the files named: file.log.1, file.log.2, ...,
>file.log.x, _but_ not to delete: file.log
>
>If I use "file.log.*" in pFrom, also the file.log will be deleted. How
>can I avoid this?


In cmd, I regularily do

ren yyy.log xxx
del yyy.*
ren xxx yyy.log

Andreas
--
Every program has at least one bug and can be reduced by at least one
line. By induction, then, every program can be reduced to a single
instruction, and that will be wrong.
(unknown)
 
Reply With Quote
 
Sten Westerback
Guest
Posts: n/a
 
      14th May 2004

Method 1: Enumerate the files yourself and detect when you shouldn't delete.
Method 2: Temporarily add ReadOnly-attribute or lock the file that shouldn't
be deleted.

- Sten

"Viviana Vc" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Thanks for the answer. I have tried using SHFileOperation and it works,
> but I still have a problem.
>
> I want to delete the files named: file.log.1, file.log.2, ...,
> file.log.x, _but_ not to delete: file.log
>
> If I use "file.log.*" in pFrom, also the file.log will be deleted. How
> can I avoid this?
>
> Thx in advance,
> Viv
>
> On Wed, 12 May 2004 17:59:22 +0200, "El Cascador !!!"
> <(E-Mail Removed)> wrote :
>
> >Hello,
> >
> >You can either use "SHFileOperation" with FO_DELETE flag set in the
> >SHFILEOPSTRUCT structure or browse the directory deleting files that

match
> >your expression using "PathMatchSpec" function.
> >
> >Regards,
> >Arno
> >
> >"Viviana Vc" <(E-Mail Removed)> a écrit dans le message de
> >news:(E-Mail Removed)...
> >> Hi all,
> >>
> >> I would like to delete from a directory all the files that match:

bar*.*
> >> I know that I could do for instance: system("del bar*.*"), but this

will
> >> bring up the command prompt window and as my app is a winmain app this
> >> wouldn't be nice. I could use DeleteFile, but you can not use wildcards
> >> in this one.
> >>
> >> How could I do this using Windows functions?
> >>
> >> Thanks in advance,
> >> Viv

> >

>



 
Reply With Quote
 
El Cascador !!!
Guest
Posts: n/a
 
      14th May 2004
Are you sure it will delete file.log ?
It shouldn't, there is no point after the filename...
Anyway, you may want to use generic character '?' that can replace ONE
character...
.... and try "file.log.?"...

Regards,
Arno

"Viviana Vc" <(E-Mail Removed)> a écrit dans le message de
news:(E-Mail Removed)...
> Thanks for the answer. I have tried using SHFileOperation and it works,
> but I still have a problem.
>
> I want to delete the files named: file.log.1, file.log.2, ...,
> file.log.x, _but_ not to delete: file.log
>
> If I use "file.log.*" in pFrom, also the file.log will be deleted. How
> can I avoid this?
>
> Thx in advance,
> Viv
>
> On Wed, 12 May 2004 17:59:22 +0200, "El Cascador !!!"
> <(E-Mail Removed)> wrote :
>
> >Hello,
> >
> >You can either use "SHFileOperation" with FO_DELETE flag set in the
> >SHFILEOPSTRUCT structure or browse the directory deleting files that

match
> >your expression using "PathMatchSpec" function.
> >
> >Regards,
> >Arno
> >
> >"Viviana Vc" <(E-Mail Removed)> a écrit dans le message de
> >news:(E-Mail Removed)...
> >> Hi all,
> >>
> >> I would like to delete from a directory all the files that match:

bar*.*
> >> I know that I could do for instance: system("del bar*.*"), but this

will
> >> bring up the command prompt window and as my app is a winmain app this
> >> wouldn't be nice. I could use DeleteFile, but you can not use wildcards
> >> in this one.
> >>
> >> How could I do this using Windows functions?
> >>
> >> Thanks in advance,
> >> Viv

> >

>



 
Reply With Quote
 
Sten Westerback
Guest
Posts: n/a
 
      24th May 2004

"Andreas Hadler" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> Viviana Vc <(E-Mail Removed)> wrote:
>
> >Hi all,
> >
> >I would like to delete from a directory all the files that match: bar*.*
> >I know that I could do for instance: system("del bar*.*"), but this will
> >bring up the command prompt window and as my app is a winmain app this
> >wouldn't be nice. I could use DeleteFile, but you can not use wildcards
> >in this one.
> >

>
> STARTUPINFO si;
> PROCESS_INFORMATION pi;
>
> ZeroMemory( &si, sizeof(si) );
> si.dwFlags |= STARTF_USESHOWWINDOW ;
> si.wShowWindow = SW_HIDE ;
> si.cb = sizeof(si);
>
> ZeroMemory( &pi, sizeof(pi) );
>
> if ( CreateProcess( NULL, // Name of Application
> "del bar*.*", // command line
> NULL, // process attributes
> NULL, // Thread attributes
> FALSE, // no inheritable handle
> 0, // Creation flags
> NULL, // use callers environment
> NULL, // use current directory
> &si,
> &pi
> )
> )
> {
> DWORD dwExitCode = 1 ; // preset: error
> DWORD dwWaitResult ;
> dwWaitResult = WaitForSingleObject( pi.hProcess, INFINITE ) ;
> if ( WAIT_OBJECT_0 == dwWaitResult
> && GetExitCodeProcess( pi.hProcess, &dwExitCode )
> && 0 == dwExitCode
> )
> {
> // success
> }
> else
> {
> // failed
> }
> CloseHandle( pi.hProcess );
> CloseHandle( pi.hThread );
> }
> else
> {
> // failed
> }
>
> Andreas


Did you test that? I would claim that you have to inform shellexecute that
the REN command is inside CMD.exe .. that is "cmd /c ren .....". Also,
i can't see why ShellExecute() couldn't be used as well.

- Sten



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Offline Files: "Delete Files" vs "Re-initalize the chache" Brendan Windows XP General 7 24th Sep 2008 01:47 AM
"Rule based design" or "event-condition-action" pattern? feng Microsoft VB .NET 1 28th Feb 2005 09:18 AM
Delete all files with "a pattern" Viviana Vc Microsoft VC .NET 12 27th May 2004 11:49 AM
Re: Rename or Delete old "Program Files" & "Documents & Settings" Craig Landis [MSFT] Microsoft Windows 2000 File System 0 6th Mar 2004 06:47 PM
RE: Unable to delete "Kaza"; Files are "right protected". =?Utf-8?B?Qnl0ZQ==?= Windows XP General 1 6th Jan 2004 07:05 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 06:54 PM.