How can I move a certain file extension into one folder with VB - vb.net

I am new to VB and would like to create a software that moves a certain file extension into a single folder. I have already built the code that creates a folder on the desktop when clicking the button although after that runs I need to compile a certain file such as (.png) into the folder in created.
This code creates two buttons that when pressed creates a folder called "Pictures" and "Shortcuts".
How would I go about moving all .png files from the desktop into the pictures folder?
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Pictures")
MessageBox.Show("Pictures Compiled And Cleaned")
End Sub
Private Sub Button1_Click_1(sender As Object, e As EventArgs) Handles Button1.Click
My.Computer.FileSystem.CreateDirectory(
"C:\Users\bj\Desktop\Shortcuts")
MessageBox.Show("Shortcuts Compiled And Cleaned")
End Sub
End Class

We'll start simple. This command will generate an array of all the PNG files' paths on the desktop
Dim filePaths = Io.Directory.GetFiles("C:\Users\bj\Desktop\", "*.png")
We can loop through this array and act on each filepath:
For Each filePath in filePaths
Dim filename = Io.Path.GetFilename(filepath)
Dim newPath = IO.Path.Combine("C:\Users\bj\Desktop\Pictures", filename)
IO.File.Move(filePath, newPath)
Next filePath
We have to pull the filename off the path and put it into a new path, then move from old to new. This I also how you rename files; have a new name in the same folder and use Move. Always use the Path class to cut and combine file paths

Related

How to overwrite a file in specific folder with a file in my program's resources

I have a file named "something.a binary extension" "not a txt extension" in my program's resources and the same file exists in another folder and named "Something.a binary extension" too.
I want to copy my resources file and replace the existing file with it.
My program has 2 button controls, one for browsing to get the path from OpenFileDialog1 and the other to copy my resources file and replace the existing file "Which have the same name" by it, and 1 TextBox to display the path.
My program has a manifest file to run as Adminstrator with level="highestAvailable.
I use this code:
Public Class Form1
Dim Path_of_folder As String
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles btnBrowse.Click
'getting the path
OpenFileDialog1.ValidateNames = False
OpenFileDialog1.CheckFileExists = False
OpenFileDialog1.CheckPathExists = True
OpenFileDialog1.FileName = "somthing.exe"
OpenFileDialog1.ShowDialog()
TextBox1.Text = System.IO.Path.GetFullPath(OpenFileDialog1.FileName)
TextBox1.Text = TextBox1.Text.Replace("somthing.exe", "").Trim()
Path_of_folder = TextBox1.Text 'we got the path
End Sub
Private Sub BtnCopy_Click(sender As Object, e As EventArgs) Handles BtnCopy.Click
FileCopy(My.Resources.somthing, Path_of_folder) ' this got Error when i use it, but the program can Run
End Sub
End Class

How to get a file from Pastebin.com and saving the file on computer in VB.NET

How to get a file from the Pastebin.com and the save the file to the system. In the interface there is a textbox, where a user can paste a link to a pastebin file https://pastebin.com/raw/*a-random-code*, there will be a button to start fetching the file, once the program fetches the file, the file is saved on to the computer as a downloadedtext.txt file.
I found an alternate method very easy.
Private Sub frm1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Dim filename As String
filename = "C:\Users\sample.txt"
My.Computer.Network.DownloadFile("https://pastebin.com/raw/*enter code*", filename)
End Sub

CopyFile isn't copying my file with the same extension?

I am trying to make it so my user can copy files from one folder to another folder, their playlist folder, so that they can use it throughout my program. So I tried this:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim result As DialogResult = MessageBox.Show("Are you sure you want to finish the playlist?", "Finish Playlist- WikiFinder", MessageBoxButtons.YesNo)
If (result = DialogResult.Yes) Then
For Each Item In ListBox1.Items
Dim str As String = IO.Path.Combine(MusicMenu.FolderBrowserDialog2.SelectedPath, "DONUTS")
My.Computer.FileSystem.CopyFile(Item.ToString(), str)
Next
Else
End If
End Sub
This works and makes the file, but the issue is I told it to copy an MP3 file and it just gave me a "File". Is there any way I can copy the file AND keep the original file's extension?
Since you pass only the directory to the CopyFile function, it creates a FILE.
Pass filename with Extension.
For Each Item In ListBox1.Items
Dim str As String = IO.Path.Combine(MusicMenu.FolderBrowserDialog2.SelectedPath, "DONUTS")
str = IO.Path.Combine(str,IO.Path.GetFileName(Item.ToString()))
My.Computer.FileSystem.CopyFile(Item.ToString(), str)
Next
Now the files will be copied to your DONUTS folder.

The system cannot find the reference specified for an assembly

I have a VB.NET solution which have a few projects and a 'References' folder in the root directory.
Now I want to add a new dll reference to one of the projects from this References folder but getting the following message:
The system cannot find the reference specified
How can this be solved?
I encountered this many times. Here I was trying to execute a jar file when a picturebox was clicked. I added my jar to my resources :
Private Sub PictureBox9_Click(sender As Object, e As System.EventArgs) Handles _PictureBox9.Click
Dim dir As String = My.Computer.FileSystem.SpecialDirectories.Temp
Dim filename As String = dir + "minecraft.jar" 'Look Below
IO.File.WriteAllBytes(filename, My.Resources.minecraft)
Process.Start(My.Computer.FileSystem.SpecialDirectories.Temp & "minecraft.jar")
End Sub
The following worked for other programs though, but not minecraft, :
Private Sub Panel9_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Panel9.Click
proc = Process.Start("vbexpress.exe", "")
End Sub
*proc was a New System.Diagnostics.Process()
But for actual files, you have manually lure them out of their location.
So I did Me.Resources.1234.png which saved me writing the directory. But this still did not work with minecraft, so I kept that long paragraph about in place.

Release file\folder lock when using drag\drop onto control

I'm trying to loop through a list of files to get the path and filename.
These files are dragged onto a datagrid:
Private Sub DataGridView1_DragDrop(ByVal sender As System.Object,
ByVal e As System.Windows.Forms.DragEventArgs) Handles DataGridView1.DragDrop
Dim filenames As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
For Each File In filenames
If Array.IndexOf(SupportedFormats, System.IO.Path.GetExtension(File)) <> -1 Then
Frm = New FormRestore(ServerName, File)
Frm.Show()
While Frm.Visible
Application.DoEvents()
End While
End If
Next
End Sub
A child form is created that processes an action based on the path and file name.
until the loop is complete, the folder the files were dragged from is locked.
How would I get a list of the path and file names AND process each one without locking out the source folder?
(I'm using the while loop to process the file names sequentially, pausing between each one while maintaining UI response)
Thanks.
Try processing the files after the drag&drop event by calling BeginInvoke in the handler.