How to separate readable and unreadable images into different folders? - vb.net

I have the folder D:\both_img. In that folder I have a bulk of both readable and unreadable .bmp images.
How can I move the unreadable images to a another folder?
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim openfiled1 As New OpenFileDialog
If openfiled1.ShowDialog <> DialogResult.Cancel Then
PictureBox1.Image = Image.FromFile(openfiled1.FileName)
End If
End Sub

Try the following code:
Dim di As New DirectoryInfo("D:\both_img")
Dim fiArr As FileInfo() = di.GetFiles()
Dim fi As FileInfo
For Each fi In fiArr
Try
Dim image1 As Bitmap = CType(Image.FromFile(fi.FullName, True), Bitmap)
fi.MoveTo(validFiledestPath) 'Move to valid file folder
Catch ex As OutOfMemoryException
fi.MoveTo(invalidFileDestPath) 'Move to invalid file folder
End Try
Next fri
This code iterates all the files. Tries to open them using Image.FromFile and if it opens moves them to valid folder otherwise move to invalid folder.
References

Related

GetDirectory will not list all Directories

I have a form that allows you to click a button, which triggers an OpenFileDialog. From there, you are suppose to select a specific file within that folder, and then the program is supposed to go through from the folder you were in the the /subjects folder and list those directories.
At the moment, I have 3 directories within /subjects: english, mathematics, and cte.
My issue is that when the program is ran, it will only list the English directory in the combo-box, and will not list any of the others.
Private Sub btnDocumentChoice_Click(sender As Object, e As EventArgs) Handles btnDocumentChoice.Click
Dim ofd As New OpenFileDialog
Dim DirList As New ArrayList
If ofd.ShowDialog = Windows.Forms.DialogResult.OK AndAlso ofd.FileName <> "" Then
strRootLocation = (Path.GetDirectoryName(ofd.FileName))
GetDirectories(strRootLocation + "/subject/", DirList)
'MessageBox.Show(Path.GetDirectoryName(ofd.FileName))
End If
End Sub
Private Sub OpenFileDialog1_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles OpenFileDialog1.FileOk
strRootLocation = OpenFileDialog1.FileName
cmbSubject.Items.Add(strRootLocation)
End Sub
Sub GetDirectories(ByVal StartPath As String, ByRef DirectoryList As ArrayList)
Dim Dirs() As String = Directory.GetDirectories(StartPath)
DirectoryList.AddRange(Dirs)
For Each Dir As String In Dirs
GetDirectories(Dir, DirectoryList)
cmbSubject.Items.Add(Replace(Path.GetDirectoryName(Dir), strRootLocation + "\subject", ""))
cmbSubject.Items.Remove("")
Next
End Sub
I managed to fix my own issue by removing the For Each loop in the question, and replacing it with this:
Dim directories As String
For Each directories In Directory.GetDirectories(strRootLocation + "\subject")
cmbSubject.Items.Add(Replace(directories, strRootLocation + "\subject\", ""))
Next

Deleting Specific Files and then Extract them into another folder

With the following code I am trying to delete specific files inside of a folder on a flash drive, and then copy the remaining files into a separate folder. When the program runs and I initiate the button to do so, the program deletes files that have not been modified within the past year, but then it does not continue to extract the remaining files and place them into a separate folder.
Does anyone know why?
Imports System.IO
Public Class frmExtractionator
Dim txtFiles1 As Control
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim sourceDirectory As String = "E:\CopierFolderforTestDriveCapstone"
Dim archiveDirectory As String = "E:\FilesExtracted"
Try
DeleteUnmodifiedFiles(sourceDirectory, 365)
Dim txtFiles = Directory.EnumerateFiles(sourceDirectory)
If (Not System.IO.Directory.Exists(archiveDirectory)) Then
System.IO.Directory.CreateDirectory(archiveDirectory)
End If
For Each currentFileLoc As String In txtFiles
Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
End Sub
Private Sub DeleteUnmodifiedFiles(ByVal directoryName As String, ByVal modificationThresholdDays As Integer)
Dim folder As New DirectoryInfo(directoryName)
Dim thresholdDate As Date
Dim wasModifiedSinceThreshold As Boolean
For Each file As FileInfo In folder.GetFiles
thresholdDate = DateTime.Now().AddDays(-1 * modificationThresholdDays)
wasModifiedSinceThreshold = (file.LastWriteTime > thresholdDate)
If (Not wasModifiedSinceThreshold) Then file.Delete()
Next
MessageBox.Show("Deleting Files")
End Sub
End Class
This will delete any file in the source directory that hasn't been modified for a year, and then will move any remaining files to the destination directory...
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
Dim fileListA() As String
fileListA = (IO.Directory.GetFiles("C:\Scource_Directory"))
For Each i As String In fileListA
If (IO.File.GetLastWriteTime(i).ToShortDateString.Substring(6)) < (CType(DateTime.Now.Year.ToString, Integer) - 1) Then
IO.File.Delete(i)
End If
Next
Dim fileListB() As String
fileListB = (IO.Directory.GetFiles("C:\Scource_Directory"))
For Each i As String In fileListB
IO.File.Move(i, "Destination_Directory")
Next
End Sub

Save Data to text tile using SaveFileDialog?

I have already viewed the MSDN Example but I am still having problems.
I created a super-simple program to multiply two numbers, and display the output in the textbox. Now I need to be able to read that text box value and put the value in a text file, bringing up the save to file dialog when the "Save To File" button is clicked.
Private Sub MutiplyBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MutiplyBtn.Click
Dim FirstNum As Double = Num1.Text
Dim SecondNum As Double = Num2.Text
Dim Answer2 As Double = FirstNum * SecondNum
Answerbox.Text = Answer2
End Sub
Private Sub SaveResultToFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SaveResultToFile.Click
Dim myStream As Stream
Dim saveFileDialog1 As New SaveFileDialog()
saveFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"
saveFileDialog1.FilterIndex = 2
saveFileDialog1.RestoreDirectory = True
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
myStream = saveFileDialog1.OpenFile()
If (myStream IsNot Nothing) Then
System.IO.File.WriteAllText(Answerbox.Text)
myStream.Close()
End If
End If
End Sub
Currently, Visual Studio is giving me an error: Overload resolution failed because no accessible 'WriteAllText' accepts this number of arguments.
WriteAllText static method requires the name of the file where the data should be written to.
You could use directly the name selected in the saveFileDialog1
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
System.IO.File.WriteAllText(saveFiledialog1.FileName, Answerbox.Text)
End If
instead if you really want to use the stream opened by OpenFile() method your code should be
If saveFileDialog1.ShowDialog() = DialogResult.OK Then
Dim sw As StreamWriter = new StreamWriter(saveFileDialog1.OpenFile())
If (sw IsNot Nothing) Then
sw.WriteLine(Answerbox.Text)
sw.Close()
End If
End If
The code is an example, you need to add a bit of error handling
Hi I tried above method but I succeed in this way....
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
SaveFileDialog1.Filter = "TXT Files (*.txt*)|*.txt"
If SaveFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK _
Then
My.Computer.FileSystem.WriteAllText _
(SaveFileDialog1.FileName, RichTextBox1.Text, True)
End If
End Sub

How to display Picturebox.image from Combobox.selecteditem

I'm using VB 2008 express to create a windows form application. I have a combobox named cb_face. The items in the combobox are image file names populated from my resource folder using a "for each" loop. When an item is selected I would like to display the image in picturebox1. I have tried several different codes but none of them display the image. I am not getting any errors. The comment lines show some of the code that has been tried.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim ImgFolder As New IO.DirectoryInfo("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources")
Dim ImgFile As IO.FileInfo() = ImgFolder.GetFiles("*.bmp")
Dim info As IO.FileInfo
For Each info In ImgFile
Dim FaceName As String = IO.Path.GetFileNameWithoutExtension(info.FullName)
CB_Face.Items.Add(FaceName)
Next
End Sub
Private Sub CB_Face_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles CB_Face.SelectedIndexChanged ', CB_Type.SelectedIndexChanged
If CB_Face.SelectedValue IsNot Nothing Then
'Load the image from the full file path
'PictureBox1.ImageLocation = CStr(CB_Face.SelectedItem.ToString)
'PictureBox1.Image = CB_Face.Items(CB_Face.SelectedItem).ItemData
'Dim pic = CType(My.Resources.ResourceManager.GetObject(CStr(CB_Face.SelectedItem)), Image)
'PictureBox1.Image = pic
'PictureBox1.Image = CB_Face.SelectedItem
PictureBox1.Image = Image.FromFile("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources"(CB_Face.SelectedItem.ToString).ToString)
'PictureBox1.ImageLocation = CB_Face.SelectedItem(Name)
'Try
'PictureBox1.Image = Image.FromFile(CB_Face.SelectedItem.ToString)
'Catch ex As Exception
'End Try
'PictureBox1.Image = DirectCast(CB_Face.SelectedItem, Image)
'CType(CB_Face.SelectedItem, Image)
End If
End Sub
Where you have:
PictureBox1.Image = Image.FromFile("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources"(CB_Face.SelectedItem.ToString).ToString)
Change that to something more like:
PictureBox1.Image = Image.FromFile("C:\Documents and Settings\ubd\My Documents\Visual Studio 2008\Projects\Blank Out\Blank Out\Resources\" & (CB_Face.SelectedItem.ToString) & ".bmp")
This assumes based on the rest of your code that the file is a bmp and is located in the Resources directory of the rest of that path.
You need to use & to append to your directory path string and then readd the file extension in the same way.

adding a file into a listbox in vb.net / vb2008

Hello i store proxys in a notpad.txt file and im trying to grab all proxys in the notpad and put them into listbox1
i am using
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using Ofd As New OpenFileDialog
Ofd.Filter = "All files (*.*)|*.*"
If Ofd.ShowDialog = 1 Then ListBox1.Items.AddRange(IO.File.ReadAllLines(Ofd.FileName))
End Using
End Sub
I click it the button it lets me pick a file but does not import the stuff in the file to listbox1
Please help
I tested your code and it works for me, so I assume the problem is with the format of your file. How was the file created? Could you provide a link to it so I can take a look?
One thing to note, you should use the DialogResult Enumeration instead of the magic number 1 for the OK result to improve the readability and maintenance of your code.
If Ofd.ShowDialog = DialogResult.OK Then ListBox1.Items.AddRange(IO.File.ReadAllLines(Ofd.FileName))
try this example add it if you click on the OK button
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
Using Ofd As New OpenFileDialog
Ofd.Filter = "All files (*.*)|*.*"
If Ofd.ShowDialog = 1 Then
'Pass the file path and file name to the StreamReader constructor
Dim sr As New StreamReader(Ofd.FileName)
Dim line As String = String.Empty
Try
'Read the first line of text
line = sr.ReadLine()
'Continue to read until you reach end of file
While line IsNot Nothing
Me.listBox1.Items.Add(line)
'Read the next line
line = sr.ReadLine()
End While
'close the file
sr.Close()
Catch e As Exception
MessageBox.Show(e.Message.ToString())
Finally
'close the file
sr.Close()
End Try
End If
End Using
End Sub
Regards