Learning VB .net by my own and post what i tried and got success with VB .net.

Saturday, May 2, 2015

Finalize the renamed files application

Finally, I can build an application that have ability to rename a batch of files within a folders. This application will add a specific text at the begining of all file name, no matter what are the orginal file name are? For example, i like to add YYMMDD format to my file so i can manage all of that file according to their created date. This application will check if some files might already have an intend to be added text, in case it found it will not add again.

Let say i want to add 150501 to the beginning of all file, if few files already have it in the beginning of file name (probably i already added it) the application will not add it again.

I started with a form with one text box and two command buttons as you can see in picture below:

MF_001

Textbox for storing the path of folder that need to update name of files, that textbox will remember/save the last path of folder that we workon. The small button is for browse for folder that store files to be update name, while button 2 (Rename) has a function to check and rename all file within the selected folder.

Behind button 1 (small) it contain following code:

   Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click

Using OpenDailog As New FolderBrowserDialog
With OpenDailog
If OpenDailog.ShowDialog() = Windows.Forms.DialogResult.OK Then
Me.TextBox1.Text = .SelectedPath
My.Settings.CodeSerial = TextBox1.Text
End If
End With

End Using
End Sub

And button 2 (rename) contain following code:

   
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim newDir As String = Me.TextsBox1.Text
My.Computer.FileSystem.CreateDirectory(newDir)

Dim i = 0, j = 0
Dim xNum As String = InputBox("Please input date range YYMMDD", "Rename file")
If xNum = "" Then
MsgBox("Date range is should not blank, program exit now so please start it again.")
Exit Sub
End If
For Each filename As String In My.Computer.FileSystem.GetFiles(newDir, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly)
Dim XFName As String = My.Computer.FileSystem.GetName(filename)

If XFName.Substring(0, 6) = xNum Then
j = j + 1
Else
My.Computer.FileSystem.RenameFile(filename, xNum & "" & My.Computer.FileSystem.GetName(filename))
i = i + 1
End If

Next
MsgBox(i & " files were updated their names" & Chr(13) & j & " files were skipped.")
End Sub

Mostly coding in Button 1 are already explained in previous posts, except only Me.Setting.CodeSerial that is a code to save path of folder that we work on, this will be explained more at the end of this post.


Behind button 2 there are a lot of code were written as it is more busy that the first button.

Dim xNum As String = InputBox("Please input date range YYMMDD", "Rename file")

This code is for calling a input message box that we need to input a string that we want to add into the file name. In case you don't input anything below code will handle the task:

        If xNum = "" Then
MsgBox("Date range is should not blank, program exit now so please start it again.")
Exit Sub
End If

Mean that application will work nothing with your comment and you have to click on Button two again to restart all actions again. As well, you might noted i have use i and j as another parameter because i want to count how many files were updated and how many were skipped (due to text i want to update already have in that file name).


I figure our Left or Right function that i quit offen use it in VBA or VB6, but we can't use left and right due to it were assigned to other and work different way. We should use string.substring(0,6) instead of left and right. For example, in above code XFName.Substring(0, 6). XFName is each file name (application will read one by one), while substring(0,6) says that it will cut file name (XFName) from 0 character to 6 character, result will return the first six character from filename. If we want to have 7th character up to end of file name we just say substring(7) than the program will work for you. Then the next code is where application handle the renaming the file:

< xNum My.Computer.FileSystem.GetName(filename)) & ?? My.Computer.FileSystem.RenameFile(filename,>

Lastly I would like to show you how to let application remember the path of folder we workon? This need to create a setting for the application by to Project (menu) then select project property then you will see below setting:


MF_001_02


Make sure you create a name of code (in my example i use CodeSerial and type i choose string, while scope is users. I chosed users because i can't make it work with application (here only two options). That it as you can see in button 1 code: My.Settings.CodeSerial = TextBox1.Text mean that we give a value to the setting. And at the form load even we have to place this code: Me.TextBox1.Text = My.Settings.CodeSerial so value we save at setting will be draw to textbox.

VB .net to read all files exist in folder

In my previous post you already see how do i write code to browse and select folder. Today i will show i explored the way to read all file name in a folder, which i select only a top folder. It means that it will read all files in my selected folder but application will not read any files which in its subfolders.
Generally code to browse files and folder are the same so i will not show you how to browse files but just show you what code i use to read file name which that folder.
 Using OpenDailog As New FolderBrowserDialog
            With OpenDailog

                If OpenDailog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    Dim newDir As String = .SelectedPath
                    My.Computer.FileSystem.CreateDirectory(newDir)
                    For Each filename As String In My.Computer.FileSystem.GetFiles(newDir, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly)
                        MsgBox(My.Computer.FileSystem.GetName(filename))
                    Next
                End If
            End With

        End Using

Through searching through VB help viewer as well as some forum i look, i understand that in VB .net i can have it to read all file information through following code:
For Each filename As String In My.Computer.FileSystem.GetFiles(newDir, Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly)

Next

Through that code system will read all file name "For each filename as string" within folder "In My.Computer.FileSystem.Getfiles(newDei,Microsoft.VisualBasic.FileIO.SearchOption.
SearchTopLevelOnly". There are a few notes here:

  • We can declare object within the code as you can see "For Each filename As string In..."
  • For need to have next otherwise it will result in error.
  • "Microsoft.VisualBasic.FileIO.SearchOption.SearchTopLevelOnly" is a search option that we provide, in case you want application to search subfolder you can change it to "SearchAllSubDirectories".

    With above code application will read all of your files in that selected folder, but you need to know what do the application found? so i apply following code:
    MsgBox(My.Computer.FileSystem.GetName(filename))

    Actually you can use "filename" as we already declare and provide it the value, however in this case application will show you path and filename, but in above code my intention is to show only file name, that is why i add "My.computer.filesystem.getname" more.


    In some case you may want to select some specific file from file browser, in this case you can change code to following:
     Using OpenDialog As New OpenFileDialog
                With OpenDialog
    
                    .Filter = "MS Access Files (*.mdb)|*.mdb"
                    .Title = "Select the Destenation DB to Execute the Queries"
                    .Multiselect = False
                    If OpenDialog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        msgobox (.FileName)
                    End If
                End With
            End Using


    Where "Filter" has a role to limit what type of file do you want to see, in case you want to see all type of file you can change above code from "|*.mdb" to "|*.*". And if you want to have a possibility to select many files you can change "Multiselect=False" to "Multiselect=True"
  • Friday, May 1, 2015

    VB .net to browse folders and files

    Last night I started to learn to use VB .Net, with a purpose to create an application that will rename files in a folder. That application will rename only files in the folder but not in sub folders.

    My first impression regarding its language, it might have around 30% language (code) that similar to VB6/VBA so i might need to pay more energy and time to learn more before I can build any application, even it is a very small application.

    However VB .net has some automate function/declaration, for example when i create a form it is automatic declare the form that i can call it to use later. I am sorry, it might normal for you but it seem strange for me and i feel some comfort about it.

    Now let go back to my application that i am learning to build (application that rename files in folder), i start VB .net by choosing VB6 as i familiar with it before, and i choose a project to create a desktop application.

    It is automatic create a form for me then i created a button that i expect to click to browse for folders and file. At my first try i want to explore the and select the folder. So i used following code:

        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Using OpenDailog As New FolderBrowserDialog
                With OpenDailog
    
                    If OpenDailog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                        MsgBox(.SelectedPath & " were selected")
                    End If
                End With
    
            End Using
        End Sub 

    When i right click on button that i just created and choose view code it automatic create a sub button click as you can see in above code. Then below code is function to show folders/files browser that allow me to select files or folders.
            Using OpenDailog As New FolderBrowserDialog
                With OpenDailog
                    If OpenDailog.ShowDialog() = Windows.Forms.DialogResult.OK Then
                    End If
                End With
            End Using
    

    Then because i just want the system to show me which folder i select i use below code:
    MsgBox(.SelectedPath & " were selected")
    
    Mean that when i select a folder and i click "OK" then the system will show a message of folder and it path i selected. At last i can have a desktop application that will work to show me a selected folders. My next post will show a code that will have the application to read all files that exist in the top folder. It seem a long path for me to go....

    Thursday, April 30, 2015

    My first post to this blog!!

    Hi all friends around the cloud!

    I am the one who have some knowledge and experience with VBA code of Ms Excel and Ms Access and I used to build only some small applications using these two office programs, I really enjoy and have a lot of passion to learn and share all of those things.

    I used to be part of some Excel forum around the internet as well but due to time constraint i decided to leave those forum, however i admit that through those forums i could learn from, share to, and enjoy very much.

    In around 3 to 5 years ago I started to develop few application using VB6 as its language quit similar to VBA that i know in Ms Excel and Ms Access, however i stopped it because of time constraint as well. It seem that i can't manage my time to enjoy my passion.

    Just these few weeks, i decided to start programing again and i want to start with VB6, but after performing a search in internet for a while i found that Microsoft abandon it and replace by VB .net that also include VB6. So now i start to learn and hope to build some applications from VB .net 2012 version very soon.

    At the same time i also decide to post what i learn and build into this blog and hope friends here will help me some idea, method, etc. so that will help me to quickly learn VB .net.

    Hope you who come across this blog will not hesitate to share me some idea, and i expect to ask many questions as i quit new to VB .net.

    Thanks.

    Recent Posts

    Popular Posts

    Powered by Blogger.