TheDeveloperBlog.com

Home | Contact Us

C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML

<< Back to VBNET

VB.NET Sort by File Size

Get an array of files and sort them by their sizes, from largest to smallest. Use OrderByDescending.
Sort files, size. Files have varying sizes. Some are larger than others. In VB.NET, we can use Directory.GetFiles and OrderByDescending along with the FileInfo type to sort them from largest to smallest.
Example. This program scans the "C:\programs" directory, but you can change this. We first invoke Directory.GetFiles, which returns a string array of the files in the directory. Next, we invoke the OrderByDescending method.String Array

OrderByDescending: The OrderByDescending call is more complex. It receives a Func argument—this is an anonymous method.

Func: The Func method is created with anonymous method syntax. The Function receives a string (the file name) and returns its length.

So: The OrderByDescending method sorts the files by their lengths. We then convert its result (an IEnumerable) into an array with ToArray.

ToArray
VB.NET program that sorts files by size Imports System.IO Module Module1 Sub Main() ' The target directory. Dim dir As String = "C:\\programs" ' Get files. Dim files() As String = Directory.GetFiles(dir) ' Order files from largest size to smallest size. Dim sorted() As String = files.OrderByDescending( New Func(Of String, Integer)(Function(fn As String) Return New FileInfo(fn).Length End Function)).ToArray ' Loop and display files. For Each element As String In sorted Console.WriteLine(element) Console.WriteLine(New FileInfo(element).Length) Next End Sub End Module Output C:\\programs\file.py 611 C:\\programs\file.rb 369 C:\\programs\file.php 235 C:\\programs\file.pl 184
In the output, we see that the folder contains four script files. The largest is file.py—it is 611 bytes. The smallest meanwhile is file.pl which is 184 bytes. They are correctly ordered in descending numeric order.

Tip: This program is inefficient. If you built a data structure (a List of KeyValuePairs) you could avoid accessing FileInfo so many times.

ListKeyValuePair
Summary. This program combined many functions in VB.NET. It uses System.IO functions to get a file array and their sizes. And with OrderByDescending we specify a lambda expression (an anonymous method) to sort the file names.
© TheDeveloperBlog.com
The Dev Codes

Related Links:


Related Links

Adjectives Ado Ai Android Angular Antonyms Apache Articles Asp Autocad Automata Aws Azure Basic Binary Bitcoin Blockchain C Cassandra Change Coa Computer Control Cpp Create Creating C-Sharp Cyber Daa Data Dbms Deletion Devops Difference Discrete Es6 Ethical Examples Features Firebase Flutter Fs Git Go Hbase History Hive Hiveql How Html Idioms Insertion Installing Ios Java Joomla Js Kafka Kali Laravel Logical Machine Matlab Matrix Mongodb Mysql One Opencv Oracle Ordering Os Pandas Php Pig Pl Postgresql Powershell Prepositions Program Python React Ruby Scala Selecting Selenium Sentence Seo Sharepoint Software Spellings Spotting Spring Sql Sqlite Sqoop Svn Swift Synonyms Talend Testng Types Uml Unity Vbnet Verbal Webdriver What Wpf