working with sytem.io

  • Thread starter Thread starter ZR
  • Start date Start date
Z

ZR

Hi,
I am trying to delete multiple files in a particular directory.
The code will like this
\\\
Dim s As String
For Each s in Directory.GetFiles(...)
system.io.file.delete(s)
Next s
///

Is there a method that would allow me delete more than one file at a time?

for instance Delete "*.txt"
 
* "ZR said:
I am trying to delete multiple files in a particular directory.
The code will like this
\\\
Dim s As String
For Each s in Directory.GetFiles(...)
system.io.file.delete(s)
Next s
///

Is there a method that would allow me delete more than one file at a time?

AFAIK there is no easier way to do what you want. You can use
'Directory.Delete' to delete a whole directory.

--
Herfried K. Wagner
MVP · VB Classic, VB.NET
<http://www.mvps.org/dotnet>

<http://www.plig.net/nnq/nquote.html>
 
I have some code I will give you that does recursive directory and file
searches. You can pass the function a wildcard.

'*******************************
'*
'* Shawn Shelton
'* This will search directories for files and directories.
'* May be recursive if told to.
'* Use wildcards for searching such as * or .exe
'*
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Imports System.Collections
Module Searcher
Public Sub SearchDirectoryForFiles(ByVal dirlist As Stack, ByVal
searchPath As String, ByVal RecurseDirs As Boolean)
' Split searchPath into a directory and a wildcard specification.
'
Dim directoryPath As String = Path.GetDirectoryName(searchPath)
Dim search As String = Path.GetFileName(searchPath)
Dim dir As New DirectoryInfo(directoryPath)
If directoryPath Is Nothing Or search Is Nothing Then
Return
End If
Dim f As FileInfo
Try
For Each f In dir.GetFiles(search)
dirlist.Push(f.FullName)
Next
Catch e As UnauthorizedAccessException
Return
Catch e As DirectoryNotFoundException
Return
End Try

' Now that you have finished the files in this directory, recurse for
' each subdirectory.
If RecurseDirs = True Then
Dim directories As String() = Directory.GetDirectories(directoryPath)
Dim d As String
For Each d In directories
SearchDirectoryForFiles(dirlist, Path.Combine(d, search), True)
Next d
End If
End Sub

Public Sub SearchDirectoryForDirectories(ByVal dirList As Stack, ByVal
searchPath As String)
Dim directoryPath As String = Path.GetDirectoryName(searchPath)
Dim directories As String() = Directory.GetDirectories(directoryPath)
Dim d As String
For Each d In directories
dirList.Push(d)
SearchDirectoryForDirectories(dirList, d & "\")
Next d
End Sub

End Module
****************************

Call it like this:

dim dirlist as stack
dim x as string
SearchDirectoryForFiles(DirList, "c:\winnt\*.txt", False)
If DirList.Count > 0 Then
For Each x In DirList
SetAttr(x, FileAttribute.Normal)
File.Delete(x)
Next
End If
 
Thanks alot for your help
Shawn D Shelton said:
I have some code I will give you that does recursive directory and file
searches. You can pass the function a wildcard.

'*******************************
'*
'* Shawn Shelton
'* This will search directories for files and directories.
'* May be recursive if told to.
'* Use wildcards for searching such as * or .exe
'*
Option Explicit On
Option Strict On
Imports System
Imports System.IO
Imports System.Collections
Module Searcher
Public Sub SearchDirectoryForFiles(ByVal dirlist As Stack, ByVal
searchPath As String, ByVal RecurseDirs As Boolean)
' Split searchPath into a directory and a wildcard specification.
'
Dim directoryPath As String = Path.GetDirectoryName(searchPath)
Dim search As String = Path.GetFileName(searchPath)
Dim dir As New DirectoryInfo(directoryPath)
If directoryPath Is Nothing Or search Is Nothing Then
Return
End If
Dim f As FileInfo
Try
For Each f In dir.GetFiles(search)
dirlist.Push(f.FullName)
Next
Catch e As UnauthorizedAccessException
Return
Catch e As DirectoryNotFoundException
Return
End Try

' Now that you have finished the files in this directory, recurse for
' each subdirectory.
If RecurseDirs = True Then
Dim directories As String() = Directory.GetDirectories(directoryPath)
Dim d As String
For Each d In directories
SearchDirectoryForFiles(dirlist, Path.Combine(d, search), True)
Next d
End If
End Sub

Public Sub SearchDirectoryForDirectories(ByVal dirList As Stack, ByVal
searchPath As String)
Dim directoryPath As String = Path.GetDirectoryName(searchPath)
Dim directories As String() = Directory.GetDirectories(directoryPath)
Dim d As String
For Each d In directories
dirList.Push(d)
SearchDirectoryForDirectories(dirList, d & "\")
Next d
End Sub

End Module
****************************

Call it like this:

dim dirlist as stack
dim x as string
SearchDirectoryForFiles(DirList, "c:\winnt\*.txt", False)
If DirList.Count > 0 Then
For Each x In DirList
SetAttr(x, FileAttribute.Normal)
File.Delete(x)
Next
End If
 
Back
Top