Delete all files with "a pattern"

  • Thread starter Thread starter Viviana Vc
  • Start date Start date
V

Viviana Vc

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
 
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 said:
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
 
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 said:
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
 
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
 
Viviana Vc said:
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
 
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
 
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
 
Andreas Hadler said:
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
 
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,

No, to be honest. I just took a snippet, that I use for a command not
build into cmd.exe, stripped my success- and error-handling and
replaced the command. Hoping, that Viviana will know (or learn) to use
"cmd /c", and preferring to give an example that's working for me.

Sorry, if I have caused confusion. Later on, I regretted this posting,
especially for cmd not being available everywhere, but was not
bothered enough to give this correction. Sorry again.
i can't see why ShellExecute() couldn't be used as well.

Maybe, as well as system, spawn,...

I just happen to dislike the shellapi.

Andreas
 
Viviana said:
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 CreateProcess and set the show flag to SW_HIDE.

Jussi Jumppanen
Author of: Zeus for Windows, Win32 (Brief, Emacs, etc) FTP Text Editor
"The C/C++, Java, HTML, FTP, Python, PHP, Perl programmer's editor"
Home Page: http://www.zeusedit.com
 
I actually implemented by browsing through the dir with FindFirstFile
and FindNextFile and delete the ones I was interested in.

Viv
 
Back
Top