Fill a ListBox with Excel files older than x month
I'm making a WinForm project that shows me all the Excel files older than x months of a selected folder.
I can populate a ListBox
with all the Excel files (see code below).
In selecting the older than x month, I don't succeed. I think I should work with the LastWriteTime
but can’t find how to use it.
Can anyone help me to find a solution on this?
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fileNames As String() =
System.IO.Directory.GetFiles(folderPath,
"*.xl*", System.IO.SearchOption.TopDirectoryOnly)
For Each fileName As String In fileNames
fileslistbox.Items.Add(fileName)
Next
End Sub
excel vb.net winforms
add a comment |
I'm making a WinForm project that shows me all the Excel files older than x months of a selected folder.
I can populate a ListBox
with all the Excel files (see code below).
In selecting the older than x month, I don't succeed. I think I should work with the LastWriteTime
but can’t find how to use it.
Can anyone help me to find a solution on this?
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fileNames As String() =
System.IO.Directory.GetFiles(folderPath,
"*.xl*", System.IO.SearchOption.TopDirectoryOnly)
For Each fileName As String In fileNames
fileslistbox.Items.Add(fileName)
Next
End Sub
excel vb.net winforms
add a comment |
I'm making a WinForm project that shows me all the Excel files older than x months of a selected folder.
I can populate a ListBox
with all the Excel files (see code below).
In selecting the older than x month, I don't succeed. I think I should work with the LastWriteTime
but can’t find how to use it.
Can anyone help me to find a solution on this?
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fileNames As String() =
System.IO.Directory.GetFiles(folderPath,
"*.xl*", System.IO.SearchOption.TopDirectoryOnly)
For Each fileName As String In fileNames
fileslistbox.Items.Add(fileName)
Next
End Sub
excel vb.net winforms
I'm making a WinForm project that shows me all the Excel files older than x months of a selected folder.
I can populate a ListBox
with all the Excel files (see code below).
In selecting the older than x month, I don't succeed. I think I should work with the LastWriteTime
but can’t find how to use it.
Can anyone help me to find a solution on this?
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fileNames As String() =
System.IO.Directory.GetFiles(folderPath,
"*.xl*", System.IO.SearchOption.TopDirectoryOnly)
For Each fileName As String In fileNames
fileslistbox.Items.Add(fileName)
Next
End Sub
excel vb.net winforms
excel vb.net winforms
edited Nov 14 '18 at 16:34
Jimi
8,56241934
8,56241934
asked Nov 14 '18 at 12:55
Bart Van AudenhoveBart Van Audenhove
103
103
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
The information on the a File Creation Time / Last Write Time is returned by the FileInfo class, which provides the Creation Time, Last Write Time and Last Access Time of a file through a FileSystemInfo class.
See whether you need the File Creation Time or the Last Write Time, depending on what your requirements are.
The Last Access Time may return just the file Creation DateTime
, depending on the System.
An example, with a modified ListFiles
method:
I've added a parameter to the method, OlderThanMonths
, which is used to specify how old a file should be to be included in the list.
Here, the DateTime
reference is LastWriteTime
.
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
Dim fileNames As String() = Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly)
ListBox1.BeginUpdate()
For Each fileName As String In fileNames
Dim FIinfo As New FileInfo(fileName)
If FIinfo.LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now Then
filesListBox.Items.Add(fileName)
End If
Next
ListBox1.EndUpdate()
End Sub
Or with a LINQ Where()
filter:
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
filesListBox.Items.AddRange(
Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly).
Where(Function(f) New FileInfo(f).LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now).
ToArray())
End Sub
add a comment |
You are almost there. Just need to add an IF clause to check the Modified Date.
Check this code sample:
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fileNames As String() =
System.IO.Directory.GetFiles(folderPath,
"*.xl*", System.IO.SearchOption.TopDirectoryOnly)
For Each fileName As String In fileNames
Dim dtFileModifiedDate As DateTime = IO.File.GetLastWriteTime(fileName)
Dim dtCustomDate As DateTime = DateTime.ParseExact(20180401, "yyyyMMdd", Globalization.CultureInfo.InvariantCulture)
If dtFileModifiedDate > dtCustomDate Then
filesListBox.Items.Add(fileName)
End If
Next
End Sub
add a comment |
Your Answer
StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");
StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);
else
createEditor();
);
function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);
);
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300769%2ffill-a-listbox-with-excel-files-older-than-x-month%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
The information on the a File Creation Time / Last Write Time is returned by the FileInfo class, which provides the Creation Time, Last Write Time and Last Access Time of a file through a FileSystemInfo class.
See whether you need the File Creation Time or the Last Write Time, depending on what your requirements are.
The Last Access Time may return just the file Creation DateTime
, depending on the System.
An example, with a modified ListFiles
method:
I've added a parameter to the method, OlderThanMonths
, which is used to specify how old a file should be to be included in the list.
Here, the DateTime
reference is LastWriteTime
.
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
Dim fileNames As String() = Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly)
ListBox1.BeginUpdate()
For Each fileName As String In fileNames
Dim FIinfo As New FileInfo(fileName)
If FIinfo.LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now Then
filesListBox.Items.Add(fileName)
End If
Next
ListBox1.EndUpdate()
End Sub
Or with a LINQ Where()
filter:
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
filesListBox.Items.AddRange(
Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly).
Where(Function(f) New FileInfo(f).LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now).
ToArray())
End Sub
add a comment |
The information on the a File Creation Time / Last Write Time is returned by the FileInfo class, which provides the Creation Time, Last Write Time and Last Access Time of a file through a FileSystemInfo class.
See whether you need the File Creation Time or the Last Write Time, depending on what your requirements are.
The Last Access Time may return just the file Creation DateTime
, depending on the System.
An example, with a modified ListFiles
method:
I've added a parameter to the method, OlderThanMonths
, which is used to specify how old a file should be to be included in the list.
Here, the DateTime
reference is LastWriteTime
.
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
Dim fileNames As String() = Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly)
ListBox1.BeginUpdate()
For Each fileName As String In fileNames
Dim FIinfo As New FileInfo(fileName)
If FIinfo.LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now Then
filesListBox.Items.Add(fileName)
End If
Next
ListBox1.EndUpdate()
End Sub
Or with a LINQ Where()
filter:
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
filesListBox.Items.AddRange(
Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly).
Where(Function(f) New FileInfo(f).LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now).
ToArray())
End Sub
add a comment |
The information on the a File Creation Time / Last Write Time is returned by the FileInfo class, which provides the Creation Time, Last Write Time and Last Access Time of a file through a FileSystemInfo class.
See whether you need the File Creation Time or the Last Write Time, depending on what your requirements are.
The Last Access Time may return just the file Creation DateTime
, depending on the System.
An example, with a modified ListFiles
method:
I've added a parameter to the method, OlderThanMonths
, which is used to specify how old a file should be to be included in the list.
Here, the DateTime
reference is LastWriteTime
.
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
Dim fileNames As String() = Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly)
ListBox1.BeginUpdate()
For Each fileName As String In fileNames
Dim FIinfo As New FileInfo(fileName)
If FIinfo.LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now Then
filesListBox.Items.Add(fileName)
End If
Next
ListBox1.EndUpdate()
End Sub
Or with a LINQ Where()
filter:
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
filesListBox.Items.AddRange(
Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly).
Where(Function(f) New FileInfo(f).LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now).
ToArray())
End Sub
The information on the a File Creation Time / Last Write Time is returned by the FileInfo class, which provides the Creation Time, Last Write Time and Last Access Time of a file through a FileSystemInfo class.
See whether you need the File Creation Time or the Last Write Time, depending on what your requirements are.
The Last Access Time may return just the file Creation DateTime
, depending on the System.
An example, with a modified ListFiles
method:
I've added a parameter to the method, OlderThanMonths
, which is used to specify how old a file should be to be included in the list.
Here, the DateTime
reference is LastWriteTime
.
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
Dim fileNames As String() = Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly)
ListBox1.BeginUpdate()
For Each fileName As String In fileNames
Dim FIinfo As New FileInfo(fileName)
If FIinfo.LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now Then
filesListBox.Items.Add(fileName)
End If
Next
ListBox1.EndUpdate()
End Sub
Or with a LINQ Where()
filter:
Private Sub ListFiles(ByVal folderPath As String, OlderThanMonths As Integer)
filesListBox.Items.Clear()
filesListBox.Items.AddRange(
Directory.GetFiles(folderPath, "*.xl*", SearchOption.TopDirectoryOnly).
Where(Function(f) New FileInfo(f).LastWriteTime.AddMonths(OlderThanMonths) <= Date.Now).
ToArray())
End Sub
edited Nov 18 '18 at 21:43
answered Nov 14 '18 at 15:47
JimiJimi
8,56241934
8,56241934
add a comment |
add a comment |
You are almost there. Just need to add an IF clause to check the Modified Date.
Check this code sample:
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fileNames As String() =
System.IO.Directory.GetFiles(folderPath,
"*.xl*", System.IO.SearchOption.TopDirectoryOnly)
For Each fileName As String In fileNames
Dim dtFileModifiedDate As DateTime = IO.File.GetLastWriteTime(fileName)
Dim dtCustomDate As DateTime = DateTime.ParseExact(20180401, "yyyyMMdd", Globalization.CultureInfo.InvariantCulture)
If dtFileModifiedDate > dtCustomDate Then
filesListBox.Items.Add(fileName)
End If
Next
End Sub
add a comment |
You are almost there. Just need to add an IF clause to check the Modified Date.
Check this code sample:
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fileNames As String() =
System.IO.Directory.GetFiles(folderPath,
"*.xl*", System.IO.SearchOption.TopDirectoryOnly)
For Each fileName As String In fileNames
Dim dtFileModifiedDate As DateTime = IO.File.GetLastWriteTime(fileName)
Dim dtCustomDate As DateTime = DateTime.ParseExact(20180401, "yyyyMMdd", Globalization.CultureInfo.InvariantCulture)
If dtFileModifiedDate > dtCustomDate Then
filesListBox.Items.Add(fileName)
End If
Next
End Sub
add a comment |
You are almost there. Just need to add an IF clause to check the Modified Date.
Check this code sample:
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fileNames As String() =
System.IO.Directory.GetFiles(folderPath,
"*.xl*", System.IO.SearchOption.TopDirectoryOnly)
For Each fileName As String In fileNames
Dim dtFileModifiedDate As DateTime = IO.File.GetLastWriteTime(fileName)
Dim dtCustomDate As DateTime = DateTime.ParseExact(20180401, "yyyyMMdd", Globalization.CultureInfo.InvariantCulture)
If dtFileModifiedDate > dtCustomDate Then
filesListBox.Items.Add(fileName)
End If
Next
End Sub
You are almost there. Just need to add an IF clause to check the Modified Date.
Check this code sample:
Private Sub ListFiles(ByVal folderPath As String)
filesListBox.Items.Clear()
Dim fileNames As String() =
System.IO.Directory.GetFiles(folderPath,
"*.xl*", System.IO.SearchOption.TopDirectoryOnly)
For Each fileName As String In fileNames
Dim dtFileModifiedDate As DateTime = IO.File.GetLastWriteTime(fileName)
Dim dtCustomDate As DateTime = DateTime.ParseExact(20180401, "yyyyMMdd", Globalization.CultureInfo.InvariantCulture)
If dtFileModifiedDate > dtCustomDate Then
filesListBox.Items.Add(fileName)
End If
Next
End Sub
answered Nov 14 '18 at 15:37
JortxJortx
1711112
1711112
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53300769%2ffill-a-listbox-with-excel-files-older-than-x-month%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown