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.
Related
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?
Part of a program I am making I need to open a file (for example a txt file) from the folder where the program was opened.
The idea is that it can be zipped up and put anywhere without having to place the file in a certain location.
It's got to be Visual Basic and I will really appreciate some help.
I have googled this but found nothing for VB. I'm relatively new to the language.
Thanks, Jack
To open the file do this:
Dim fileName as String = "yourfile.txt"
Dim appDir as String = System.IO.Path.GetDirectoryName( _
System.Reflection.Assembly.GetExecutingAssembly().CodeBase)
Process.Start(appDir & "\" & fileName)
You can use this to get the path to the folder where the currently executing assembly (i.e. the EXE) is located:
System.Reflection.Assembly.GetExecutingAssembly().Location.Substring(0, assembly.Location.LastIndexOf(System.IO.Path.DirectorySeparatorChar))
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.
I'm trying to separate files in a folder by extension, using the code below:
Dim file_list3 As String() = Directory.GetFiles(path, "*.xls")
Dim file_list4 As String() = Directory.GetFiles(path, "*.xlsm")
but all of the ".xls" files end up in list 4 along with the ".xlsm" files. How can I keep the subset of just ".xls" flies out of the list of ".xlsm" files?
Thanks.
I found the answer:
Dim file_list3 = Directory.GetFiles(path, "*.xls").Where(Function(item) item.ToLower().EndsWith(".xls"))
Dim file_list4 = Directory.GetFiles(path, "*.xlsm").Where(Function(item) item.ToLower().EndsWith(".xlsm"))
Thanks.
I finally managed to reproduce the behavior documented.
It seems that I have set this command on my working pc
fsutil 8dot3name set 0
this command disables the creation of the old 8.3 short filenames required at MS-DOS time.
With this configuration the command Directory.GetFiles(path, "*.xls") returns only the files that have exactly the extension specified and not other extensions that starts with XLS.
Applying the command
fsutil 8dot3name set 1
I expected to have the same result explained by the OP, but this is not the case with files already in the folder because they were created when the 8dot3name flag was disabled.
But a new file named test.xlsm started to appear between the files returned by the search pattern *.xls
So, to fix your problem you could use the fsutil command as explained in the article How to disable 8.3 file name creation on NTFS partitions and perhaps strip away the old 8.3 entries with the strip option or just use a slight variation of your code above
Dim all_files = Directory.EnumerateFiles(path, "*.xls")
Dim file_list3 = all_files.Where(Function(x) Path.GetExtension(x).ToLower = ".xls").ToList()
Dim file_list4 = all_files.Where(Function(x) Path.GetExtension(x).ToLower = ".xlsm").ToList()
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