הערה
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות להיכנס או לשנות מדריכי כתובות.
הגישה לדף זה מחייבת הרשאה. באפשרותך לנסות לשנות מדריכי כתובות.
Question
Monday, August 10, 2015 8:44 PM
I need to be able to search through a directory and find the files that are image files and then include their full path in a text file. The directory has a large number of different file types. I have been using the file extension to identify if the file is an image file. My code looks like this:
For Each foundImgFile In My.Computer.FileSystem.GetFiles(extrFilePath)
fileStats = My.Computer.FileSystem.GetFileInfo(foundImgFile)
If (fileStats.Extension = ".jpg") Or (fileStats.Extension = ".jpeg") Or (fileStats.Extension = ".png") Then 'add it to the convertlist.txt file
fileWriter.WriteLine(foundImgFile)
End If
Next
The issue I am having is that I have to explicitly check the file extension to a string like ".jpg" in order to determine if it is an image file. This comparison is case sensitive and I am finding that my folders contain a huge number of different image files like .jpg, .JPG, .jpeg, .JPEG, .png, .PNG, .tiff, .TIFF..... I think you get the picture.
So I have two questions.
1. is there a simple function or a method in Visual Basic to determine if a file is an image type of file. IF NOT THEN
2. Is there a way to make the comparison of the file extension not case sensitive?
All replies (7)
Monday, August 10, 2015 9:09 PM ✅Answered
Actually you don`t have to compare each extension individually, you can put all the extensions in one string and just use the String.Contains method something like this.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim extensions As String = ".jpg.bmp.png.gif.jpeg.tiff"
Using sw As New IO.StreamWriter("C:\TestFolder\Images.txt")
For Each img As String In IO.Directory.GetFiles("C:\TestFolder")
If extensions.Contains(IO.Path.GetExtension(img).ToLower) Then
sw.WriteLine(img)
End If
Next
End Using
End Sub
If you say it can`t be done then i`ll try it
Monday, August 10, 2015 8:46 PM | 1 vote
..... OrElse fileStats.Extension.tolower = ".jpeg" OrElse
Success
Cor
Monday, August 10, 2015 8:53 PM
Here is a simple method:
- Use windows search tool (type in search: "image", windows knows file type)
- Select all the found files
- Copy into Clipboard
- Use the following code to convert to text
Private Function convertClipboardFilesToText() As String
Dim _output As String = ""
If Clipboard.ContainsFileDropList Then
Dim _sc As Collections.Specialized.StringCollection = Clipboard.GetFileDropList
For Each _str As String In _sc
_output &= _str & ControlChars.NewLine
Next
End If
Return _output
End Function
Monday, August 10, 2015 9:21 PM
Not case sensitive. Three methods. Only checks extension since file could be anything. Checking if a file is actually an image file is not necessarily difficult but requires much more code and consumes time. Any file can be renamed to have an extension where the extension is not accurate for that file type.
Option Strict On
Imports System.IO
Public Class Form1
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
Me.Text = "Form1"
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
RichTextBox1.Clear()
For Each Item In GetFiles("C:\Users\John\Desktop\Picture Files", "*.bmp|*.gif|*.jpg|*.png|*.tif", IO.SearchOption.AllDirectories)
RichTextBox1.AppendText(Item & vbCrLf)
Next
End Sub
Private Shared Function GetFiles(sourceFolder As String, filters As String, searchOption As System.IO.SearchOption) As String()
Return filters.Split("|"c).SelectMany(Function(filter) System.IO.Directory.GetFiles(sourceFolder, filter, searchOption)).ToArray()
End Function
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
' For .Net 4.0 or above. Faster than get files.
RichTextBox1.Clear()
Dim files = Directory.EnumerateFiles("C:\Users\John\Desktop\Picture Files", "*.*", SearchOption.AllDirectories).Where(Function(s) s.EndsWith(".bmp") OrElse s.EndsWith(".gif") OrElse s.EndsWith(".jpg") OrElse s.EndsWith(".png") OrElse s.EndsWith(".tif"))
For Each Item In files
RichTextBox1.AppendText(Item & vbCrLf)
Next
End Sub
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
' For less than .Net 4.0 or above.
RichTextBox1.Clear()
Dim files = Directory.GetFiles("C:\Users\John\Desktop\Picture Files", "*.*", SearchOption.AllDirectories).Where(Function(s) s.EndsWith(".bmp") OrElse s.EndsWith(".gif") OrElse s.EndsWith(".jpg") OrElse s.EndsWith(".png") OrElse s.EndsWith(".tif"))
For Each Item In files
RichTextBox1.AppendText(Item & vbCrLf)
Next
End Sub
End Class
La vida loca
Monday, August 10, 2015 10:20 PM
Here is a simple method:
- Use windows search tool (type in search: "image", windows knows file type)
- Select all the found files
- Copy into Clipboard
- Use the following code to convert to text
Private Function convertClipboardFilesToText() As String Dim _output As String = "" If Clipboard.ContainsFileDropList Then Dim _sc As Collections.Specialized.StringCollection = Clipboard.GetFileDropList For Each _str As String In _sc _output &= _str & ControlChars.NewLine Next End If Return _output End Function
Thanks but this is a manual method not a programmatic solution. I have thousands of directories to go through
Monday, August 10, 2015 10:51 PM
@ Monkeyboy,
Aren't you getting fancy with your SelectMany function Mr. 8)
If you say it can`t be done then i`ll try it
Tuesday, August 11, 2015 3:58 AM
Actually you don`t have to compare each extension individually, you can put all the extensions in one string and just use the String.Contains method something like this.
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click Dim extensions As String = ".jpg.bmp.png.gif.jpeg.tiff" Using sw As New IO.StreamWriter("C:\TestFolder\Images.txt") For Each img As String In IO.Directory.GetFiles("C:\TestFolder") If extensions.Contains(IO.Path.GetExtension(img).ToLower) Then sw.WriteLine(img) End If Next End Using End Sub
If you say it can`t be done then i`ll try it
Thanks IronRazerz. I marked it as the solution. Nice and elegant. I like having a string that has a list of all the image extensions.. it only required changing 2 lines in my code.