|
Create a Touch utility
|
Total Hit (3284) |
The SetCreationTime, SetLastWriteTime, and SetLastAccessTime methods of the System.IO.Directory class let you modify the date attributes of a file or directory:
«Code LangId=2»
' Change the access date- and time of all files in C:\DOCS.
For Each fname In Directory.GetFiles("c:\docs")
File.
....Read More |
Rating
|
|
|
Create all the subdirectories for a new folder
|
Total Hit (3839) |
The System.IO.Directory class exposes a CreateDirectory method that, as its name suggests, can create a subdirectory. However, this method is even more powerful than it appears at a first glance, in that it can even create all the intermediate subdirectories, if necessary. In other words, the follow
....Read More |
Rating
|
|
|
Create directory paths
|
Total Hit (3887) |
Visual Basic 6's MkDir command, as well as the DOS MD command, can create only one subdirectory and fail if any subdirectory on the specified path doesn't exist:
«Code LangId=2»
' This VB6 statement fails if C:\MyApp doesn't exist
MKDIR "c:\MyApp\MyDir"
«/Code»
Conversely, the Directory.Crea
....Read More |
Rating
|
|
|
Display a directory tree
|
Total Hit (4038) |
Thanks to the GetDirectories and GetFiles methods of the System.IO.Directory class, you need very little code to iterate over all the directories and files of a directory tree. For example, the following code snippet prints the structure of a directory tree and (optionally) the name of files in each
....Read More |
Rating
|
|
|
Filter file names on their names or attributes
|
Total Hit (2849) |
The GetFiles and GetDirectories methods of the System.IO.Directory class can take an argument containing wildcards, to filter the result:
«Code LangId=2»
' Display all the *.txt files in C:\DOCS.
Dim fname As String
For Each fname In Directory.GetFiles("c:\docs", "*.txt")
Console.WriteLin
....Read More |
Rating
|
|
|
Reading text files
|
Total Hit (3974) |
In Visual Basic .NET you read a text file by first opening a StreamReader on it, and then iterating over all its lines until its Peek method returns -1:
«Code LangId=2»
Dim sr As New StreamReader("c:\autoexec.bat")
' Display all the text lines in the file.
Do Until sr.Peek = -1
' The Re
....Read More |
Rating
|
|
|
Retrieve file version information
|
Total Hit (3716) |
The System.Diagnostics.FileVersionInfo class lets you easily read file version information without having to call Windows API functions (as you have to do under previous VB versions). This class has a static GetVersionInfo method that returns a FileVersionInfo object related to a given file. Once yo
....Read More |
Rating
|
|
|
AddBackslash - Append a backslash to a path if needed
|
Total Hit (4183) |
«Code LangId=2»
' Append a backslash (or any character) at the end of a path
' if it isn't there already
Function AddBackslash(ByVal Path As String, Optional ByVal ch As Char = "\"c) _
As String
If Not Path.EndsWith(ch) Then
AddBackslash = Path & ch
Else
AddBack
....Read More |
Rating
|
|
|
ChangeFileExtension - Modify the extension in a file name
|
Total Hit (3016) |
«Code LangId=2»
' Change the extension of a file name
' if the last argument is True, it adds the extension even if the file doesn't
' have one
Function ChangeFileExtension(ByVal FileName As String, _
ByVal Extension As String, Optional ByVal AddIfMissing As Boolean = False) _
As St
....Read More |
Rating
|
|
|
CompareFiles - Comparing two binary/text files
|
Total Hit (2836) |
«Code LangId=2»' Returns a boolean indicating whether two files are equal
' Example: Debug.WriteLine(CompareFiles("D:\File1.mdb", "D:\File2.mdb"))
Function CompareFiles(ByVal path1 As String, ByVal path2 As String) As Boolean
Dim file1 As New System.IO.FileInfo(path1)
Dim file2 As New
....Read More |
Rating
|
|
|
ConcatenateFiles - Concatenating multiple text files
|
Total Hit (2908) |
«Code LangId=2»
' Concatenate a variable number of text files into a single result file
'
' Params:
' - resultFile: the complete path of the result file you want to create
' - header: a string that is written when a file is added to the result file.
' Note: this string can contain the #Fi
....Read More |
Rating
|
|
|
CopyDirectory - Copy a directory
|
Total Hit (3117) |
«Code LangId=2»
' Copies a source directory to the destination directory.
' The last parameter specifies whether the files already present in the
' destination directory will be overwritten
' - Note: requires Imports System.IO
' - Usage: CopyDirectory("C:\Misc", "D:\MiscBackup")
Sub CopyDire
....Read More |
Rating
|
|
|
FolderHasFiles - Returns whether the specified folder has files
|
Total Hit (2753) |
«Code LangId=2»
' Returns a boolean indicating whether the specified folder has files
Function FolderHasFiles(ByVal folderPath As String) As Boolean
Return System.IO.Directory.GetFiles(folderPath).Length > 0
End Function
«/Code»
|
Rating
|
|
|
|
GetDirectorySize - Calculate the size of a directory
|
Total Hit (2874) |
«Code LangId=2»
' Returns the size of the specified directory
' - Note: requires Imports System.IO
' - Usage: Dim DirSize As Long = GetDirectorySize("D:\Projects")
Function GetDirectorySize(ByVal DirPath As String) As Long
Dim DirSize As Long
Dim Dir As DirectoryInfo = New DirectoryInfo(
....Read More |
Rating
|
|
|
|
IsDriveReady - Returns whether a drive is ready
|
Total Hit (3239) |
«Code LangId=2»
' Returns a boolean indicating whether a given drive is ready
' Example: check if the floppy disk drive is ready
' Debug.WriteLine(IsDriveReady("a"))
Function IsDriveReady(ByVal driveLetter As String) As Boolean
If driveLetter.Length = 1 Then driveLetter &= ":\"
Dim
....Read More |
Rating
|
|
|
IsExecutableFile - Returns whether the file is an executable
|
Total Hit (2769) |
«Code LangId=2»
' Returns a boolean indicating whether the file is an executable
Function IsExecutableFile(ByVal filePath As String) As Boolean
' add more extensions if you wish
Dim extensions() As String = New String() {".exe", ".bat", ".com", ".pif"}
' return true if the extension
....Read More |
Rating
|
|
|
IsImageFile - Returns whether the file is an image
|
Total Hit (3757) |
«Code LangId=2»
' Returns a boolean indicating whether the file is an image
Function IsImageFile(ByVal filePath As String) As Boolean
' add more extensions if you wish
Dim extensions() As String = New String() {".bmp", ".jpg", ".jpeg", ".gif", _
".tif", ".tiff", ".png", ".tga",
....Read More |
Rating
|
|
|
IsValidPath - Validating a system path
|
Total Hit (4669) |
«Code LangId=2»' Validate a system path
' Example:
' MessageBox.Show(IsValidPath("C:\Test\Memo.txt")) ' => True
' MessageBox.Show(IsValidPath("\\RemotePC\Test\Memo.txt")) ' => True
' MessageBox.Show(IsValidPath("C:\Test\Mem|o.txt")) ' => False
Function IsValidPath(ByVal p
....Read More |
Rating
|
|
|
LoadTextFile - Load the contents of a text file
|
Total Hit (2872) |
«Code LangId=2»
' Returns the content of the specified text file
' Note: it can throw an exception
' Usage: Dim FileContent As String = LoadTextFile("C:\Autoexec.bat")
Function LoadTextFile(ByVal FilePath As String) As String
Dim sr As System.IO.StreamReader
Try
sr = New
....Read More |
Rating
|
|
|
SaveTextFile - Save or append text to a file
|
Total Hit (3733) |
«Code LangId=2»
' Saves a text file. If the destination file already exists,
' its content can be replaced, or the new content can be appended
' at the end of the file, according to the last parameter
' Note: the destination directory must exist, otherwise the file is not
' saved and th
....Read More |
Rating
|
|
|
SearchFileInDirTree - Searches a file on a directory tree
|
Total Hit (2924) |
«Code LangId=2»
<System.Runtime.InteropServices.DllImport("imagehlp.dll")> Shared Function _
SearchTreeForFile(ByVal rootPath As String, ByVal inputPathName As String, _
ByVal outputPathBuffer As System.Text.StringBuilder) As Boolean
End Function
' Returns the complete path+name of th
....Read More |
Rating
|
|
|
SearchFileOnPath - Searching a file on the system
|
Total Hit (2783) |
«Code LangId=2»<System.Runtime.InteropServices.DllImport("kernel32")> Shared Function _
SearchPath(ByVal tartPath As String, ByVal fileName As String, _
ByVal extension As String, ByVal bufferLength As Integer, _
ByVal buffer As System.Text.StringBuilder, ByVal filePart As String) As
....Read More |
Rating
|
|
|
|
|
|
|
|
How Do I...Read a text file?
|
Total Hit (1111) |
This sample retrieves information from a text file, and uses an ArrayList to display that information for the user.
Opening and reading files for read access is an important part of IO functionality, even if you do not need to write to the file in question. In this example, we open a file for read
....Read More |
Rating
|
|