I am trying to move only one file that is the newest created or edited from a directory to another folder on two different servers. How would I only move the newest file from one directory to the next instead of all the files in the folder?
Here is the code I am using to move the file over.
My.Computer.FileSystem.CopyDirectory("\\172.16.1.42\s$\SQLBackup\FWP", "\\172.16.1.22\F$\BackupRestore", True)
It shouldn't be too hard to determine which file is the newest one. A simple way to do this would be to retrieve information about all the files in a directory and then cycle through them to find the most recent.
You could do something like this:
Imports System.IO
Dim di As New IO.DirectoryInfo("c:\") ' Change this to match your directory
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
Dim mostRecentFile As IO.FileInfo = Nothing
Dim mostRecentTimeStamp As DateTime = Nothing
DateTime.TryParse("01/01/1900 0:00:00", mostRecentTimeStamp) ' Set to early date
For Each dra In diar1 ' Cycle through each file in directory
If File.GetLastAccessTime(dra.FullName) > mostRecentTimeStamp Then
mostRecentTimeStamp = File.GetLastAccessTime(dra.FullName)
mostRecentFile = dra
End If
Next
Debug.Print(mostRecentFile.FullName) ' Will show you the result
' Use mostRecentFile.Copy to copy to new directory
Hopefully that solves your problem. If not, let me know. There may be an issue about this routine detecting hidden files, so if you see something like that, post back here. You will also want to add code to detect if no new file is found, for example.
Related
I am trying to transfer a file from one directory to another that is the current and latest file in that folder, however I cannot use Robocopy to do this, since it fails when the server is logged out. Is there another way I can do this in Vb.net?
Thank you.
you can use File.Copy(Source,Destination,Overwrite?) to overwrite the file with a newer one, or
If File.Exists(destination) Then File.Delete(destination);
' Move the file.
File.Move(source, destination);
to move the file... I pesonally prefer:
File.Copy(Source,Destination,true)
File.Delete(Source)
To move the file and overwrite it if it exists :)... less code
Here's the code to move the latest file to another directory
Dim SourceDirectory As String = "C:\sourcedirectory\"
Dim SaveDirectory As String = "C:\targetdirectory\"
Dim LatestFile as IO.FileInfo = Nothing
'Let's scan the directory and iterate through each file...
Dim DirInfo As New IO.DirectoryInfo(SourceDirectory)
For Each file As IO.FileInfo In DirInfo.GetFiles()
If LatestFile Is Nothing Then
'This is the first time we run any permutations, so let's assign this file as "the latest" until we find one that's newer
LatestFile = file
ElseIf file.CreationTime > LatestFile.CreationTime Then
'Changes the "Latest file" if this file was created after the previous one.
'You can also change the check to .LastAccessTime and .LastWriteTime depending on how you want to define "the newest"...
LatestFile = file
End If
Next
'Now we move the file, but first, check to see if we actually did find any file in the source directory
If NewestFile IsNot Nothing Then
NewestFile.CopyTo(SaveDirectory & NewestFile.Name,true) 'will copy and overwrite existing file
'Now we can delete the old one
NewestFile.Delete()
Else
'Could not find the newest file, or directory might be empty...
End If
'Done!
Hope that Helps
I need to write a script for deleting multiple files. I have been able to write one that searches a directory for one file name and then deletes it but I need the ability to specify more than one file name at a time and delete all of them if found. It should be noted that I am not trying to search for duplicate file names. Right now I have to edit the script each time I want to search for a file to delete it, I have to do this several times per day with a dozen or so files at a time.....the file names are never the same; once used they will never be used again.
Here is an example of what I'd like to achieve.
Search C:/junk/log/ for "ff12345a.txt" and "hg76930b.dr1" and "890379.rt" then delete all.
For now I am attempting to do this through a bat file but eventually it would be cool to have a vb program that has a GUI allowing me to specify multiple files I want to search for rather than editing the script each time.
Thanks in advance!
' make a reference to a directory
Dim di As New IO.DirectoryInfo("c:\")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
'Do what you want with dra / Use your delete code here
ListBox1.Items.Add(dra)
Next
To filter search change di.GetFiles() to di.GetFiles(“.extionsion”)
I'm doing some experiments lately but I can't make this one work. Here's my code:
If cbHistory.Checked = True Then
Dim di As New DirectoryInfo("C:\Users\" & userName & "\AppData\Local\Google\Chrome\UserData")
Dim diar1 As FileInfo() = di.GetFiles("*.*", SearchOption.AllDirectories)
Dim dra As FileInfo
'list the names of all files in the specified directory
For Each dra In diar1
listToDelete.Items.Add(dra)
Next
End If
My code takes a the list of files located in the specified location and put it in a list box. I have 2 more block of code like that (cbCache and cbCookies).
Now my problem is, I want that if I unchecked one of them, the cbHistory checkbox for example, it will remove the items it took from the directory. Now, I can't use listbox.items.clear() because it will remove everything in the list box. So what can I do here?
Any help would be appreciated. Thank you! :)
Hi I'm doing some testing on unzipping files using Shell32. In this code there's a several files in "C:\Temp" and I unzip those files by creating a new folder name Unzip which will store the unzipped files.
So the question is how to check if the file already exists, the program will hold or continue to the next function. Here's the code.
Dim di As New IO.DirectoryInfo("C:\Temp")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
Dim sc As New Shell32.Shell()
For Each dra In diar1
IO.Directory.CreateDirectory("C:\Temp\Unzip\" & dra.Name & "")
Dim output As Shell32.Folder = sc.NameSpace("C:\Temp\Unzip\" & dra.Name & "")
Dim input As Shell32.Folder = sc.NameSpace("C:\Temp\" & dra.Name & "")
output.CopyHere(input.Items, 4)
Next
Thanks In advance :)
File.Exists() is bad for this kind of thing.
What you want to do instead is attempt to open your output stream in a way that will fail (throw an exception) if the file is already there, and handle the exception. Unfortunately, the Shell32 process you're using right now doesn't allow for that. However, it's not that hard to unzip files using .Net directly. Especially if you can use .Net 4.5, this is pretty easy: there is new ZipArchive support built-in. In prior versions of .Net, you may want to look at SharpZipLib. It's in C#, but you will be able use the library from VB.Net.
Function getItems()
''# make a reference to a directory
Dim di As New IO.DirectoryInfo("https://ipossum.svn.sourceforge.net/svnroot/ipossum/")
Dim diar1 As IO.FileInfo() = di.GetFiles()
Dim dra As IO.FileInfo
''#list the names of all files in the specified directory
For Each dra In diar1
ListBox1.Items.Add(dra)
Next
End Function
That is my code and it did not work. The error was "Warning 1 Function 'getItems' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used. C:\Users\******\AppData\Local\Temporary Projects\iPossum\Form1.vb 13 5 iPossum".
How do I do this? Thanks!
To fix the error you're asking about, just change the word Function to Sub.
However, after you do this you're code still won't work. You'll have a new error, because the System.IO directory and file classes only work on the local file system. You can't reference a remote https location that way. You'll need to use System.Net.HttpWebRequest/System.Net.HttpWebResponse or System.Net.WebClient instead, and that means essentially starting from scratch with this code.
A very simple example that may or may not work, depending on the https requirement:
Dim fileList As String
Using wc As New WebClient
fileList = wc.DownloadString("https://ipossum.svn.sourceforge.net/svnroot/ipossum/")
End Using
''# Here you'll have to parse the file names out of this response on your own