get directory name of directory containing directory - vb.net

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

Related

How can i get short path from folder directory Vb.Net

i have some path's that i get from folder when i drop in my program but i want to start the path from where the program located.
im putting the program inside the desktop and drag and drop my folder call "Folder" into my program and getting full path and i want get only from where the program is located.
for example:
what i want:
tools\test.exe
tools\test2\test.exe
normal:
C:\Users\xxx\Desktop\Folder\tools\test.exe
C:\Users\xxx\Desktop\Folder\tools\test2\test.exe
my code:
Dim files() As String = e.Data.GetData(DataFormats.FileDrop)
For Each path In files
For Each Dir As String In IO.Directory.GetDirectories(path)
ListBox1.Items.Add(path)
Next
Next
This should work:
Dim relativePath = fullPath.Substring(Application.StartupPath.Length + 1)
The 1 is added to remove the slash that I don't think is included in Application.StartupPath.
Try below line of code:
Dim relpath=System.IO.Directory.GetCurrentDirectory().Replace(System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory()).Parent.Parent.FullName,"")
This should also work:
Dim relPath = fullPath.Substring(Directory.GetCurrentDirectory.Length + 1)

Parse through all files from all the subfolders of a folder

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?

replace text multiple file vb.net

I want multiple files with a name and in a different format
I will replace some of the text
To do this, use the following command:
Dim sb As New StringBuilder(File.ReadAllText("tmp\boot_root\initrd\fstab.*"))
sb.Replace("ro,barrier", "ro,noatime,barrier")
sb.Replace("ro,errors", "ro,noatime,errors")
But this does not work. I need a better command to do this
help please
To get a list of all files matching a pattern in a given directory use Directory.Getfiles(path, pattern). Example:
Dim list as String()
Dim path as String = "C:\tmp\"
Dim filename as String = "fstab"
list = Directory.GetFiles(path, filename & ".*")
For Each file As String In list
'Logic goes here.
Next

Get count of similar files in a folder

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

VB.net Return String with Path of folder

My program needs to read an XML file that a software vendor sent me in order to complete a process. The problem is I cannot tell the program where the file is locate!
When i publish the program and install the program it generates a random folder every time it installs
Location the same folder name is always different
C:\Users\Ray\AppData\Local\Apps\2.0\6ZNVVG8V.C6O\0MELQPL9.LCB\lol-..tion_531c8308fa0ff83d_0001.0000_5a2aee0cd0a667c1
I Have figured out how to get that folder to show by doing this
Dim resourcePath As String = _
System.IO.Path.GetFullPath(My.Resources.ResourceManager.BaseName)
Dim rIndex As Integer = resourcePath.LastIndexOf("\")
resourcePath = resourcePath.Substring(0, rIndex)
Dim filePath As String = System.IO.Path.Combine(resourcePath, "Client.xml")
But the program creates a second folder that it puts the XML and the ICOn file in thats randomly generated but in the same directory.
How do I get the program to look in that folder for the xml?
PLEASE HELP ME !
Ray
You could do this:
Dim query = _
From d In System.IO.Directory.GetDirectories(resourcePath)
Let f = New FileInfo(System.IO.Path.Combine(d, "Client.xml"))
Where f.Exists
select f.FullName
Dim filePath = query.FirstOrDefault()
get list of all files by filter
lblPaymentMode.Location = New Point(lblDate.Right - lblPaymentMode.Width, lblPaymentMode.Location.Y)
Dim mFiles() As String = Directory.GetFiles("Path of folder", "*.xml", SearchOption.AllDirectories)
For i As Integer = 0 To mFiles.Count - 1
Debug.Print(mFiles(i)) 'print name and path of of each file
Next