How to list only file name? - vb.net

I have program to find files in a directory and list them in a listbox, but the following code I'm using adds the full path for the file found.
Is there something I'm missing to make it only add the file name and not the full path?
If My.Computer.FileSystem.DirectoryExists(My.Computer.FileSystem.CurrentDirectory & "\" & Details.IDL.Text) Then
For Each FoundFile As String In My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.CurrentDirectory & "\" & Details.IDL.Text)
ListBox.Items.Add(FoundFile)
Next
Else
My.Computer.FileSystem.CreateDirectory(My.Computer.FileSystem.CurrentDirectory & "\" & Details.IDL.Text)
End If
so to fix it i only had to put ListBox.Items.Add(IO.Path.GetFileName(FoundFile)) instead of ListBox.Items.Add(FoundFile)

Here is a working example to list file name individually with GetFileNameWithoutExtension, along with the way you are using GetFileName.
Dim fileName As String = "C:\mydir\myfile.ext"
Dim pathname As String = "C:\mydir\"
Dim result As String
result = Path.GetFileNameWithoutExtension(fileName)
Console.WriteLine("GetFileNameWithoutExtension('{0}') returns '{1}'", fileName, result)
result = Path.GetFileName(pathname)
Console.WriteLine("GetFileName('{0}') returns '{1}'", pathname, result)

Related

VBA open a csv file with unknow file name

I have got a rather simple question as i think but i couldnt find out it myself.
I want to open a csv File in a defined binder but with an unknow filename. I would asume that it should work with simply "path/*.csv" however it is not :( The errormessage says "Wrong Filename". Do i need to use something else in VBA.
path = ActiveWorkbook.path & "\input\"
Open path & "*.csv" For Binary As #1
The above code does not work for me :( The CSV is called xyz.csv
path = ActiveWorkbook.path & "\input\"
Open path & "xyz.csv" For Binary As #1
The code above is working however i have fix added the csv filename, which in this case is xyz.
Somebudy knows how to get that thing to work?
Cheers and thx for your time
Marc
Dim path As String
Dim csvFiles As String
path = ActiveWorkbook.path & "\input\"
csvFiles = Dir(path & "*.csv")
Do While Len(csvFiles) > 0
Debug.Print csvFiles
csvFiles = Dir
Loop
You can use the Dir() Function to check the files in your folder if you don't know the filename.

StreamWriter Text File name must include hour, minute and second

Run following code and see that you have text file on the desktop named MyLogFile 08.04.2017
Dim Log As System.IO.StreamWriter
Log = My.Computer.FileSystem.OpenTextFileWriter(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) & "\MyLogFile " & System.DateTime.Now.Date.ToString("dd/MM/yyyy") & ".txt", False)
Log.WriteLine("Hello")
Log.Close()
I had wanted to change file name from MyLogFile 08.04.2017 to MyLogFile 08.04.2017 07:50:59 but it is not possible because : is not allowed.
Now, I want to change file name from MyLogFile 08.04.2017 to MyLogFile 08.04.2017 07.50.59 thanks to your support.
I would strongly recommend that, when including dates and time in file and folder names, that you go from most significant to least significant. The reason for that is that then alphabetical and chronological order will match. Personally, I don't use separators at all so that would be:
Log = My.Computer.FileSystem.OpenTextFileWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
String.Format("MyLogFile.{0:yyyyMMddHHmmss}.txt",
Date.Now),
False)
If you really want to do it your way then it would be:
Log = My.Computer.FileSystem.OpenTextFileWriter(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop),
String.Format("MyLogFile {0:dd.MM.yyyy HH.mm.ss}.txt",
Date.Now),
False)
You simply change the format specifier as needed.
You could do:
Dim Folder As String = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)
Dim FileName As String = "MyLogFile " & DateTime.Now.ToString("dd.MM.yyyy HH.mm.ss") & ".txt"
Dim Log As System.IO.StreamWriter
Log = My.Computer.FileSystem.OpenTextFileWriter(System.IO.Path.Combine(Folder, FileName), False)
Log.WriteLine("Hello")
Log.Close()

Directory.Move isn't trying the correct folder

I have made some code intended to sort all my movies on my PC into subfolders for each letter of the alphabet (e.g. "An Example" would go in a subfolder containing only movies which start with the letter "A".
The code I've written looks to me like it should work without problems, although for some reason this code:
'Declarations
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")
For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
System.IO.Directory.Move(f.ToString, mainFolder.Name & UCase(Left(f.Name, 1)) & "\" & f.Name)
ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
System.IO.Directory.Move(f.ToString, TS.ToString & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
End If
Next
Keeps throwing a exception at this line:
System.IO.Directory.Move(f.ToString, CAM.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
This is the exception:
An unhandled exception of type 'System.IO.DirectoryNotFoundException' occurred in
mscorlib.dll
Additional information: Could not find a part of the path
'D:\Users\Yorrick\documents\visual studio 2012\Projects\filmsort\filmsort\bin\Debug\(CAM)The Internship'.
As indicated by the declarations above, I have no idea how or why this code is trying to access that folder.
If anyone could take a look and hopefully spot the mistake I've made, that would be very much be appreciated.
Edit:
Having fixed the above problem, I encountered a new one.
Using the code below, I now get the same exception, except this time additional information says "Could not find a part of the path." All my variables seem to be correct while debugging, so I seriously can't see why this isn't working.
Note: The commented line is something I tried, which gives me the System.IO.IOException: Cannot create a file when that file already exists.
Code:
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")
For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
If Not UCase(f.ToString).Contains("(CAM)") And UCase(f.ToString).Contains("(TS)") Then
System.IO.Directory.Move(f.FullName, mainFolder.FullName & UCase(Left(f.Name, 1)) & "\" & f.Name)
ElseIf UCase(f.Name.ToString).Contains("(CAM)") And Not UCase(f.Name.ToString).Contains("(TS)") Then
'System.IO.Directory.CreateDirectory(CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
System.IO.Directory.Move(f.FullName, CAM & UCase(Mid(f.Name, 6)) & "\" & f.Name.Substring(6))
ElseIf UCase(f.Name.ToString).Contains("(TS)") And Not UCase(f.Name.ToString).Contains("(CAM)") Then
System.IO.Directory.Move(f.FullName, TS.ToString & UCase(Mid(f.Name, 5)) & "\" & f.Name.Substring(5))
End If
Next
This happens when you don't specify a full path name, like "c:\foo\bar", but a relative path name, like "bar". Relative path names are turned into full ones by prepending Environment.CurrentDirectory. Which by default is the build directory of your project.
This happened because you used DirectoryInfo.Name. Which is "bar" for a directory whose path is c:\foo\bar. You must use the FullName property instead.
I just rewrote it a bit, i think that should suffice for your purpose.
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.CAMS")
System.IO.Directory.CreateDirectory("D:\Vuze Downloads\Movies\.TS")
Dim TS As String = "D:\Vuze Downloads\Movies\.TS\"
Dim CAM As String = "D:\Vuze Downloads\Movies\.CAMS\"
Dim mainFolder As New System.IO.DirectoryInfo("D:\Vuze Downloads\Movies\")
For Each f As System.IO.DirectoryInfo In mainFolder.GetDirectories()
If f.Name.ToUpper.Contains("(TS)") Then
System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(TS, f.Name))
ElseIf f.Name.ToUpper.Contains("(CAM)") Then
System.IO.Directory.Move(f.FullName, System.IO.Path.Combine(CAM, f.Name))
End If
Next

Get and edit a file name

I'm looking to retreive a txt file and then edit the file name (adding "converted" to the file name) and extension (from .r01 to .txt).
The purpose for this is so I can know if the txt file has been converted
Here's my code so far;
Dim infilename As Variant
infilename = Application.GetOpenFilename("Text & r01 Files (*.r01;*.txt),*.r01;*.txt", , "Open Neutral File", "OPEN")
InStrRev will allow you to find the last . and remove it and everything following from the string
FileNameWithoutExt = Left(Filename, InStrRev(Filename, ".") - 1)
An example with the workbooks FullName:
?activeworkbook.FullName
Z:\Individual Folders\Sean\transfers2.xlsx
?Left(activeworkbook.FullName, InStrRev(activeworkbook.FullName, ".") - 1)
Z:\Individual Folders\Sean\transfers2
You can wrap these in a function to make them easier to use. I've also added a function that will give the filename only instead of the one with the full path
Function FileNameOnly(fName)
'Changes "C:\Path\Filename.ext" to "Filename.ext"
FileNameOnly=mid(fName,instrrev(fName,"\")+1)
End Function
Function DelExt(fName)
'Changes "C:\Path\Filename.ext" to "C:\Path\Filename"
DelExt=left(fName,instrrev(fName,".")-1)
End Function
You can then use these in your program, with a line like NewFileName=DelExt(infilename) & "CONVERTED.txt"
I managed to get what I was looking for using part of Sean Cheshire's code.
Dim newFileName As Variant
newFileName = Left(inFileName, (InStrRev(inFileName, ".") - 1)) & "CONVERTED.txt"

replace file name have multiple extensions in vb.net

I have an issue while uploading documents have multiple periods. For example if I upload a file having an extension of ammu.gopu.docx. I would like to replace that as ammu_gopu.docx, means to preserve the extension and replace the file name with undescore.
This should do what your asking. Beware - If your file name also appears in the path it will also be updated.
Dim fullPath As String = "C:\Test\My.File.Name.txt"
Dim fileName As String = IO.Path.GetFileNameWithoutExtension(fullPath)
fullPath = fullPath.Replace(fileName, fileName.Replace("."c, "-"))
Use the System.IO.Path.GetExtension method.
Try this:
filePath = IO.Path.GetDirectoryName(filePath) & _
IO.Path.GetFileNameWithoutExtension(filePath).Replace("."c, "_"c) & _
"." & IO.Path.GetExtension(filePath)