Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / Languages / Managed C++ / May 2004

Tip: Looking for answers? Try searching our database.

Delete all files with "a pattern"

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Viviana Vc - 12 May 2004 16:54 GMT
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
El Cascador !!! - 12 May 2004 16:59 GMT
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

> Hi all,
>
[quoted text clipped - 8 lines]
> Thanks in advance,
> Viv
Viviana Vc - 13 May 2004 18:18 GMT
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

>Hello,
>
[quoted text clipped - 17 lines]
>> Thanks in advance,
>> Viv
Andreas Hadler - 13 May 2004 21:16 GMT
>Thanks for the answer. I have tried using SHFileOperation and it works,
>but I still have a problem.
[quoted text clipped - 4 lines]
>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
Signature

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)

Sten Westerback - 14 May 2004 13:01 GMT
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

> Thanks for the answer. I have tried using SHFileOperation and it works,
> but I still have a problem.
[quoted text clipped - 29 lines]
> >> Thanks in advance,
> >> Viv
El Cascador !!! - 14 May 2004 16:14 GMT
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

> Thanks for the answer. I have tried using SHFileOperation and it works,
> but I still have a problem.
[quoted text clipped - 29 lines]
> >> Thanks in advance,
> >> Viv
Andreas Hadler - 12 May 2004 20:07 GMT
>Hi all,
>
[quoted text clipped - 3 lines]
>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
Signature

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

Sten Westerback - 24 May 2004 10:11 GMT
> >Hi all,
> >
[quoted text clipped - 50 lines]
>
> 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
Andreas Hadler - 24 May 2004 19:51 GMT
>>    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
Signature

Prosperity and ruin issue from the power of the tongue.
Therefore, guard yourself against thoughtless speech.

Viviana Vc - 27 May 2004 11:49 GMT
I actually implemented by browsing through the dir with FindFirstFile
and FindNextFile and delete the ones I was interested in.

Viv

>>>    if ( CreateProcess( NULL,          // Name of Application
>>>                        "del bar*.*",  // command line
[quoted text clipped - 18 lines]
>
>Andreas
AlexS - 12 May 2004 20:08 GMT
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

> Hi all,
>
[quoted text clipped - 8 lines]
> Thanks in advance,
> Viv
Sten Westerback - 13 May 2004 18:10 GMT
> Hi all,
>
[quoted text clipped - 5 lines]
>
> How could I do this using Windows functions?

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

- Sten
Jussi Jumppanen - 25 May 2004 10:51 GMT
> 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*.*"),
[quoted text clipped - 3 lines]
>
> 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

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.