Get creation date of multiple files in vb - vb.net

I'm trying to pull the creation date of all the files (1000+) in a folder on a local server to list in an excel file. I've been trying to use FileSystemInfo for this, but it doesn't seem to work to get the creation date of MULTIPLE files because it doesn't allow me to use wildcard characters to read all (not even sure if that's what I'll need to do). Has anyone ever done this?

This should help you loop through all of the files:
Dim dirinfo As New DirectoryInfo("C:\somefolder")
For Each fsi As FileSystemInfo In dirinfo.GetFileSystemInfos()
debug.print(fsi.CreationTime)
Next
Some things to read up on:
For loops
FileSystemInfo Class
DirectoryInfo Class

Related

VB.NET file filter

I want get the oldest creation file in a directory but want to exclude the file ‘Startup’(Which is currently the oldest file). So I would like to skip that file and get the next oldest creation file in the directory which would be ‘TEST’.
This code only gets the Startup.
Dim oldestFile = folderlist.OrderBy(Function(fi) fi.CreationTime).First
1:
You use the Where function to filter.
Dim folder As New DirectoryInfo(folderPath)
Dim oldestFile = folder.EnumerateFiles().
Where(Function(fi) fi.Name <> "Startup.txt").
OrderBy(Function(fi) fi.CreationTime).
First()
Note that it is preferable to use EnumerateFiles rather than GetFiles unless you really want access to the full array of files after the fact. If you only need that one file, EnumerateFiles is better because it doesn't get all the files first and then perform the other operations on that array. You may already know this but most people don't at first.
Note also that I'm assuming that those are text files based on the icons. Change the name in the filter if they are something else. As a developer, you really ought to switch off the File Explorer feature that hides file extensions. That's for people who don't understand computers.

How can I get the names a files inside a directory

Good day all
I have a test path name ""C:\test". There are the following folders in my directory:
importantStuff
UselessStuff
TopSecret
My question is this: How can I have Visual Basic return these exact names if I input the path "C:\test"? I have tried to use Directory.GetFiles(path), but it returns the path of those folders, and not their names. I'm stuck with this.
Just as a note: I am trying to port a program I wrote in python to vb.net. In python there is the function os.listdir(path). I essentialy want the vb.net equivalent of this
Try this
Dim direct As New DirectoryInfo(".\")
'Get the files based on .txt extension
Dim files As FileInfo() = direct.GetFiles("*.*")
'loop through each files and add it to Listbox control
For Each file As FileInfo In files
ComboBox.Items.Add(System.IO.Path.GetFileNameWithoutExtension(file.Name))
Next

Visual Basic FileSystem.GetFiles

I'm making a console application and I want to see what files is in a folder
For Each foundFile As String In My.Computer.FileSystem.GetFiles("c:\users\zac\desktop\booked vehicle\requested\")
Console.WriteLine(foundFile)
Next
after using this code and find that the folder is empty I need an If statement that say's
If foundfile has no files then
tell user no files found
end if
but I don't know how to write this so Visual Basic understands.
Load the files into a variable then check the count.
Dim files = My.Computer.FileSystem.GetFiles("c:\users\zac\desktop\booked vehicle\requested\")
If files.Count = 0 Then
'tell user no files
Else
For Each file In files
Console.WriteLine(file)
Next
End If
FileSystem.GetFiles() returns a collection of file name strings. As OneFineDay showed, you can use the collection's Count property to know if any files were found.
The downside of using FileSystem.GetFile() is that it has to search the entire folder before then returning the entire list of filenames. If you are searching large folders and speed is an issue, consider using Directory.EnumerateFiles() instead. That way, you can output a message if no file was found, otherwise loop throuh the list of found files. For example:
Dim files = Directory.EnumerateFiles("c:\users\zac\desktop\booked vehicle\requested\").GetEnumerator()
If files.MoveNext Then
' files were found
Do
Console.WriteLine(files.Current)
Loop Until Not files.MoveNext
Else
' no files were found
End If
I personally would use Linq to accomplish this. It's very quick and efficient to use in this case. I put the Console.ReadLine() at the end to show the files, you can remove this if need to be. Also you can change the Console.WriteLine to not include the string (s) if you don't want to. The s was declared if you want to show the files and also to see if there are any files. As I said, this was for my viewing to see the files. Straight to the point!
Dim s As String = String.Join(Environment.NewLine, New DirectoryInfo("YOUR DIRECTORY").GetFiles().[Select](Function(file) file.Name).ToArray)
Console.WriteLine(If(s.Length > 0, s, "No files found!"))
Console.ReadLine()

Script > Search directory for multiple file names and then delete them

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”)

Create unique files in loop

Im looking for a way to create unique files in a loop, meaning that I want them to have different names for each time if loops. Im writing something that finds some values from a html and does that in a loop. It doesnt matter what the file is called since its deleted shortly after (just a temporary file to store two lines) but they have to be saved in the same directory. Can I create a sort of array for the files? I have a feeling that its quite easy but I just can't see it at the moment.
Hope you can help me.
Cheers,
Snoop
If it doesn't matter at all you could use a Guid:
Dim directory = "Folder-Path"
Dim fileName = String.Format("{0}.txt", Guid.NewGuid())
Dim fullPath = System.IO.Path.Combine(directory, fileName)