Visual Basic - Copying the only Recently Modified Files to another Folder - visual-studio-2010

the following code I have hear will extract all the files inside of a certain folder, and then copy all of them and place them into another folder. My question today is how do I modify this code so that only files extracted from the original folder have been recently modified. Even if you could show me how to extract only the files that have been modified from today would be great. Thanks to all of you who help!
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, -14)
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

You can use the Directory.GetLastWriteTime Method to determine when the file was last written to.
From Link:
Returns the date and time the specified file or directory was last written to.
Dim checkDate As Date = Date.Parse("01/1/2013")
For Each currentFileLoc As String In txtFiles
Dim fileName = currentFileLoc.Substring(sourceDirectory.Length + 1)
If Directory.GetLastWriteTime(Path.Combine(sourceDirectory, fileName)) > checkDate Then
File.Move(currentFileLoc, Path.Combine(archiveDirectory, fileName))
End If
Next

Related

How to get Volume Serial Number using Visual Basic 2010?

I'm trying to get Volume Serial Number using Visual Basic 2010,
Is there a whole code example that shows me how to do this?
Thanks
I guess the simplest answer to my question was given by:
Hans Passant:
From his link,
I just copied and pasted this function and it works for Microsoft Visual basic 2010 express, Without any modifications
Public Function GetDriveSerialNumber() As String
Dim DriveSerial As Long
Dim fso As Object, Drv As Object
'Create a FileSystemObject object
fso = CreateObject("Scripting.FileSystemObject")
Drv = fso.GetDrive(fso.GetDriveName(AppDomain.CurrentDomain.BaseDirectory))
With Drv
If .IsReady Then
DriveSerial = .SerialNumber
Else '"Drive Not Ready!"
DriveSerial = -1
End If
End With
'Clean up
Drv = Nothing
fso = Nothing
GetDriveSerialNumber = Hex(DriveSerial)
End Function
I would like to thank everyone for their help,
And i apologize for repeating the question,
I did do a google search and a stackflow search,
But my search was"
"get hard drive serial number in visual basic 2010"
So this website did not show up,
Thanks again
This thread here http://social.msdn.microsoft.com/Forums/vstudio/en-US/43281cfa-51c8-4c35-bc31-929c67abd943/getting-drive-volume-serial-number-in-vb-2010 has the following bit of code that you could use/adapt.
I made a piece of code for you to show all drive information.
The Volume serial number is included you can get that by simple
putting some more if's in the code
Imports System.Management
Public Class Form1
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim drivetype() As String = {"Unknown", "NoRootDirectory", _
"RemoveableDisk", "LocalDisk", "NetworkDrive", "CompactDisk", "RamDisk"}
Dim allDrives() As String = Environment.GetLogicalDrives()
For Each drive In allDrives
Dim win32Drive As String = _
"Win32_LogicalDisk='" & drive.Substring(0, 2) & "'"
Dim Disk As System.Management.ManagementObject _
= New System.Management.ManagementObject(win32Drive)
Me.ListBox1.Items.Add(drive.ToString & drivetype(CInt((Disk("DriveType").ToString))))
For Each diskProperty In Disk.Properties
If Not diskProperty.Value Is Nothing Then
Me.ListBox1.Items.Add("---" & diskProperty.Name & "=" & diskProperty.Value.ToString)
End If
Next
Next
End Sub
End Class

How to write data to a file in Visual Studios?

I'm writing a program that reads an input file that contains a line:
Scott Atchison,200,74
The file contains around 30 different lines of data. I know how to read in the file. After the file is read in it is split and then a calculation needs to be done (I know how to do that).
However, the problem that I have is the output file, I can get only the last line of the input file to the output file. This is what I have right now:
Public Class BMI
Dim data As String
Dim strName As String
Dim intWeight As Integer
Dim intHeight As Integer
Dim decBMI As Decimal
Private Sub btnInputFile_Click(sender As System.Object, e As System.EventArgs) Handles btnOpenFile.Click
'User chooses a file
OpenFile.ShowDialog()
'Choose a file name into a label
lblFileInput.Text = OpenFile.FileName
Dim inputFile As New IO.StreamReader(lblFileInput.Text)
Do While (inputFile.Peek() > -1)
data = inputFile.ReadLine
Dim fields() As String = data.Split(",")
strName = fields(0)
intWeight = fields(1)
intHeight = fields(2)
txtData.Text = txtData.Text & data & vbNewLine
Loop
FileClose()
End Sub
Private Sub Button1_Click(sender As System.Object, e As System.EventArgs) Handles btnSaveFile.Click
SaveFile.ShowDialog()
lblFileOutput.Text = SaveFile.FileName
Dim output As IO.StreamWriter
output = New IO.StreamWriter(lblFileOutput.Text)
output.WriteLine(data)
End Sub
Private Sub Button1_Click_1(sender As System.Object, e As System.EventArgs) Handles Button1.Click
decBMI = (intWeight * 703 / (intHeight ^ 2))
data = strName & ", " & decBMI
End Sub
End Class
Wouldn't it write a line until all 30 lines are read, or Am I missing something, like a while loop? Any help would be appreciated.
It would be nice if you gave us a nice, executable, snippet of code, including declarations of all your variables. For instance, where is "data" defined. What is the relationship between both chunks of code? Are they in the same procedure?
The first thing I notice is that you are splitting each line, "data", into fields, saving them into variables, which you just ignore. You then append "data" to a text box.
Assuming that "data" is shared between the two chunks of code, then reason for your problem is that the value being written to the streamwriter is the last value of "data", which happens to be the last line that was read into it.

Visual Basic Deleting Unmodified Files

The following code will extract files from one Folder and Place them another as you can see. But I am trying to delete the files that have not been modified in the past 3 months, and it does not delete any files at all.
Code:
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
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))
Try
Dim di As DirectoryInfo = New DirectoryInfo(sourceDirectory)
Dim fi As FileInfo() = di.GetFiles()
For Each currentFile As FileInfo In fi
File.Move(currentFile.FullName, Path.Combine(archiveDirectory, currentFile.Name))
Dim dt As DateTime = currentFile.LastWriteTime
' Add 3 months to the last write and check if it is less than today '
If dt.AddMonths(3) < DateTime.Today Then
File.Delete(currentFile.FullName)
End If
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
Next
Catch eT As Exception
Console.WriteLine(eT.Message)
End Try
End Sub
End Class

How do I get the most recently dated file in a folder in VB6

I'm working with some legacy VB6 code and I'm horrible with it. The code below "thinks" its getting the most recent file by using the name of the file. But this method no longer works because it uses digits 0 - 9 to determine this. And it sees 6 as being newer than 2, where that number is the year. For example. the files that need sorting are formatted like this.
FORMZZ6.eln is a 2006 file
FORMZZ2.eln is a 2012 file
The code below sorts alphabetically. Is it possible to choose the file nased on its last modified date?
Private Function ResolveFormVersion(sForm As String) As String
Dim sFile As String
Dim sFile2 As String
sFile = Dir(BaseDirectory & sForm, vbNormal)
Do
sFile2 = Dir
If sFile2 > sFile Then sFile = sFile2
Loop Until sFile2 = ""
ResolveFormVersion = sFile
End Function
Thanks
Yes you should be able to rewrite the loop to use the FileDateTime function:
http://msdn.microsoft.com/en-us/library/aa262740%28VS.60%29.aspx

Outputting list of files that are part of a Visual Studio Project

I'm looking for a way to output a list of all files in a Visual Studio project for documentation purposes.
I would have thought this would be possible but I can't find any info. I'm not talking about using Sandcastle to hook up to XML comments, I just want a "simple" indented list of project files.
I'm guessing we could run an xsl file against the Proj file but hopefully somebody has already got a solution for this? Ideally this would work on both 2008 and 2010.
The VS2008 sample macros already contain a macro that practically does this (prints a list of source/header file to the output window). It's called ListProj1, under the Utilities samples. Here's the code in case you don't have it:
Sub ListProj()
Dim project As Project
Dim projectObjects As Object()
Dim window As Window
Dim target As Object
window = DTE.Windows.Item(Constants.vsWindowKindCommandWindow)
projectObjects = DTE.ActiveSolutionProjects
If projectObjects.Length = 0 Then
Exit Sub
End If
project = DTE.ActiveSolutionProjects(0)
If (DTE.ActiveWindow Is window) Then
target = window.Object
Else
target = GetOutputWindowPane("List Project")
target.Clear()
End If
ListProjAux(project.ProjectItems(), 0, target)
End Sub
Sub ListProjAux(ByVal projectItems As EnvDTE.ProjectItems, ByVal level As Integer, ByVal outputWinPane As Object)
Dim projectItem As EnvDTE.ProjectItem
For Each projectItem In projectItems
If projectItem.Collection Is projectItems Then
Dim projectItems2 As EnvDTE.ProjectItems
Dim notSubCollection As Boolean
OutputItem(projectItem, level, outputWinPane)
'' Recurse if this item has subitems ...
projectItems2 = projectItem.ProjectItems
notSubCollection = projectItems2 Is Nothing
If Not notSubCollection Then
ListProjAux(projectItems2, level + 1, outputWinPane)
End If
End If
Next
End Sub
Sub OutputItem(ByVal projectItem As EnvDTE.ProjectItem, ByVal level As Integer, ByVal outputWinPane As Object)
Dim i As Integer = 0
While (i < level)
outputWinPane.OutputString(" ")
i = i + 1
End While
outputWinPane.OutputString(projectItem.FileNames(1))
outputWinPane.OutputString(Microsoft.VisualBasic.Constants.vbCrLf)
End Sub

Resources