File check and update - vb.net

I want to make updater for some files (Like a SVN). So all files are around 250MB and downloading them and comparing with exist files will be too long. I thought to make system, that compares just information about files.For example: get number of bytes from file "a" on hdd and file "a" on a cloud, then compare the numbers and update if needed file on hdd.
So I tried to make it like I said before, but failed. I have some code, but I can't make it done:
Private Sub scan()
Dim path As String = TextBox1.Text
Dim files() As FileInfo
Dim dirinfo As New DirectoryInfo(path)
files = dirinfo.GetFiles("*", IO.SearchOption.AllDirectories)
For Each file In files
ListBox1.Items.Add(file.Name & "," & file.Length)
Next
End Sub
I just have a file list in my program and in the cloud, but i can't use it for comparing. Is there any idea how to make it?

Related

Identify some files in Specialized.StringCollection

I read the *.fileExt files using:
Dim tempList As New Specialized.StringCollection
         tempList.AddRange(My.Computer.FileSystem.GetFiles(path, subFolders, "*" & fileExt).ToArray)
When processing I find some files that were deleted and are invisible from the file explorer but that are read by .GetFiles, they have their original name preceded by: "~$"
Example: "C:.......\~$OriginalFileName.fileExt"
I tried to exclude files that have "~$" at the beginning of the name, but it's not a good solution since I can create and save a file with "~$" at the beginning of the name, then the code would avoid a file that was never deleted.
I want to continue using .AddRange and exclude deleted files, I see two options:
Prevent .GetFiles from taking those files.
Identify them in tempList to avoid processing them.
I can't get any of the two to work.
I guess there is a simple way to identify if a file is deleted or not... an attribute or flag that identifies it as "deleted"... but I don't know how to do it. It's possible? How I should proceed?
Some programs create a backup file while a file is being edited, and those files are named starting with "~$" and have the hidden attribute set. Sometimes those files get orphaned, perhaps because of a program crash, but you don't notice them because they are hidden.
To avoid getting hidden files, you can select only files which do not have the hidden attribute, something like this:
Option Strict On
Option Infer On
Imports System.IO
Module Module1
Sub Main()
Dim srcDir = "C:\temp\testfiles"
Dim fileExt = ".txt"
Dim tempList As New Specialized.StringCollection()
Dim selectedFiles = (New DirectoryInfo(srcDir)).GetFileSystemInfos("*" & fileExt).
Where(Function(fi) Not (fi.Attributes And FileAttributes.Hidden) = FileAttributes.Hidden).
Select(Function(fi) fi.FullName)
tempList.AddRange(selectedFiles.ToArray())
For Each s In tempList
Console.WriteLine(s)
Next
Console.ReadLine()
End Sub
End Module
I created some files in a directory, and one of them I named starting with "~$" and set its hidden attribute:
C:\temp\testfiles>dir
Directory of C:\temp\testfiles
21/10/2019 15:48 0 one.txt
21/10/2019 15:49 0 two.txt
21/10/2019 15:48 0 ~$one.txt
3 File(s) 0 bytes
C:\temp\testfiles>dir /a:h
Directory of C:\temp\testfiles
21/10/2019 15:49 0 ~$three.txt
1 File(s) 0 bytes
and got this output from the program:
C:\temp\testfiles\one.txt
C:\temp\testfiles\two.txt
C:\temp\testfiles\~$one.txt
N.B. You are probably making things difficult for yourself by using a Specialized.StringCollection() instead of a List(Of String).
This can help you:
tempList As New Specialized.StringCollection
tempList.AddRange((From sFile As String In My.Computer.FileSystem.GetFiles(path, subFolders, "*" & fileExt).ToList.AsEnumerable
Where sFile IsNot Nothing
Where sFile.IndexOf("~$") = -1).ToArray)

How to Auto Copy text file from one folder to another folder in vb 2010 console or windows application

I want to create a program that auto copy text file from one folder to another folder . is it possible to make in windows form in vb.net ? if not what about in console apps ? i tried to search but i didn't find an answer for both. please help me i'm new to to this. I want to copy all the text file that is being save to c:folder1\test1.text copy to c:folder2\test1.text then test2.text,test3.text all the text file that are being put in folder1. i want to copy in folder2.
now i only have this code:
it will only copy 1 specific textfile with file name test.txt.
enter code here
My.Computer.FileSystem.CopyFile("C:\CopyTo\test.txt",
"C:\CopyHere\test.txt")
Of course! First of all we need a function that search for files.
Public Sub SearchFiles(ByVal Pattern As String, ByVal Path As String, ByVal FilesFound As ArrayList)
FilesFound.AddRange(Directory.GetFiles(Path, Pattern))
End Sub
But where we should save the list of files? We can use a Array for it. Also we should define our output and input folder
Dim files As New ArrayList
Dim inDir As String = "input path"
Dim outDir As String = "output path"
We can now call this function like this:
SearchFiles("*.txt", inDir, files)
All .txt files in the folder are now saved in our Array List. But how we can work with it? We can now work with it like this:
Try
For Each file As String In files
Dim fName As String = Path.GetFileName(file)
My.Computer.FileSystem.CopyFile(file , outDir & "\" & fName, overwrite:=False)
Next
Catch ex As Exception
Console.WriteLine(ex.ToString)
End Try
This will copy every .txt file that where found in our inDir to our outDir. If something goes wrong then you will see this in the console. Try it out and understand how it works :)

Search multiple files though sub folders VB.net

I'm having a hard time trying to figure it out how to find multiple files through sub folders, I'm looking for different file names and I think that's why I can't solve it.
"file_name" in the code is the name of the varible, I pull the name of the document from the data base which it's stored in a field without the extension and then when it finds the .docx/doc file it should convert it to pdf.
Dim di As DirectoryInfo = New DirectoryInfo("\\192.168.1.70\sisint\court\
agreements")
For Each fi In di.GetFiles(file_name, SearchOption.AllDirectories)
'seleccionamos los archivos con las extensiones de Word
If fi.Extension.ToUpper = ".DOC" Or fi.Extension.ToUpper = ".DOCX" Then
ListaArchivos.Add(fi.FullName)
ListaNombres.Add((fi.Name.Substring(0, fi.Name.Length - fi.Extension.Length)))
End If
Next
To search a directory with multiple sub directories for a file you can use the following line of code:
Dim folders As List(Of String) = New DirectoryInfo("C:\Test").EnumerateFiles("test.txt", SearchOption.AllDirectories).[Select](Function(d) d.FullName).ToList()
Replace "C:\Test" with the directory to search and "test.txt" with the filename to search for. If the same file name is in multiple directories all iles matching that name will be returned.
To find multiple file names, just loop through your list(file_name) replacing "test.txt" with the file. How you save all the output is up to you. You can add everything found to 1 large list and then convert them all to pdf's, or you can convert them as you find them.
Here is an example:
Private Function FindFiles() As List(Of String)
Dim allFilesFound As New List(Of String)
Dim dirSearch = "C:\Test"
Dim file_name As New List(Of String) From {"test.txt", "test2.txt"}
For Each file In file_name
allFilesFound.AddRange(New DirectoryInfo(dirSearch).EnumerateFiles(file, SearchOption.AllDirectories).[Select](Function(d) d.FullName).ToList())
Next
Return allFilesFound
End Function
Keep in mind that the more files in file_name and the farther up the Directory tree you start searching from, the longer this will take. If you are dealing with large amounts of files, then consider using recursion so you can find all files in one pass. You can take a look here Recursively search folder with subdirectories in .NET and modify the code demonstrated there to fit your search pattern.

How to transfer a file from one directory to another that is the newest file

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

Read File name and put it into Variable

I am trying to find a way to read a specific file name in a directory and then put the file name into a variable. In my program I have a batch file that zips a folder called logs and then changes the name of zip to username_date_time.zip.
So basically if the filename was jobs_03152015_1315.zip I would want the entire file name but not the path stored into a variable. The file starts off on the user's local machine. It is then uploaded into a network share.
The network path will be uploaded to a database for others to view. I want to just add the unique file name to the end of a pre set path. Here is the code I am using.
Dim filePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(filePath)) Then
For Each file As String In filePath
If file.Contains(".zip") Then
Dim zip As String = file
testbox.Text = zip
Exit For
End If
Next
End If
You can enumerate all files using the GetFiles() method of the static (shared in VB) Directory class, see msdn.
Assuming you have more then 1 file I add the files to a listbox control. But you could change that if you wish. I get the filename using the Path class. You will find a lot other helpful functions in this class.
Dim searchPath = "C:\temp\logs"
Dim files = Directory.GetFiles(path, "*.zip")
For Each file In files
listbox1.Items.Add(path.GetFileName(file))
Next
This should do it.
Your loop doesn't read anything. The string variable filePath is just the name of the directory, not a list of the files in that directory. Calling For Each over a string simply enumerates each character contained in the string
To get a list of files contained in that folder you need Directory.EnumerateFiles() and pass the filePath variable and the extension required.
It seems that you are interested only to find if that folder contains at least one file with zip extension. If this is the case then you could remove the explicit loop and write simply
Dim file = Directory.EnumerateFiles(filePath, "*.zip").FirstOrDefault()
If file IsNot Nothing Then
testbox.Text = Path.GetFileName(file)
End If
Using Path.GetFileName will return just the file without the path part
Try this code :
Dim FilePath As String = "c:\temp\logs\"
If (System.IO.Directory.Exists(FilePath)) Then
For Each File As String In System.IO.Directory.EnumerateFiles(FilePath)
If File.Contains(".zip") Then
Dim info As New System.IO.FileInfo(File)
zip = info.Name
testbox.Text = zip
Exit For
End If
Next
End If