[CheatSheet] - Filesystem APIs

This CheatSheet introduces the basic use of available functionalities provided by the .Net API in order to deal with the filesystem. With further examples also special cases are presented.

Basic Usage

System.IO.Directory

Directory offers some static methods for basic operations on folder and subfolders

arrFullFilePaths | String() = Directory.GetFiles("C:\_demo\FileSystem\SubfolderA")

Visuals

grafik

grafik

grafik

System.IO.DirectoryInfo

The DirectoryInfo class offers instance methods for basic operations on folder and subfolders

arrFilesFI = new DirectoryInfo("C:\_demo\FileSystem\SubfolderA").GetFiles

Visuals

grafik

System.IO File / FileInfo

Similar to above we do have the same for files

File.GetCreationTime("C:\_demo\FileSystem\SubfolderA\Inventory_2020.xlsx")
new FileInfo("C:\_demo\FileSystem\SubfolderA\Inventory_2020.xlsx").CreationTime
Visuals

grafik

File Retrieval

MyDirPathVar = "C:\_demo\FileSystem\SubfolderA"

arrFullFilePaths | datatype: String() =

Directory.GetFiles(MyDirPathVar)
Directory.GetFiles(MyDirPathVar,"*.txt")
Directory.GetFiles(MyDirPathVar,"Report*.xlsx")
Directory.GetFiles(MyDirPathVar,"*.*",SearchOption.AllDirectories)

arrFilesFI | datatype: FileInfo() =

New DirectoryInfo(MyDirPathVar).GetFiles()
New DirectoryInfo(MyDirPathVar).GetFiles("Report*.xlsx")
New DirectoryInfo(MyDirPathVar).GetFiles("*.*",SearchOption.AllDirectories)

Folder Retrieval

Directory.GetDirectories("C:\_demo\FileSystem\")
Directory.GetDirectories("C:\_demo\FileSystem","*",SearchOption.AllDirectories)
new DirectoryInfo("C:\_demo\FileSystem").GetDirectories()
new DirectoryInfo("C:\_demo\FileSystem").GetDirectories("*", SearchOption.AllDirectories)

FileSystem Entries Retrieval

Directory.GetFileSystemEntries("C:\_demo\FileSystem")
Directory.GetFileSystemEntries("C:\_demo\FileSystem","*", SearchOption.AllDirectories)
new DirectoryInfo("C:\_demo\FileSystem").GetFileSystemInfos()
new DirectoryInfo("C:\_demo\FileSystem").GetFileSystemInfos("*", SearchOption.AllDirectories)
new DirectoryInfo("C:\_demo\FileSystem").EnumerateFiles("*",SearchOption.AllDirectories)

File Property Retrieval

Path.GetFileNameWithoutExtension("C:\Temp\abc.txt")
New FileInfo("C:\Temp\abc.txt").Extension
File.GetCreationTime("C:\Temp\abc.txt")
New FileInfo("C:\Temp\abc.txt").CreationTime

File Paths

Path.Combine("C:\","Temp","abc.txt")
Path.GetDirectoryName("C:\Temp\abc.txt")
new FileInfo("C:\Temp\abc.txt").DirectoryName
New FileInfo("C:\Temp\SubfolderA\abc.txt").Directory.Parent
New FileInfo("C:\Temp\SubfolderA\abc.txt").Folder
New FileInfo("C:\Temp\SubfolderA\abc.txt").Folder.Split({"\"}, StringSplitOptions.RemoveEmptyEntries)
("C:\Temp\SubfolderA\abc.txt").Folder.Split({"\"}, StringSplitOptions.RemoveEmptyEntries)

File Retrieval - Advanced Filtering

arrFilesFI  = new DirectoryInfo("C:\").GetFiles().Where(Function (x) x.CreationTime.Date < Now.Date).toArray
myFileInfo = new DirectoryInfo("C:\").GetFiles().OrderBy(Function (x) x.CreationTime).Last()
myFileInfo = new DirectoryInfo("C:\").GetFiles().OrderBy(Function (x) x.CreationTime).First()

Filter on: x.CreationTime.Date < Now.Date - get all files created before today
Retrieve: x.CreationTime).Last() - newest created file FileInfo
Retrieve: x.CreationTime).First() - oldest created file FileInfo


Filter all file names matching a regex pattern:

(From fi In New DirectoryInfo("C:\_RPA\_Demo").GetFiles()
Where Regex.IsMAtch(fi.Name,"\(\d+\)\.docx$", RegexOptions.IgnoreCase)
Select fi.FullName).toArray

Filter on multiple file extensions:

new DirectoryInfo("C:\").GetFiles().Where(Function (x) {".pdf",".txt"}.Contains(x.Extension)).toArray

References

Questions

For questions on your file system API case open a new topic and get individual support

15 Likes