Plz help me in converting vb code to vb.net

Function Main()
                Dim sMsg as string

                sMsg = MoveFile(DTSGlobalVariables("LoadFileName").Value)
                if (sMsg) = "" then
                                Main = DTSTaskExecResult_Success
                                Main = DTSTaskExecResult_Failure
                end if
End Function
Function MoveFile(ByVal fileName)
                on error resume next
                Dim objFSO as string
		Dim  objFolderDestination as string
		Dim objFile as string
		Dim  newFolderName as string
                Set objFSO = CreateObject("Scripting.FileSystemObject")
                Set objFolderDestination = objFSO.GetFolder(DTSGlobalVariables("DestinationFolder").Value)
                newFolderName = DatePart("m",now) & "-" & Day(now) & "-" & DatePart("yyyy",now) & " " & DatePart("h",now) & "-" & DatePart("n",now) & "-" & DatePart("s",now)
                'Check for existance of the fileName passed in
                If objFSO.FileExists(fileName) Then
                                set objFile = objFSO.GetFile(fileName)

                                'Move the file to the correct directory
                                If objFSO.FolderExists(objFolderDestination & "\" & newFolderName) Then
                                                objFile.Move(objFolderDestination & "\" & newFolderName & "\")
                                                if Err.Number <> 0 Then
                                                                MoveFile = vbCrLf & "could not move file because" & Err.Description & vbCrLf
                                                end if
                                                objFSO.CreateFolder(objFolderDestination & "\" & newFolderName)
                                                if Err.Number <> 0 Then
                                                                MoveFile = vbCrLf & "could not create folder because" & Err.Description & vbCrLf
                                                end if
                                                objFile.Move(objFolderDestination & "\" & newFolderName & "\" & newFileName)
                                                if Err.Number <> 0 Then
                                                                MoveFile = vbCrLf & "could not move file because" & Err.Description & vbCrLf
                                                end if
                                end if
                End If                       

                If Err.Number <> 0 Then
                                MoveFile = vbCrLf & "File " & fileName & " could not be moved, due to the following error message. " & Err.Description & vbCrLf
                                MoveFile = ""
                End If                       
End Function

Edited by Ezzaral: Added code tags. Please use them to format any code that you post.

5 Years
Discussion Span
Last Post by hkdani

This code has problems. It should be simplified: creating objects in arcane and roundabout ways, too many lines of code to do a simple operation, etc.

MoveFile is also a Win32 API kernel32 function. It returns a boolean value . Looks like whoever wrote this VB MoveFile function is trying to parrot the MoveFile API.

If you want to use the MoveFile API, then declare it properly first. Private Declare Function MoveFile lib "kernel32" Alias "MoveFileA" (ByVal lpExistingFileName as String, ByVal lpNewFileName as String) as long All it does is rename the file or folder name. It returns 0, if it fails. Otherwise, it returns a nonzero value if successful.

It's pretty sad when you can write 6 or 7 lines of code to do the same thing using Win32 APIs to replace about 40 lines of VB6 code. This is a prime example of violating the programmer's paradigm of KISS: Keep It Simple Stupid. Visual Basic was designed to make things simple, not complicated.

And you're requesting .NET information on a VB6 forum. You are in the wrong place.
Whereever you got this code, it is more complicated than it should be. If you wrote this code, you obviously don't need any help. Or you're just bored and looking for a good laugh.


hkdani: Actually i dont know vb as well as vb.net but i got this work. And m requesting on vb6 forum bz to change the code first u shud know vb6 rght then only u can convert it.


Ok, this is the .NET version. In the future post .NET questions on the .NET forum.

To move a directory and rename it use the MoveDirectory method: specify the source and target directories.

In general, just post what you wish to accomplish and ask for instructions on the appropriate topic in the appropriate forum.

My.Computer.FileSystem.MoveDirectory("C:\MyDir1", _
    "C:\MyDir2", True)

Edited by hkdani: n/a

This article has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.