473,388 Members | 1,383 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,388 software developers and data experts.

Delete all files with "a pattern"

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
Nov 17 '05 #1
12 5539
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" <vc*******@hotmail.com> a écrit dans le message de
news:2g************@uni-berlin.de...
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

Nov 17 '05 #2
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" <vc*******@hotmail.com> wrote in message
news:2g************@uni-berlin.de...
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

Nov 17 '05 #3

"Viviana Vc" <vc*******@hotmail.com> wrote in message
news:2g************@uni-berlin.de...
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

Nov 17 '05 #4
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 !!!"
<no***********@free.fr.spam> 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" <vc*******@hotmail.com> a écrit dans le message de
news:2g************@uni-berlin.de...
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


Nov 17 '05 #5

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" <vc*******@hotmail.com> wrote in message
news:2g************@uni-berlin.de...
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 !!!"
<no***********@free.fr.spam> wrote :
Hello,

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

Regards,
Arno

"Viviana Vc" <vc*******@hotmail.com> a écrit dans le message de
news:2g************@uni-berlin.de...
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

Nov 17 '05 #6
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" <vc*******@hotmail.com> a écrit dans le message de
news:2g************@uni-berlin.de...
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 !!!"
<no***********@free.fr.spam> wrote :
Hello,

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

Regards,
Arno

"Viviana Vc" <vc*******@hotmail.com> a écrit dans le message de
news:2g************@uni-berlin.de...
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

Nov 17 '05 #7
Viviana Vc <vc*******@hotmail.com> 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
Nov 17 '05 #8
Viviana Vc <vc*******@hotmail.com> 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)
Nov 17 '05 #9

"Andreas Hadler" <An************@t-online.de> wrote in message
news:27********************************@4ax.com...
Viviana Vc <vc*******@hotmail.com> 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

Nov 17 '05 #10
Viviana Vc wrote:
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
Nov 17 '05 #11
"Sten Westerback" <sten.westerback@NO_SPAMnokia.com> wrote:

"Andreas Hadler" <An************@t-online.de> wrote in message
news:27********************************@4ax.com.. .
if ( CreateProcess( NULL, // Name of Application
"del bar*.*", // command line

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
--
Prosperity and ruin issue from the power of the tongue.
Therefore, guard yourself against thoughtless speech.
Nov 17 '05 #12
I actually implemented by browsing through the dir with FindFirstFile
and FindNextFile and delete the ones I was interested in.

Viv
On Mon, 24 May 2004 20:51:26 +0200, Andreas Hadler
<An************@t-online.de> wrote :
"Sten Westerback" <sten.westerback@NO_SPAMnokia.com> wrote:

"Andreas Hadler" <An************@t-online.de> wrote in message
news:27********************************@4ax.com. ..

if ( CreateProcess( NULL, // Name of Application
"del bar*.*", // command line

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


Nov 17 '05 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Viviana Vc | last post by:
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...
1
by: Michael Ramey | last post by:
Hi, I'm dynamically creating a table of "delete" imagebuttons, that correspond to files on the webserver. I want to respond to clicks of these buttons, so I know to know what file to delete. ...
4
by: spamfurnace | last post by:
Hi there. Ive just been reading about the Whidbey Provider Pattern on MSDN, http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspnet/html/asp02182004.asp and i wanted to...
0
by: Terry Olsen | last post by:
I need to delete files from "C:\WINNT\Downloaded Program Files" I can delete them using Windows Explorer, but they do not appear in the command prompt directory listing. How can I delete these...
3
by: tvnaidu | last post by:
I compiled tinyxml files (.cpp files) and when I am linking to create final exe in Linux, I am getting lot of errors saying "undefiend reference" to "operator new" and "delete", any idea?. ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.