handing the OnCreated event of FileSystemWatcher - vb.net

I implemented this event of the FileSystemWatcher:
Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
If e.Name.ToUpper() == "MYTEXTFILE.TXT" then
' code '
End If
End Sub
Is there a way to monitor if created files are in a textbox similar to this?
Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
If e.Name.ToUpper.contains(textbox1.text) then
' code '
End Sub

Having more than one file in a multiline textbox and every filename is in a separate line requires that you should split the filenames individually and then check each one with the file just created.
Private Shared Sub OnCreated(source As Object, e As FileSystemEventArgs)
' Get an array of the files at each line and remove eventually spurious empty lines
Dim files() = textbox1.Text.Split(New String() {Environment.NewLine}, _
StringSplitOptions.RemoveEmptyEntries)
Dim newFile = e.Name.ToUpper()
for each file in files
if file.ToUpper() = newFile Then
' code '
Exit For
End If
Next
End Sub

I'm not 100% clear I understand your question, but I'm assuming that you want to compare the name of the file that was just created with the name of a file in a textbox.
1) Is the text in the textbox also uppercase? I see you uppercase the name of the file before comparing.
2) Is the text in the textbox "containable" in the name of the file, maybe the other way around?
3) Should the condition be .Equal instead of .Contains?

Related

Multiple Selections from Listbox to rename Folders

Have a vb.net userform with a listbox set to multiselect. This is for personal use. The listbox populates itself when the form loads with all the subfolders, by name only, in a designated folder. I want to append a prefix to each selected folder in the listbox.
By default, all folders would have the prefix, let's say X. So, for example, Xfolder1 becomes folder1 if selected and the submit button is pressed (not on listbox change).
Here is my code and pseudocode so far. Getting string errors. Only the first sub, loading the form and populating the list works. Many thanks for any help. Health and safety to all during this pandemic.
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
For Each folder As String In System.IO.Directory.GetDirectories("D:\TestFolder\")
ListBox1.Items.Add(Path.GetFileName(folder))
Next
End Sub
This is ofc pseudocode
Private Sub RenameFolders(sender As Object, e As EventArgs) Handles Button1.Click
For i = 0 To ListBox1.Items.Count - 1
If Prefix Exists Then
FileIO.FileSystem.RenameDirectory(Prefix & FolderName, FolderName)
Else
FileIO.FileSystem.RenameDirectory(FolderName, Prefix & FolderName)
End If
Next
End Sub
What is above and lots of research. Issue might be with whether my strings are the full path or just the folder name. Listbox returns folder names, but is that what the code returns? Very confused.
Hi. Sorry for the lack of clarity. Because the listbox is populated on load, it will show the current state of the folders. This could be Xfolder1, folder2, xfolder3 etc. It will be the folder names as they currently exist.
Another way to look at it.
Selecting folders will remove any prefix from all selected when submit is hit.
Not selecting folders will add the prefix to all non-selected when submit is hit.
If xfolder1 appears in the listbox and is selected, it becomes folder1.
If xfolder1 appears in the listbox and is NOT selected, it remains xfolder1.
If folder1 appears in the listbox and it is selected, it remains folder1.
If folder1 appears in the listbox but is NOT selected, it changes to xfolder1
I hope that makes more sense?
Assuming that I'm understanding your question properly now, I would suggest that you use the DirectoryInfo class. You can create one for the parent folder and then get an array for the subfolders. You can then bind that array to the ListBox and display the Name property, which is just the folder name, while still having access to the FullName property, which is the full path. It also has an Exists property and a MoveTo method for renaming. Here is how I would do what you're asking for:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim folderPath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Test")
Dim folder As New DirectoryInfo(folderPath)
Dim subFolders = folder.GetDirectories()
With ListBox1
.DisplayMember = "Name"
.DataSource = subFolders
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Const prefix As String = "X"
For Each selectedSubFolder As DirectoryInfo In ListBox1.SelectedItems
'This is required to update the Exists property if the folder has been deleted since loading.
selectedSubFolder.Refresh()
If selectedSubFolder.Exists Then
Dim parentFolderPath = selectedSubFolder.Parent.FullName
Dim folderName = selectedSubFolder.Name
If folderName.StartsWith(prefix) Then
folderName = folderName.Substring(prefix.Length)
Else
folderName = prefix & folderName
End If
Dim folderPath = Path.Combine(parentFolderPath, folderName)
selectedSubFolder.MoveTo(folderPath)
End If
Next
End Sub
End Class
Note that this doesn't update the ListBox as it is. If you want that too, here's how I would do it with the addition of a BindingSource:
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim folderPath = Path.Combine(My.Computer.FileSystem.SpecialDirectories.MyDocuments, "Test")
Dim folder As New DirectoryInfo(folderPath)
Dim subFolders = folder.GetDirectories()
BindingSource1.DataSource = subFolders
With ListBox1
.DisplayMember = "Name"
.DataSource = BindingSource1
End With
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Const prefix As String = "X"
For Each selectedSubFolder As DirectoryInfo In ListBox1.SelectedItems
'This is required to update the Exists property if the folder has been deleted since loading.
selectedSubFolder.Refresh()
If selectedSubFolder.Exists Then
Dim parentFolderPath = selectedSubFolder.Parent.FullName
Dim folderName = selectedSubFolder.Name
If folderName.StartsWith(prefix) Then
folderName = folderName.Substring(prefix.Length)
Else
folderName = prefix & folderName
End If
Dim folderPath = Path.Combine(parentFolderPath, folderName)
selectedSubFolder.MoveTo(folderPath)
End If
Next
BindingSource1.ResetBindings(False)
End Sub
End Class
Note that this will still not resort the data but I'll leave that to you if you want it. The reason is that you could do a simple sort of the DirectoryInfo array on Name but that will do a straight alphabetic sort, which the ListBox can already do. If you want a logical sort like File Explorer does, where actual numbers in folder names are sorted numerically, then you need to use a Windows APi function too, which is beyond the scope of this question. If you want that, see here for more information.

Not read text file correct

I want when I read txt file and add in ListBox1.items, add this text http://prntscr.com/on12e0 correct text §eUltra §8[§716x§8].zip not like this http://prntscr.com/on11kv
My code
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim appDataFolder = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
My.Computer.FileSystem.CopyFile(
appDataFolder & "\.minecraft\logs\latest.log",
appDataFolder & "\.minecraft\logs\latestc.log")
Using reader As New StreamReader(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) & "\.minecraft\logs\latestc.log")
While Not reader.EndOfStream
Dim line As String = reader.ReadLine()
If line.Contains(" Reloading ResourceManager: Default,") Then
Dim lastpart As String = line.Substring(line.LastIndexOf(", ") + 1)
ListBox1.Items.Add(lastpart)
End If
End While
End Using
My.Computer.FileSystem.DeleteFile(appDataFolder & "\.minecraft\logs\latestc.log")
End Sub
This question is only different from your first question in that your have substituted a ListBox for a RichTextBox. It seems you got perfectly acceptable answers to your first question. But I will try again.
First get the path to the file. I don't know why you are copying the file so I didn't.
Add Imports System.IO to the top of your file. The you can use the File class methods. File.ReadAllLines returns an array of strings.
Next use Linq to get the items you want. Don't update the user interface on each iteration of a loop. The invisible Linq loop just adds the items to an array. Then you can update the UI once with .AddRange.
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim appDataFolder = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "\.minecraft\logs\latest.log")
Dim lines = File.ReadAllLines(appDataFolder)
Dim lstItems = (From l In lines
Where l.Contains(" Reloading ResourceManager: Default,")
Select l.Substring(l.LastIndexOf(", ") + 1)).ToArray
ListBox1.Items.AddRange(lstItems)
End Sub
If this answer and the previous 2 answer you got don't work, please lets us know why.

.NET Delete actual files from listbox

This code is intended to delete the actual files from the system when it is selected from the system:
Dim file As String()
file = System.IO.Directory.GetFiles("C:\Users\User\Desktop", "lalala.txt", IO.SearchOption.AllDirectories)
If ListBox1.SelectedIndex = -1 Then
MsgBox("No files selected")
Else
System.IO.File.Delete(ListBox1.Items(ListBox1.SelectedIndex).ToString())
ListBox1.Items.RemoveAt(ListBox1.SelectedIndex)
End If
However, only the items in the listbox are deleted. The actual file still exists. I am unsure where I should put the file into the Delete function.
I have referred to this but it has not helped me.
________UPDATE________
I have discovered where it went wrong: it is because only the file name is added to the listbox:
ListBox1.Items.Add(Path.GetFileName(fileFound))
Instead of Path.GetFullPath.
Anyhow, can I delete the file with GetFileName only?
The problem, as you realised, is that the filename only is not enough information to delete a file. You need the whole path to the file as well. So you need some way of storing the whole path but only showing the filename. This is also important because there might be two (or more) files with same name in separate directories.
A ListBox can have its Datasource property set to show items from "an object that implements the IList or IListSource interfaces, such as a DataSet or an Array."
Then you set the DisplayMember and ValueMember properties to tell it what to display and what to give as the value.
For example, I made up a class named "FileItem" which has properties for the full filename and for whatever you want to display it as, filled a list with instances of "FileItem", and told ListBox1 to display it:
Imports System.IO
Public Class Form1
Class FileItem
Property FullName As String
Property DisplayedName As String
Public Sub New(filename As String)
Me.FullName = filename
Me.DisplayedName = Path.GetFileNameWithoutExtension(filename)
End Sub
End Class
Private Sub PopulateDeletionList(dir As String, filter As String)
Dim files = Directory.EnumerateFiles(dir, filter, SearchOption.AllDirectories)
Dim fileNames = files.Select(Function(s) New FileItem(s)).ToList()
Dim bs As New BindingSource With {.DataSource = fileNames}
ListBox1.DataSource = bs
ListBox1.DisplayMember = "DisplayedName"
ListBox1.ValueMember = "FullName"
End Sub
Private Sub ListBox1_Click(sender As Object, e As EventArgs) Handles ListBox1.Click
Dim lb = DirectCast(sender, ListBox)
Dim sel = lb.SelectedIndex
If sel >= 0 Then
Dim fileToDelete = CStr(lb.SelectedValue)
Dim choice = MessageBox.Show("Do you really want to delete " & fileToDelete, "Confirm file delete", MessageBoxButtons.YesNo, MessageBoxIcon.Question)
If choice = DialogResult.Yes Then
Try
File.Delete(fileToDelete)
lb.DataSource.RemoveAt(sel)
Catch ex As Exception
MessageBox.Show("Could not delete " & fileToDelete & " because " & ex.Message)
End Try
End If
End If
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
PopulateDeletionList("C:\temp", "*.txt")
End Sub
End Class
Edited I had forgotten to delete the item from the ListBox. To do that, it needs to be tied to the DataSource through a BindingSource.
Extra feature Seeing as there could be more than one file with the same name, you might want to add a tooltip to the listbox items so that you can see which directory it is in. See how to add tooltips on winform list box items for an implementation which needs only minor adjustments to work, such as:
Dim toolTip As ToolTip = New ToolTip()
' ...
Private Sub ListBox1_MouseMove(sender As Object, e As MouseEventArgs) Handles ListBox1.MouseMove
Dim lb = DirectCast(sender, ListBox)
Dim index As Integer = lb.IndexFromPoint(e.Location)
If (index >= 0 AndAlso index < ListBox1.Items.Count) Then
Dim desiredTooltip = DirectCast(lb.Items(index), FileItem).FullName
If (toolTip.GetToolTip(lb) <> desiredTooltip) Then
toolTip.SetToolTip(lb, desiredTooltip)
End If
End If
End Sub
The most simple (and reliable) solution would be to create a custom data type and add that to the ListBox instead.
By overriding the ToString() method you can make it display only the file name, while the back-end object still contains the full path.
Public Structure FileEntry
Public FullPath As String 'A variable holding the full path to the file.
'Overriding the ToString() method, making it only return the file name.
Public Overrides Function ToString() As String
Return System.IO.Path.GetFileName(Me.FullPath)
End Function
Public Sub New(ByVal Path As String)
Me.FullPath = Path
End Sub
End Structure
Now whenever you want to add paths to the ListBox you've got to add a new instance of the FileEntry structure, instead of a regular string:
ListBox1.Items.Add(New FileEntry(fileFound))
And to delete you just cast the currently selected item into a FileEntry, and then pass its FullPath onto the File.Delete() method.
Dim Entry As FileEntry = DirectCast(ListBox1.Items(ListBox1.SelectedIndex), FileEntry)
System.IO.File.Delete(Entry.FullPath)
NOTE: For this to work every item in the list box must be a FileEntry.
Online test: https://dotnetfiddle.net/x2FuV3 (pardon the formatting, DotNetFiddle doesn't work very well on a cellphone)
Documentation:
How to: Declare a Structure (Visual Basic) - Microsoft Docs
Overriding the Object.ToString() method - MSDN
You can use Path.Combine.
Since you are going to search in C:\Users\User\Desktop, you can do this to delete:
System.IO.File.Delete(Path.COmbine("C:\Users\User\Desktop",ListBox1.Items(ListBox1.SelectedIndex).ToString())
Here, "C:\Users\User\Desktop" and the selected index's text will be combined to make a single path.
Edit:
I get it, you want to show the file name onlyy in the textbox but want to delete the file from the system too but can't do it, right?
Well you can do this:
Put two listbox and while you add a file to a listbox1, put it's path to the listbox2 whose visibility will be False, meaning it won't be shown in the runtime.
DOing this, while an item is selected in the listbox1, use the path.combine to make a path by adding the filename & path from the list with same index number.
Something like this:
System.IO.File.Delete(path.combine(ListBox1.Items(ListBox1.SelectedIndex).ToString(), ListBox2.Items(ListBox1.SelectedIndex).ToString())

Error with VB ParseFileText method

When I run this I made a dummy "Button1" to test populate the fields they will successfully fill out the texts boxes. How ever I will have it parse that file every minute, and when I do it again I get the error shown below. By adding the routine "DisplayForm_Load" to the button1_click even it would work fine.
My question is I'm pretty sure I shouldn't have to redefine this every time? I think I'm not setting the index back to 0 or something along those lines. From what I've been able to understand from MS website is its like its indexing things in the array that don't exist.
Error received:
An unhandled exception of type 'System.IndexOutOfRangeException' occurred in WindowsApplication4.exe
Imports Microsoft.VisualBasic.FileIO
Public Class Form1
Private Directory As String ' Used to hold the folder directory to push/pull data from.
Private FileParser As Microsoft.VisualBasic.FileIO.TextFieldParser
Private Sub PushButton_Click(ByVal sender As Object, ByVal e As EventArgs) Handles PushButton.Click
' Sends information to txt file.
' This bit works fine, just writes code to txt file that can be parsed below.
End Sub
Sub DefineTextFieldParse_Load() Handles MyBase.Load
' Instantiate teh TextFieldParser and the set the delimiter
Dim FileName As String = "C:\Users\Caleb\Documents\TestDoc.txt"
Try
FileParser = New FileIO.TextFieldParser(FileName) '' Selects File to Parse
FileParser.TextFieldType = FieldType.Delimited
FileParser.SetDelimiters(",")
Catch ex As Exception
' Errors
MessageBox.Show("Unable to read the file" & "," & FileName)
End Try
End Sub
Sub UpdateCheck()
' Checks share txt file for update.
Dim FileName As String = "C:\Users\<Me>\Documents\TestDoc.txt"
Dim FieldString() As String
'Read the file
If Not FileParser.EndOfData Then
FieldString = FileParser.ReadFields()
' 1st Field
NIS1TextBox.Text = FieldString(0)
' 2nd Field
NIS2TextBox.Text = FieldString(1)
' You get the idea...All Testboxes identified above in the write section
' Repeats 12 more times...
EODTextBox.Text = FieldString(14)
InfoRichTextBox.Text = FieldString.LastOrDefault()
End If
End Sub
Sub PushUpdate()
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
'DefineTextFieldParse_Load()_Load() ' When Enabled code works fine when commented out generates alert.
UpdateCheck()
End Sub
End Class
mchihinney - I did review https://msdn.microsoft.com/en-us/library/hks5e2k6.aspx
However what I found was the issue was with the formatting of the text file. The problem I found was with the text file there was a second line which was blank, so when it tried to parse nothing it through the error. Deleting the second line from the .txt file and worked as expected.

Autocomplete dictionary from a RichTextBox

While this code works fine in my RichTextBox, how do you do it from a dictionary in VB?
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
'AutoComplete.Add("do")
AutoComplete.Add("double")
AutoComplete.Add("Apple")
AutoComplete.Add("Car")
AutoComplete.Add("Table")
AutoComplete.Add("Plate")
How do you do this from a dictionary in VB?
So this isn't an actual .NET dictionary? Ambiguous title!
Assuming each line is a separate word to populate your 'dictionary':
Public Sub PopulateDict()
For Each word As String In File.ReadAllLines("path")
AutoComplete.Add(word)
Next
End Sub
Something like this, yes?
Simple, you just need to read in the dictionary text file, parse the words from it, and then add them to your ArrayList.
Here's an example:
Private AutoComplete As New ArrayList
Private Sub AddDictionary()
Try
' Create an instance of StreamReader to read from a file.
Using sr As StreamReader = New StreamReader("dictionary.txt")
Dim line As String
Dim pieces As String()
' Read every line in the file
' Split the line by the delimiter
' Add the individual pieces to the AutoComplete ArrayList
Do
line = sr.ReadLine()
pieces = line.Split(" ")
For Each piece In pieces
AutoComplete.Add(piece)
Next
Loop Until line Is Nothing
' Close the dictionary file StreamReader
sr.Close()
End Using
Catch E As Exception
' Let the user know what went wrong.
MsgBox("The dictionary file could not be read:\n" & E.message, _
MsgBoxStyle.Critical, _
"Error loading file!")
End Try
End Sub
Note: I don't know how your dictionary file is formatted, so in my example code I'm just splitting the lines into individual words using spaces.
You could call the AddDictionary() sub from your Form1_Load() method to add the words to the AutoComplete dictionary at startup.