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.

0 comments:

Post a Comment

Recent Posts

Popular Posts

Powered by Blogger.