I want to parse through all the files from the subfolders of a folder. To be more precise, I want to check if any of the files in any folder is the same as a string already stored in a variable. I have the following code:
Const fpath2 As String = "C:\ProjectArtist\"
Dim folders() As String = IO.Directory.GetFiles("C:\ProjectArtist\")
For Each folder As String In folders
%% check file name if it's same with variable
Next
How do I modify it in order to do the same for all the subfolders in fpath2?
Related
I want to delete all the files contained in a folder. The below code does delete the file but also deletes the folder itself. I am aware you will need to perform a for loop to remove each item from the folder, but can't find the information on how to start the code for it. Can someone point me in the correct direction.
Dim folderFiles() As String
folderFiles= Directory.GetFileSystemEntries("C:\New Folder")
For Each element As String In files
If (Not Directory.Exists(folder)) Then
File.Delete(Path.Combine("C:\New Folder", Path.GetFileName(folder)))
End If
Next
This is the easier way :
For Each the_file As String In Directory.GetFileSystemEntries("C:\New folder")
File.Delete(the_file)
Next
Don't bother to grab the list of files and then browse it, use your loop directly on it.
I have a function for that. With it, you can also delete all files of a pattern even for all subdirectories:
Public Sub DeleteFiles(Path As String,
Optional Pattern As String = "*.*",
Optional All As Boolean = False)
Dim SO As IO.SearchOption = If(All, IO.SearchOption.AllDirectories, IO.SearchOption.TopDirectoryOnly)
For Each Filename As String In Directory.GetFiles(Path, Pattern, SO)
File.Delete(Filename)
Next
End Sub
So you could use it for your task like:
DeleteFiles("C:\New Folder")
I would use this code
For Each file As String In IO.Directory.GetFiles("the path")
IO.File.Delete(file)
Next
This deletes everything in the folder but not the folder itself.
This is quick and easy, especially if 1) there's nothing special about the folder, eg permissions, and 2) the folder might contain sub-folders, sub-sub-folders, etc.
Simply delete the original folder and recreate it:
Dim path As String = {your path}
IO.Directory.Delete(path, recursive:=True)
IO.Directory.CreateDirectory(path)
Another option for folders with sub-folders where you wish to keep the (empty) sub-folders, is to use recursion:
Sub EmptyFolder(path As String) As Boolean
Try
For Each dir As String In IO.Directory.GetDirectories(path)
EmptyFolder(dir)
Next dir
For Each file As String In IO.Directory.GetFiles(path)
IO.File.Delete(file)
Next file
Catch
Throw
End Try
End Function
I have a file, lets call it "myFile.txt", in a folder. There are also going to be other files named "myFile1.txt, myFile2.txt, myFile3.txt, myFile4.txt" in that same folder and so forth. I want to check that folder and count how many times "myFile" shows up in that folder no matter what the extension or the number after "myFile". Here's what I have so far, but it's not getting what I want:
Dim MyFiles1() As String = IO.Directory.GetFiles("filepath", "myFile.txt")
I whipped this up and tested it in Visual Studio:
'Get a list of files from a directory you designate
Dim counter As System.Collections.ObjectModel.ReadOnlyCollection(Of String)
counter = My.Computer.FileSystem.GetFiles("C:\YOURDIR")
'Declare your file name
Dim myfile As String = "YOURFILE"
'Create count dim
Dim Occurrences As Integer = 0
'Check all files in the array and check them against our filename
For Each File As String In counter
If File.Contains(myfile) Then
'If a file is found, add +1
Occurrences = Occurrences + 1
End If
Next
'Display total count
MsgBox("number of files is " & Occurrences)
This will go and search the path you designate. It will check all files like your filename in the dir. If it finds one, it will count it. This also checks for case insensitive names as well so you can get all variants of your file name.
Something like below:
Public Shared Sub ProcessDirectory(ByVal targetDirectory As String)
Dim fileEntries As String() = Directory.GetFiles(targetDirectory)
' Process the list of files found in the directory.
Dim fileName As String
For Each fileName In fileEntries
' do a simple if statement to see if the file name contains "myFile"
' if so add to some count variable you declare
See here for more reference material on the GetFiles() method (which is the key to solving your issue): https://msdn.microsoft.com/en-us/library/07wt70x2%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-1
Textbox1.Text = part
'searching the folder with key word from Textbox1'
' Only get files that contain the keyword stored in 'part' string
Dim dirs As String() = Directory.GetFiles("d:\data\", "*$part*")
'display the result
Dim dir As String
For Each dir In dirs
Listbox1.Items.Add(dir)
Next
I can't get it to search the folder for the files that contain the keyword in their name. The keyword is stored in the 'part' variable.
I believe you want to do something like:
Dim dirs As String() = Directory.GetFiles("d:\data\", "*" & part & "*")
This will build the string for the filter based on the part variable.
This is a one-liner:
Listbox1.Items.AddRange(Directory.GetFiles("D:\data\", $"*{Textbox1.Text}*"))
How to get the folder name from the full path of folder?
This is file path,
"c:\projects\roott\wsdlproj\devlop\beta2\text"
Here text is the folder name.
But i want to get the folder containing text, that is beta2
The Path.GetDirectoryName method can be used to return "c:\projects\roott\wsdlproj\devlop\beta2", as shown below:
Dim filePath As String = "c:\projects\roott\wsdlproj\devlop\beta2\text"
Dim directory As String = Path.GetDirectoryName(filePath)
To get just the name of the parent folder, "beta2", you can split the input and take the second last entry, given that the input is indeed accurate:
Dim split As String() = filePath.Split("\")
Dim parentFolder As String = split(split.Length - 2)
Fri 7/09/2012 10:42 AM
io.path.getFileName(filePath) will return the folder name
How do I get a file's directory when open with or send to event happens?
I want it like media player, the application can get the file name & path when the user runs it with Open With.
The filename will be pased on the command line to your application, so you can access it by either
Module MainModule
Sub Main(cmdArgs As String())
Dim fileName as string = cmdArgs(0)
End Sub
End Module
For a console application, or by
Dim args As String() = Environment.GetCommandLineArgs()
Dim fileName As String = args(1)
For a GUI application.
Note that when using Environment.GetCommandLineArgs(), the first element in the array will be the full path to your executable, so you need to access the second element to get the file name. In the first example, cmdArgs will only contain the file name parameter.
To get the directory name containing the file, you can use
Dim path as String = System.IO.Path.GetDirectoryName(fileName)