vicki vicki - 14 days ago 7
Vb.net Question

Rename files sequentially

with reference to Renaming all files in a folder

on running the below code, get type def errors:
Type 'DirectoryInfo' is not defined
Type 'FileInfo' is not defined

how to resolve these errors. Please suggest.

Dim sourcePath As String = "E:\testrenamerbackup\vbdotnet"
Dim searchPattern As String = "*.doc"
Dim curDir As New DirectoryInfo(sourcePath)
Dim i As Integer = 0
For Each fi As FileInfo In curDir.GetFiles(searchPattern).OrderBy(Function(num) num.CreationTime)
File.Move(fi.FullName, Path.Combine(fi.Directory.FullName, "docFile_" & i & ".doc"))
i += 1
Next

Answer

Add Import System.IO on top of your vb.net class file, for example

Imports System
Imports System.IO

Public Class Test
    Public Shared Sub Main()
     ' your code...
    End Sub
End Class

System.IO Namespace contains types that allow reading and writing to files and data streams, and types that provide basic file and directory support including DirectoryInfo, FileInfo. by adding Import System.IO you can use those types and methods in the namespace.