Search icon CANCEL
Subscription
0
Cart icon
Your Cart (0 item)
Close icon
You have no products in your basket yet
Save more on your purchases! discount-offer-chevron-icon
Savings automatically calculated. No voucher code required.
Arrow left icon
Explore Products
Best Sellers
New Releases
Books
Videos
Audiobooks
Learning Hub
Newsletter Hub
Free Learning
Arrow right icon
Arrow up icon
GO TO TOP
Visual Basic Quickstart Guide

You're reading from   Visual Basic Quickstart Guide Improve your programming skills and design applications that range from basic utilities to complex software

Arrow left icon
Product type Paperback
Published in Oct 2023
Publisher Packt
ISBN-13 9781805125310
Length 238 pages
Edition 1st Edition
Languages
Arrow right icon
Author (1):
Arrow left icon
Aspen Olmsted Aspen Olmsted
Author Profile Icon Aspen Olmsted
Aspen Olmsted
Arrow right icon
View More author details
Toc

Table of Contents (27) Chapters Close

Preface 1. Part 1:Visual Basic Programming and Scripting
2. Chapter 1: The Visual Basic Family of Programming Languages FREE CHAPTER 3. Chapter 2: Console Input and Output 4. Chapter 3: Data Types and Variables 5. Chapter 4: Decision Branching 6. Chapter 5: Iteration 7. Chapter 6: Functions and Procedures 8. Chapter 7: Project Part I 9. Part 2:Visual Basic Files and Data Structures
10. Chapter 8: Formatting and Modifying Data 11. Chapter 9: File Input and Output 12. Chapter 10: Collections 13. Chapter 11: Project Part II 14. Part 3:Object-Oriented Visual Basic
15. Chapter 12: Object-Oriented Programming 16. Chapter 13: Inheritance 17. Chapter 14: Polymorphism 18. Chapter 15: Interfaces 19. Chapter 16: Project Part III 20. Part 4:Server-Side Development
21. Chapter 17: The Request and Response Model 22. Chapter 18: Variable Scope and Concurrency 23. Chapter 19: Project Part IV 24. Chapter 20: Conclusions 25. Index 26. Other Books You May Enjoy

Working with directories and files

We will utilize two different methods for VB.NET and older VB family members. We will see this pattern throughout this chapter as Microsoft added new file features to the .NET language that you can leverage in VB.NET and ASP.NET.

Working with files in VB.NET

In VB.NET, you use the File class from the System.IO namespace to work with files. The File class provides static methods for performing various file-related operations.

To create a file, you can use the Create method, which returns a FileStream object you can later use to write data to the file:

Imports System.IO
File.Create("C:\Path\File.txt")

To delete a file, you can use the Delete method:

Imports System.IO
File.Delete("C:\Path\ File.txt")

To check whether a file exists, you use the Exists property, which returns a Boolean value indicating whether the file exists or not:

Imports System.IO
If File.Exists("C:\Path\File.txt") Then
    Console.Writeline("The file exists")
End If

You use the Copy method to copy a file from one location to another:

Imports System.IO
File.Copy("C:\Path\File.txt", "C:\Path\NewFile.txt")

To move or rename a file, you use the Move method:

Imports System.IO
File.Move("C:\Path\File.txt", "C:\Path\ NewFile.txt")

To retrieve the file attributes, use the GetAttributes method and the FileAttributes class:

Imports System.IO
Dim attr As FileAttributes = File.GetAttributes("C:\Path\ File.txt")
If attr.HasFlag(FileAttributes.ReadOnly)Then
    Console.Writeline("The file is read-only")
End If
If attr.HasFlag(FileAttributes.Hidden) Then
    Console.Writeline("The file is hidden")
End If

Next, we will look at working with directories in VB.NET.

Working with directories in VB.NET

In VB.NET, you can use the Directory class from the System.IO namespace to work with directories. The Directory class provides static methods for performing various directory-related operations.

To create a directory, you use the CreateDirectory method:

Imports System.IO
Directory.CreateDirectory("C:\Path\NewDirectory")

To delete a directory, you use the Delete method. With the second parameter, you can specify whether to delete the directory recursively or not:

Imports System.IO
Directory.Delete("C:\Path\Directory", recursive:=True)

To check whether a directory exists, you use the Exists property, which returns a Boolean value indicating whether the directory exists or not:

Imports System.IO
If Directory.Exists("C:\Path\Directory") Then
    Console.Writeline("The directory exists")
End If

To move or rename a directory, you can use the Move method:

Imports System.IO
Directory.Move("C:\Path\OldDir", "C:\Path\NewDir")

To retrieve the files or subdirectories within a directory, you use the GetFiles and Get Directories methods:

Imports System.IO
Dim files As String() = Directory.GetFiles("C:\Path\Dir")
Dim dirs As String() = Directory.GetDirectories("C:\Path\Dir")

The GetFiles and GetDirectories methods return collections of values, which we will explore in Chapter 10.

To retrieve information about a directory, you can use the GetDirectoryInfo method to get a DirectoryInfo object. You can use this DirectoryInfo object to get files and directories, as in our last example, but from the object:

Imports System.IO
Dim dirInfo As DirectoryInfo = My.Computer.FileSystemGetDirectoryInfo("C:\Path\Dir")
Dim files() As FileInfo = dirInfo.GetFiles()

You retrieve various attributes of a directory using the GetAttributes method, just as you did for a file previously:

Imports System.IO
Dim attr As FileAttributes = File.GetAttributes("C:\Path\Dir")
If attr.HasFlag(FileAttributes.ReadOnly) Then
    Console.Writeline("The directory is read-only")
End If
If attr.HasFlag(FileAttributes.Hidden) Then
    Console.Writeline("The directory is hidden")
End if

Next, we will look at working with files in VBScript, VBA, and VB6.

Working with files in older VB family members

Working with files and directories in older VB family members (VBScript, VBA, and VB6) involves using the file system object (FSO) provided by the Windows Script Host. The FSO allows you to perform many operations on files and directories.

To create a directory with FSO, you use the CreateFolder method:

Set myFSO = CreateObject("Scripting.FileSystemObject")
myFSO.CreateFolder("C:\Path\Directory")

To delete a directory with FSO, you use the DeleteFolder method. The second parameter specifies whether to delete the directory recursively or not:

Set myFSO = CreateObject("Scripting.FileSystemObject")
myFSO.DeleteFolder("C:\Path\Directory", True)

To check whether a directory exists with FSO, you can use the FolderExists method, which returns a Boolean value indicating whether the directory exists or not:

Set myFSO = CreateObject("Scripting.FileSystemObject")
If myFSO.FolderExists("C:\Path\Directory") Then
     Wscript.Echo "The directory exists"
End If

To rename or move a directory with FSO, you use the MoveFolder method:

Set myFSO = CreateObject("Scripting.FileSystemObject")
myFSO.MoveFolder "C:\Path\OldDir", C:\Path\NewDir"

Next, we will look at working with directories in VBScript, VBA, and VB6.

Working with directories in older VB family members

To create a file with FSO, you use the CreateTextFile method, which has optional second and third parameters to specify whether the file should be overwritten and whether it should be created in Unicode format:

Set myFSO = CreateObject("Scripting.FileSystemObject")
Set myFile = myFSO.CreateTextFile("C:\Path\File.txt", True)

To delete a file with FSO, you use the DeleteFile method, which has an optional second parameter to specify whether only read-only files should be deleted:

Set myFSO = CreateObject("Scripting.FileSystemObject")
myFSO.DeleteFile("C:\Path\File.txt")

To check whether a file exists with FSO, you use the FileExists method, which returns a Boolean value indicating whether the file exists or not:

Set myFSO = CreateObject("Scripting.FileSystemObject")
If myFSO.FileExists("C:\Path\File.txt") Then
     Wscript.Echo "The file exists"
End If

To rename or move a file with FSO, you use the MoveFile method:

Set myFSO = CreateObject("Scripting.FileSystemObject")
myFSO.MoveFile "C:\Path\Old.txt", "C:\Path\New.txt"

Next, we will look at writing data in text format into files.

lock icon The rest of the chapter is locked
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $19.99/month. Cancel anytime
Banner background image