copy folder path to listbox - vb.net

I manage to drag and drop multiple folder path to listbox, is it possible to do this using copy/paste, for example, you copy multiple folder on windows explorer then paste those folder path on the listbox using contextmenu, shortcut keys, or a button..
Private Sub lstFolder_DragDrop(sender As Object, e As Windows.Forms.DragEventArgs) Handles lstFolder.DragDrop
Dim directories As String() = DirectCast(e.Data.GetData(DataFormats.FileDrop), String())
For Each folder As String In From folders In directories Where Directory.Exists(folders)
If Not lstFolder.Items.Contains(folder.ToString()) Then
lstFolder.Items.Add(folder.ToString())
End If
Next
End Sub
Private Shared Sub lstFolder_DragEnter(sender As Object, e As Windows.Forms.DragEventArgs) Handles lstFolder.DragEnter
If e.Data.GetDataPresent(DataFormats.FileDrop, False) = True Then
e.Effect = DragDropEffects.All
End If
End Sub
# Vignesh Kumar
works great, one question how about, copying the folder location from a document file or from address bar, here is my code so far.
Dim directories As String() = CType(Clipboard.GetData(Windows.Forms.DataFormats.FileDrop), String())
'loop through the string array, check if folder exist then adding each folder to the ListBox
For Each folder As String In From folders In directories Where Directory.Exists(folders)
If Not lstFolder.Items.Contains(folder.ToString()) Then
lstFolder.Items.Add(folder.ToString())
End If
Next

Yes. Use Clipboard object
string[] files = (string[])Clipboard.GetData(System.Windows.Forms.DataFormats.FileDrop);
Files or/and folders will be in this string array.

Related

How can I move a certain file extension into one folder with VB

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

How to fix copied folder name

I have a vb application which copies folders and its subfolder that is working properly. My problem is that it is not copying the correct folder name of the folder being copied.
Like if I copy the folder with this location: C:\Users\Documents\Sample_Folder
The output copied folder name would be "Documents".
C:\Users\Documents\Sample_Folder\Sample_Folder_2
The output copied folder name would be "Sample_Folder".
Private Sub btnCopy_Click(sender As Object, e As EventArgs) Handles btnCopy.Click
Dim SourcePath As String = txtBrowse.Text
Dim DestinationPath As String = "C:\Users\1000258123\Desktop\NEW"
Dim newDirectory As String =
System.IO.Path.Combine(DestinationPath,
Path.GetFileName(Path.GetDirectoryName(SourcePath)))
If Not (Directory.Exists(newDirectory)) Then
Directory.CreateDirectory(newDirectory)
End If
Microsoft.VisualBasic.FileIO.FileSystem.CopyDirectory(SourcePath, newDirectory)
MsgBox("Copy Successful")
End Sub

Dir folder and subfolder shows result in one folder, but not other folder

Just trying to learn VB.net.
Making something that lists all files an folders in folder and subfolder.
Got a testfolder in root C:\ with a 2 subfolders and som files in al folders.
On execution listbox is filled with al files an folders including subfolders an files in subfolders.
But..
If id choose a folder on G:\ things get strange, and I only get A few folders or files listed
This is my first question here,so if if screw up in telling you, I am sorry
Public Class Form1
Dim R As IO.StreamReader
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
ListBox1.Items.Clear()
Me.FolderBrowserDialog1.ShowDialog()
Listfiles(Me.FolderBrowserDialog1.SelectedPath)
End Sub
Public Sub Listfiles(ByVal Pad As String)
Dim DirInfo As New IO.DirectoryInfo(Pad)
Dim FileObject As IO.FileSystemInfo
Dim strBESTAND As String
For Each FileObject In DirInfo.GetFileSystemInfos
'if FileObject is a folder
If FileObject.Attributes = IO.FileAttributes.Directory Then '
Listfiles(FileObject.FullName)
Me.ListBox1.Items.Add(FileObject.FullName)
Else
strBESTAND = (FileObject.FullName)
Dim information = My.Computer.FileSystem.GetFileInfo(strBESTAND)
' If extention matches ..........
Dim strEXTENTIE As String
'if extentie is tikt in checkedlistbox
For i As Integer = 0 To (CheckedListBoxEXTENTIES.CheckedItems.Count - 1) ' iterate on checked items
'only us ticked items
strEXTENTIE = ((CheckedListBoxEXTENTIES.GetItemText(CheckedListBoxEXTENTIES.CheckedItems(i)).ToString))
If information.Extension = "." & strEXTENTIE Then
strBESTAND = information.Name
Me.ListBox1.Items.Add(FileObject.Name)
End If
Next
End If
Next
MessageBox.Show("Done!")
End Sub
The string comparisons are case sensitive by default. You will miss extensions having another case as in the CheckedListBox. Use
If String.Compare(information.Extension, "." & strEXTENTIE, _
StringComparison.OrdinalIgnoreCase) = 0 Then
But it would be more efficient if you prepared the extensions before browsing the folders
'Outside of subroutines
Dim extensions As New HashSet(Of String)()
'In Button1_Click before calling Listfiles
For i As Integer = 0 To CheckedListBoxEXTENTIES.CheckedItems.Count - 1
extensions.Add("." & _
CheckedListBoxEXTENTIES.CheckedItems(i).ToString().ToLowerInvariant())
Next
Then you can check the extensions like this, without having to loop through the CheckedListBox for each file.
If extensions.Contains(information.Extension.ToLowerInvariant()) Then

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.

Creating an application that moves a collection files from one place to another using VB

So this is an application I have to create that moves files from one directory to another, both directories being specified by the user. It seems easy enough, but the problem is that there are millions of files that have to be searched through. These files are all named with 8 digits for example, "98938495.crt". The directory where all these files are has multiple folders. These folders within the main one are named with the first two digits of all the files that are in the folder. And then in that folder there are roughly ten zipped folders that contain 100,000 files each. The name of those folders are the minimum and maximum names of the files. For example, I go into the main folder, then click on the "90xx" folder. In that one there are 10 zipped folders which are named with the minimum and maximum names of the files, like "90000000_90099999.zip". That zip folder contains 100000 files. Now, this app is supposed to find all the files that the user inputs and then move them to a folder specified by the user. I have posted my code so far below, any help at all is greatly appreciate!!
FYI: The STID is the name of the file.
GUI for the app
Edit: I realized there is no way to answer this question because there really isn't a question, just a broken app. Basically, how do i search for the items in the directory and then copy them into a new directory?
Imports System
Imports System.IO
Public Class CertFinder
Private Sub SourceDirectoryTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SourceDirectoryTB.TextChanged
End Sub
Private Sub DestinationDirectoryTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DestinationDirectoryTB.TextChanged
End Sub
Private Sub SearchTB_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchTB.TextChanged
End Sub
Private Sub SourceButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SourceButton.Click
Dim fbd As New FolderBrowserDialog
fbd.RootFolder = Environment.SpecialFolder.MyComputer
If fbd.ShowDialog = DialogResult.OK Then
SourceDirectoryTB.Text = fbd.SelectedPath
End If
End Sub
Private Sub DestinationButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DestinationButton.Click
Dim fbd As New FolderBrowserDialog
fbd.RootFolder = Environment.SpecialFolder.MyComputer
If fbd.ShowDialog = DialogResult.OK Then
DestinationDirectoryTB.Text = fbd.SelectedPath
End If
End Sub
Private Sub SearchButton_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SearchButton.Click
'Array of stids
Dim stidsToFind As String()
'get text in the searchTB textbox
Dim searchTBText As String = Me.SearchTB.Text.Trim()
'splits stids into seperate lines
stidsToFind = searchTBText.Split(vbCrLf)
'gets text from source directory text box
Dim sourceDirectory As String = Me.SourceDirectoryTB.Text.Trim()
'gets text from destination directory text box
Dim destinationDirectory As String = Me.DestinationDirectoryTB.Text.Trim()
Dim fullPathToFile As String = sourceDirectory
'Go through each stid in the search text box and continue if nothing
For Each stidToFind As String In stidsToFind
If String.IsNullOrWhiteSpace(stidToFind) Then
Continue For
End If
'Find the first two digits of the stid
Dim firstTwoDigitsOfSTID As String = stidToFind.Substring(0, 2)
'In the specified directory, find the folder with the first two digits and "xx"
fullPathToFile = fullPathToFile & "\" & firstTwoDigitsOfSTID & "xx"
Dim allFileNames As String() = Nothing
allFileNames = Directory.GetFiles(sourceDirectory, "*.crt*", SearchOption.AllDirectories)
Next
'-------------------------------------------------------------------------------
Try
If File.Exists(fullPathToFile) = False Then
Dim FS As FileStream = File.Create(fullPathToFile)
FS.Close()
End If
File.Move(fullPathToFile, destinationDirectory)
Console.WriteLine("{0} moved to {1}", fullPathToFile, destinationDirectory)
Catch ex As Exception
MessageBox.Show("File does not exist")
End Try
Dim sc As New Shell32.Shell()
'Declare the folder where the files will be extracted
Dim output As Shell32.Folder = sc.NameSpace(destinationDirectory)
'Declare your input zip file as folder .
Dim input As Shell32.Folder = sc.NameSpace(stidsToFind)
'Extract the files from the zip file using the CopyHere command .
output.CopyHere(input.Items, 4)
'-------------------------------------------------------------------------------
' 1. Get the names of each .zip file in the folder.
' 2. Assume that the .zip files are named as such <minimum STID>_<maximum STID>.zip
' 3. For each .zip file name, get the Minimum STID and Maximum STID values.
' 4. Check the range of 'stidToFind' against the STID ranges of each .zip file.
' 5. Once the .zip file is located,
' a. Copy the .zip file to the local computer OR
' b. Just leave it where it is.
' 6. Unzip the file.
' 7. Create the full file name path. ../<stidToFind>.crt
' 8. Copy the file to the destination directory.
' 9. Delete the unzipped folder
' 10. Delete the .zip file (IF you've copied it to your local computer).
End Sub
End Class
In answer to the .ZIP unpacking, use the System.IO.Packaging (more can be found at CodeProject - Zip Files Easy)
Don't you think using a database would be easier to store that data?