Get files in a directory not containing certain pattern with FileInfo object - vb.net

I have this files in a directory:
User1_File1.pdf
User1_File2.pdf
User1_File2-1.pdf
User1_File2-2.pdf
User1_File3.pdf
User1_File4.pdf
I'm working in vb .Net. Using the GetFiles method of the FileInfo object I want to get all the files NOT containing the "-" character in the name. How can I do this?
Dim diDirectorio As New DirectoryInfo(sPath)
Dim fiArchivos() As FileInfo = diDirectorio.GetFiles(sFilter & "*.*")

If Not fileName.Contains("-") Then
'Get code
End If
Post your code and I can implement this feature for you.

I know that this is old now but this works too...
dim dirInfo as new io.DirectoryInfo("DirectoryPath")
dim files as io.fileinfo() = dirInfo.GetFiles("pattern").Where(Function(x) Not x.Name.ToLower.Contains("exclude pattern")).toArray

Related

display all excel file in folder

I want to review all the Excel files inside a folder to listbox, noting that the Excel includes more than one version.
I used the following code and it didn't work
Dim di As New IO.DirectoryInfo("D:\EXCEL")
Dim aryFi As IO.FileInfo() = di.GetFiles("Excel Worksheets|*.xls; *.xlsx; *.xlsm")
Dim fi As IO.FileInfo
For Each fi In aryFi
ListBox1.Items.Add((fi))
Next
Modified slightly from this example: https://stackoverflow.com/a/3527717/1920035
Private Function GetExcelFiles(ByVal directoryInfo As IO.DirectoryInfo) As IO.FileInfo()
If (directoryInfo Is Nothing) Then
Throw New ArgumentNullException(NameOf(directoryInfo))
End If
Dim files As IEnumerable(Of IO.FileInfo) = directoryInfo.GetFiles()
Dim excelFileExtensions() As String = {".xlsx", ".xlsm", ".xlsb", ".xltm", ".xlam", ".xls", ".xla", ".xlb", ".xlc", ".xld", ".xlk", ".xll", ".xlm", ".xlt", ".xlv", ".xlw"}
return files.Where(Function(file) excelFileExtensions.Contains(file.Extension))
End Function
What this does is:
Get all files from the directory info
Declare a collection of file extensions to check against
Return only the files where the extension exists in the file extensions collection
Important to note - It may be worthwhile moving the excelFileExtensions so that it is a private readonly variable at a higher scope. It isn't much, but if you're running this a lot then it will make a difference.

Delete A File That Contains The App Name (VB.NET)

This is the code I'm Using:
Dim file As String
Dim prefetchPath As String
Dim FileName As String = My.Application.Info.AssemblyName
prefetchPath = Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine) & "\Prefetch"
For Each file In IO.Directory.GetFiles(prefetchPath)
If file.Contains(FileName) Then
IO.File.Delete(file)
End If
Next
i don't know why it does not work if i use FileName. But it work if i use this code
If file.Contains("Example.exe") Then
IO.File.Delete(file)
End If
I want to make sure that if someone changes the name of the application the code works the same way(I already running the file as Administrator)
Help me Thanks.
My guess is that AssemblyName only returns the name without the extension, try including the .exe. Also, it is worth noting that you can use the IO.DirectoryInfo class and pass the file name in the GetFiles method to cut out your For/Each loop.
Here is a quick example:
Dim prefetchPath As String = IO.Path.Combine(Environment.GetEnvironmentVariable("windir", EnvironmentVariableTarget.Machine), "Prefetch")
Dim FileName As String = My.Application.Info.AssemblyName & ".exe"
If New IO.DirectoryInfo(prefetchPath).GetFiles(FileName).Count > 0 Then
IO.File.Delete(IO.Path.Combine(prefetchPath, FileName))
End If

vb check for specific file type in dir and perform code

I'm trying to make a program that checks for specific file type in a directory, then executes a code if there are any files of that type found.
I'm assuming something like this:
For Each foundFile As String In
My.Computer.FileSystem.GetFiles(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
(If any found files are, for example, "txt" files, then display their content.)
Next
Thanks in advance.
You can use Directory.GetFiles or Directory.EnumerateFiles with a parameter for the extension-filter:
Dim directoryPath = My.Computer.FileSystem.SpecialDirectories.MyDocuments
Dim allTxtFiles = Directory.EnumerateFiles(directoryPath, ".txt")
For each file As String In allTxtFiles
Console.WriteLine(file)
Next
The difference between both methods is that the first returns a String(), so loads all into memory immediately whereas the second returns a "query". If you want to use LINQ it's better to use EnumerateFiles, f.e. if you want to take the first 10 files:
Dim firstTenFiles As List(Of String) = allTxtFiles.Take(10).ToList()
Dim di As DirectoryInfo = New DirectoryInfo(My.Computer.FileSystem.SpecialDirectories.MyDocuments)
For Each fi In di.GetFiles("*.txt")
Dim content As String = My.Computer.FileSystem.ReadAllText(fi.FullName)
Console.WriteLine(fi.Name)
Next

Get only those files in a directory whose name does not start with a certain string

I currently have this code:
Dim FolderInfo As IO.DirectoryInfo = New IO.DirectoryInfo("C:\Scratch")
For Each FInfo As IO.FileInfo In FolderInfo.GetFiles("*.xlsx")
MessageBox.Show(FInfo.ToString())
Next FInfo
Obviously this will get all the files that match the pattern "*.xlsx" - but I am NOT interested in any files that start with "old" - so of course within the For Next, I could do something like if If Not FInfo.Name.StartsWith("old") Then ... and do what I need to do, but I was wondering if there is any way to tell the GetFiles to only get files that "don't start with "old" and end in *.xlsx" ?
I've seen examples in C# that I believe use LINQ - so after the GetFiles there is stuff like ".Where(f => !(f.FullName.StartsWith("old")))" but not sure what ( if there is one ) the equivilant would be for VB.NET ?
Cheers,
Chris.
The syntax is a bit more verbose, but Where works as well in VB
For Each FInfo As IO.FileInfo In FolderInfo.GetFiles("*.xlsx").
Where(Function(x) Not x.Name.StartsWith("old"))
MessageBox.Show(FInfo.ToString())
Next FInfo
I would also add a StringComparison.CurrentCultureIgnoreCase to remove also files that starts with "Old" or "OLD" and so on
For Each FInfo As IO.FileInfo In FolderInfo.GetFiles("*.xlsx").
Where(Function(x) Not x.Name.StartsWith("old", StringComparisong.CurrentCultureIgnoreCase))
MessageBox.Show(FInfo.ToString())
Next FInfo
By the way, you should use the property Name instead of FullName. FullName returns also the path to the file and, obviously, this path doesn't start with "old".
Dim folder As String = "C:\Scratch"
Dim files = Directory.EnumerateFiles(folder, "*.xlsx", SearchOption.TopDirectoryOnly) _
.Where(Function(f) Not Path.GetFileName(f).ToLowerInvariant().StartsWith("old"))
For Each file As string In files
MessageBox.Show(file)
Next file

In vb.net, how do I get files from a directory based on a comma separated string?

I need to create an array() from files in a folder. Here's an example of how I would get all files within a folder.
Dim filesList = New DirectoryInfo("MyPath").GetFiles("*", SearchOption.TopDirectoryOnly).Where(Function(f) Not f.Attributes.HasFlag(FileAttributes.Hidden)).[Select](Function(f) New AClassNameHere(f)).ToArray()
I want to do the exact same thing, but only get files that exist in a comma separated string.
Dim myFiles as String = "filename1.jpg,filename2.jpg,filename3.jpg"
Where you see the AClassNameHere is a class I need to send each file to, and it would also be great if I knew how to send additional data about each file, like its type, size, etc.
Thank you kindly!
You could narrow the query results by adding an additional .Where() filter
Dim myFiles as String = "filename1.jpg,filename2.jpg,filename3.jpg"
Dim filesList = New DirectoryInfo("MyPath")
.GetFiles("*", SearchOption.TopDirectoryOnly)
.Where(Function(f) Not f.Attributes.HasFlag(FileAttributes.Hidden))
.Where(Function(f) myFiles.Contains(f.Name))
.[Select](Function(f) New AClassNameHere(f)).ToArray()
A better option would be to ensure that all filenames follow a pattern.
New DirectoryInfo("MyPath").GetFiles("filename*.jpg", SearchOption.TopDirectoryOnly)
Use this...
Dim Files() As String
Files= filesList.Split(",")
For each File In Files
Msgbox(File)
Next