File help

  • Thread starter Thread starter John Viall
  • Start date Start date
J

John Viall

I am trying to look through some directories and delete all files older than
X days, but I'm having trouble with the logic.

The directory structure might look like
\main\sample1\files
\main\sample2\files
\main\sample3\files

I need to be able to get to the files, check their creation date, and delete
them if they are older than X days.

While I'm at it, how can I get a count of all the "sample" directories?

Thanks,
John
 
I am trying to look through some directories and delete all files older than
X days, but I'm having trouble with the logic.

The directory structure might look like
\main\sample1\files
\main\sample2\files
\main\sample3\files

I need to be able to get to the files, check their creation date, and delete
them if they are older than X days.

While I'm at it, how can I get a count of all the "sample" directories?

OK, fromwhat I can tell, you want to scan all directories that begin
with the word "sample" that are all subfolders in a folder named
"main".

Get a list of the sample folders with:

DirectoryInfo.GetDirectories using the overload that takes a search
pattern and use the pattern "sample*".

Then, for each sample folder use DirectoryInfo.GetFiles to get the
list of files in a sample folder. That returns an array of FileInfo
objects for each file. The FileInfo object contains a property for
file creation date.

If the sample folders have subfolders and you want to scan all of
them, you will need to recursively call the DirectoryInfo.GetFiles
with code like this:

Public Sub GetFiles(ByVal Path As String)

Dim myDirectoryRoot As New DirectoryInfo(Path)
Dim di As DirectoryInfo
Dim fi As FileInfo
Dim lSize As Long = 0

For Each fi In myDirectoryRoot.GetFiles
_Files.Add(fi.FullName)
_TotalFiles += 1
_TotalSize += fi.Length
Next
For Each di In myDirectoryRoot.GetDirectories()
_Folders.Add(di.FullName)
_TotalFolders += 1
GetFiles(di.FullName)
Next
myDirectoryRoot = Nothing

End Sub
 
John said:
I am trying to look through some directories and delete all files older than
X days, but I'm having trouble with the logic.

The directory structure might look like
\main\sample1\files
\main\sample2\files
\main\sample3\files

I need to be able to get to the files, check their creation date, and delete
them if they are older than X days.

While I'm at it, how can I get a count of all the "sample" directories?


Have a look at the System.IO namespace, for example
- Directory.GetDirectories
- File.GetCreationTime
- FileSystemInfo.CreationTime
- FileSystemInfo.Delete
- System.IO.File.Delete


Armin
 
Back
Top