There is a one folder located in a directory, lets say its in
C:\Users\User\AppData\Roaming\Mozilla\Firefox\Profiles\
it's different on everyuser, for example 1rituum9.default named folder.
Tried this without any luck, it messages empty.
Dim filepath As String = "%Appdata%\Mozilla\Firefox\Profiles\"
Dim fi As New IO.FileInfo(filepath)
MessageBox.Show(fi.Name)
What is the correct way to get the folder name in specified directory ?
You can try this:
Dim filepath As String = Environment.GetEnvironmentVariable("appdata") & "\Mozilla\Firefox\Profiles\"
Dim di As New IO.DirectoryInfo(filepath)
MessageBox.Show(di.GetDirectories()(0).Name)
if there are more than one dirs u might need something like:
For Each Dir As IO.DirectoryInfo In di.GetDirectories()
ListBox1.Items.Add(Dir)
Next
Edit: Fixed Code Line 1 - see comments
Related
I am trying to find a way to read a specific file name in a directory and then put the file name into a variable. In my program I have a batch file that zips a folder called logs and then changes the name of zip to username_date_time.zip.
So basically if the filename was jobs_03152015_1315.zip I would want the entire file name but not the path stored into a variable. The file starts off on the user's local machine. It is then uploaded into a network share.
The network path will be uploaded to a database for others to view. I want to just add the unique file name to the end of a pre set path. Here is the code I am using.
Dim filePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(filePath)) Then
For Each file As String In filePath
If file.Contains(".zip") Then
Dim zip As String = file
testbox.Text = zip
Exit For
End If
Next
End If
You can enumerate all files using the GetFiles() method of the static (shared in VB) Directory class, see msdn.
Assuming you have more then 1 file I add the files to a listbox control. But you could change that if you wish. I get the filename using the Path class. You will find a lot other helpful functions in this class.
Dim searchPath = "C:\temp\logs"
Dim files = Directory.GetFiles(path, "*.zip")
For Each file In files
listbox1.Items.Add(path.GetFileName(file))
Next
This should do it.
Your loop doesn't read anything. The string variable filePath is just the name of the directory, not a list of the files in that directory. Calling For Each over a string simply enumerates each character contained in the string
To get a list of files contained in that folder you need Directory.EnumerateFiles() and pass the filePath variable and the extension required.
It seems that you are interested only to find if that folder contains at least one file with zip extension. If this is the case then you could remove the explicit loop and write simply
Dim file = Directory.EnumerateFiles(filePath, "*.zip").FirstOrDefault()
If file IsNot Nothing Then
testbox.Text = Path.GetFileName(file)
End If
Using Path.GetFileName will return just the file without the path part
Try this code :
Dim FilePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(FilePath)) Then
For Each File As String In System.IO.Directory.EnumerateFiles(FilePath)
If File.Contains(".zip") Then
Dim info As New System.IO.FileInfo(File)
zip = info.Name
testbox.Text = zip
Exit For
End If
Next
End If
I'm trying to pull back a list of directories that sit inside a dated folder structure. Within each dated folder could be a number of 'Jobs' however i only want to return the name of the 1st level of folders
The below code gets to the right level of folder detail however the result displays the full path
For Each Dir As String In System.IO.Directory.GetDirectories("c:\Working")
Dim dirInfo As New System.IO.DirectoryInfo(Dir)
For Each sDir As String In System.IO.Directory.GetDirectories(dirInfo.ToString)
Dim sdirInfo As New System.IO.DirectoryInfo(sDir)
chkImpExp.Items.Add(sDir)
Next
Next
This would display the following
However i would just like to display the directory name to the right of the 3rd backslash (Westdale - 28023 - Cash+Spirit for example)
Hopefully this is enough information.
Many thanks
' renamed Dir to d as Dir() is already a function in Microsoft.VisualBasic
For Each d In System.IO.Directory.GetDirectories("c:\Working")
For Each sDir In System.IO.Directory.GetDirectories(d)
Dim di = New DirectoryInfo(sDir)
chkImpExp.Items.Add(di.Name())
Next
Next
Try:
chkImpExp.Items.Add(sdirInfo.Name)
Ok, here is my story:
I am building a fileviewer, and i am trying to delete the selected file in the listview.
when i try to delete it, it gave me an error saying the file wasnt found. I looked at my desktop and the file was there. here is the original code:
dim f as string = lv1.focuseditem.text
my.computer.filesystem.deletfile(f)
lv1.update()
this gave me that error. My updated code is supposed to show me where the computer thinks my file is:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
this shows a messagebox that shows the path of:
C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt
but the real file location is:
C:\Users\tgs266\Desktop\test.txt
please help
How are you getting the filenames for the ListView? Is it just the filename and no path?
If, for example, lv1.FocusedItem.Text is "test.txt", and that is the value you use (without the path), by default the program will look in the directory it's executing in. This is most likely why you're seeing C:\Users\tgs266\Desktop\SIOS\SIOS\SIOS\obj\Debug\test.txt as the location, instead of what you expected.
If all the files are on your desktop, you can use Environment.GetFolderPath in conjunction with the Environment.SpecialFolder Enumeration to get the file, like this:
Dim file As String = lv1.FocusedItem.Text
Dim testFile As System.IO.FileInfo
testFile = My.Computer.FileSystem.GetFileInfo(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + "\" + file)
Dim folderPath As String = testFile.DirectoryName
MsgBox(folderPath)
However, if you're going to have files scattered throughout your system, you'd be better off storing the full path as #Plutonix indicates in his comment.
It looks like your code is looking in your applications path on the server while you want to look at the users desktop location.
I have an application where i want to get all subdirectories (first level) from a given path.
I use this code:
Dim di As New IO.DirectoryInfo(Application.StartupPath & "\Folderlist")
For Each d In di.EnumerateDirectories()
Console.WriteLine(d.ToString)
Next
As you can see, i just need the name of the folder, not some object with all info.
Is there any way i can speed this up?
Dim dirs() = Directory.GetDirectories(Application.StartupPath & "\Folderlist")
For Each dir In dirs
Dim parent = Path.GetFileName(dir)
Console.WriteLine(parent)
Next
MSDN -
Directory.GetDirectories
Gets the names of subdirectories (including their paths) in the
specified directory.
Path.GetFileName
So I need somone to tell me how to fix this code. I'm trying to rename a file which is in C:\%appdata%\Roaming\.minecraft\bin\XenonUpdate.jar to minecraft.jar.
The code I am using is:
My.Computer.FileSystem.RenameFile("C:\%appdata%\Roaming\.minecraft\bin\XenonUpdate.jar", "minecraft.jar")
Can someone fix this?
%appdata% not not a valid path, rather it denotes a special folder that you can get by using Environment.GetFolderPath, once a get the %appdata% path, you can easily rename file.
Dim folder As String = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim file_to_rename = Path.Combine(folder, ".minecraft\bin\XenonUpdate.jar")
My.Computer.FileSystem.RenameFile(file_to_rename, "minecraft.jar")
File handling functions do not deal with environment variable expansion, %appdata%. You need to do this yourself.
My VB.Net is non-existent, but I think it would look like
Dim path = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)
Dim from = path + "\.minecraft..."
Dim to = path + "\.minecraft..."
My.Computer.FileSystem.RenameFile(from, to)
Also, see C# getting the path of %AppData%