Convert VBA to VB.NET

M

Martin

I would like to convert this code to VB.NET. Can someone help.

Thanks.

Dim X()
Dim i As Long
Dim objShell, objFolder, objFolderItem
Dim FSO, oFolder, Fil

Sub MainExtractData()

Dim NewSht As Worksheet
Dim MainFolderName As String

ReDim X(1 To 65536, 1 To 11)

Set objShell = CreateObject("Shell.Application")

Application.ScreenUpdating = False
MainFolderName = "c:\mydir"
Set NewSht = ThisWorkbook.Sheets.Add

X(1, 1) = "Path"
X(1, 2) = "File Name"
X(1, 3) = "Last Accessed"
X(1, 4) = "Last Modified"
X(1, 5) = "Created"
X(1, 6) = "Type"
X(1, 7) = "Size"
X(1, 8) = "Owner"
X(1, 9) = "Author"
X(1, 10) = "Title"
X(1, 11) = "Comments"

i = 1

Set FSO = CreateObject("scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(MainFolderName)
On Error Resume Next
For Each Fil In oFolder.Files
Set objFolder = objShell.Namespace(oFolder.Path)
Set objFolderItem = objFolder.ParseName(Fil.Name)

i = i + 1
X(i, 1) = oFolder.Path
X(i, 2) = Fil.Name
X(i, 3) = Fil.DateLastAccessed
X(i, 4) = Fil.DateLastModified
X(i, 5) = Fil.DateCreated
X(i, 6) = Fil.Type
X(i, 7) = Fil.Size
X(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
X(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
X(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
X(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
Next

Call RecursiveFolder(oFolder)
Range("a1:K65536") = X
Range("A:K").EntireColumn.AutoFit
Set FSO = Nothing
Set objShell = Nothing
Set oFolder = Nothing
Set objFolder = Nothing
Set objFolderItem = Nothing
Set Fil = Nothing
Application.ScreenUpdating = True
End Sub

Sub RecursiveFolder(xFolder)
Dim SubFld
For Each SubFld In xFolder.SubFolders
Set oFolder = FSO.GetFolder(SubFld)
Set objFolder = objShell.Namespace(SubFld.Path)
For Each Fil In SubFld.Files
Set objFolder = objShell.Namespace(oFolder.Path)
If Not objFolder Is Nothing Then
Set objFolderItem = objFolder.ParseName(Fil.Name)
i = i + 1
X(i, 1) = SubFld.Path
X(i, 2) = Fil.Name
X(i, 3) = Fil.DateLastAccessed
X(i, 4) = Fil.DateLastModified
X(i, 5) = Fil.DateCreated
X(i, 6) = Fil.Type
X(i, 7) = Fil.Size
X(i, 8) = objFolder.GetDetailsOf(objFolderItem, 8)
X(i, 9) = objFolder.GetDetailsOf(objFolderItem, 9)
X(i, 10) = objFolder.GetDetailsOf(objFolderItem, 10)
X(i, 11) = objFolder.GetDetailsOf(objFolderItem, 14)
End If
Next
Call RecursiveFolder(SubFld)
Next
End Sub
 
C

Crouchie1998

You should be able to convert this yourself via the following documents:

http://support.microsoft.com/default.aspx?scid=kb;en-us;301982

http://support.microsoft.com/default.aspx?scid=kb;en-us;302094

http://support.microsoft.com/default.aspx?scid=kb;en-us;306666

http://www.planet-source-code.com/vb/scripts/ShowCode.asp?txtCodeId=233&lngWId=10

Use the above links as info on how to convert your code. You will also need
the System.IO.FileInfo class for Name/FullName/Created/Size/Modified etc.
Example:

Dim strFile As String = "C:\AutoExec.bat"
Dim fi As New System.IO.FileInfo(strFile)
MessageBox.Show(fi.Name)
MessageBox.Show(fi.FullName)
MessageBox.Show(fi.LastModified)
....

I hope this helps

Crouchie1998
BA (HONS) MCP MCSE
 
C

Cor Ligthert

Martin,

The code is not that much different when you start.

However don't try to convert it.

Use new classes when they are there and use them in a way as they should.

Therefore not
Set oFolder = FSO.GetFolder(SubFld)
however something as
Dim myFileInfo as new FileInfo
(You do not need it probably in your program. The File class is
shared(static) so you can use that direct, on this page are links too a lot
more IO handling)

http://msdn.microsoft.com/library/d...us/cpref/html/frlrfsystemiofileclasstopic.asp

You don't need to use the instruction Call

Don't use the On Error; the Try and Catch block is very easy to use by
instance:
Try
Do what you have to do
Catch ex as exception 'the error is set in this ex object
Do your error handling
Finally
This is forever done as long that you don't power down
End Try
(You can nest it as well and than it becomes even more powerful)

Don't use the variable therefore no
Dim X() however at least
Dim X() as object ' but try to avoid declaring things as objects create
them as they are

Try to avoid the Redim, when you need that use than a more dynamic class as
the arraylist.
http://msdn.microsoft.com/library/d...frlrfsystemcollectionsarraylistclasstopic.asp

And as least don't forget to set in your options always "Option Strict On"
or set it in top of every program file that you use.

I hope this helps,

Cor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top