locked
Program theory - Idea's? RRS feed

  • Question

  • Hi all! I'm trying to come up with a program to help out one of my family members; basically all it will do is rename files (Or just add numbers to the beginning).

     

    Example: If you have a story on your computer and you want it to burn it to an Mp3 CD and go (of course) in order. "Track 1", "Track 2" doesn't always work with some MP3 players it has to be 001_Track1, 002_Track2. So if there are 75-100 tracks then your have to manually rename them all and it's obviously very time consuming.

     

    I'm aware there are programs out there that will already do this but I'd like to learn some more about VB. I don't really have any idea how I would go about this so as of right now I'm just looking for concept Idea's (what dialogs should I use ect..)

     

    I am using Visual Basic Express Edition 2008. My experience is limited.

     

    Thanks for your time!

    Tuesday, December 9, 2008 6:48 PM

Answers

  • BowserYo said:

    Dim StartWith As Integer = Integer.Parse(Rename.Text) ' with no errors :)



    Hi Again .

    Isn't ( Rename ) a button?

    I have used it ( In my example ) as a TextBox .

    Ok Listen >> Controls { Rename( Button ), NUDRename( NumericUpDown ), FilesList( ListView ) }


    Public Class Form1

    Private Files() As String = IO.Directory.GetFiles("SD", "*.mp3", IO.SearchOption.TopDirectoryOnly)

    Private Sub Form1_Load( Byval Sender As System.Object, Byval E As System.EventArgs) Handles MyBase.Load
    For Each FileName As String In Files
    Dim LVI As New ListViewItem( IO.Path.GetFileNameWithoutExtension( FileName ) )
    LVI.Tag = FileName
    Next FileName
    End Sub

    Private Sub Rename_Click( Byval Sender As System.Object, Byval E As System.EventArgs) Handles Rename.Click
    Dim StartWith As Integer = Integer.Parse( NUDRename.Value ) ' with no errors :)
    For Each LVI As ListViewItem In FilesList.Items
    My.Computer.FileSystem.Rename( LVI.Tag.ToString() _
    , String.Format( "{0}_Track {1}.mp3", StartWith.ToString("000"), StartWith ) )
    StartWith += 1
    Next FileName
    End Sub

    End Class

    BVS, Cos(), Sin() Can Make The Miracles
    • Edited by BVS89 Sunday, December 14, 2008 2:22 PM IO.Directory.GetFiles()
    • Marked as answer by Riquel_DongModerator Wednesday, December 17, 2008 9:52 AM
    Friday, December 12, 2008 12:19 AM

All replies

  • Hi BowserYo .

     

    You can do that with something like this :

     

    Code Snippet

     

    Dim BFD As New FolderBrowserDialog

    If BFD.ShowDialog = 1 Then

    For Each FileName As String In _

    My.Computer.FileSystem.GetFiles(BFD.SelectedPath, 2, New String() {"*.mp3"})

    Dim ORName As String = IO.Path.GetFileNameWithoutExtension(FileName)

    Dim Number As Integer = Integer.Parse(Replace(ORName, "Track ", Nothing))

    My.Computer.FileSystem.RenameFile(FileName, Number.ToString("000") & "_Track" & Number.ToString() & ".mp3")

    Next

    End If

     

     

    Best Regards,

     

    BVS.

    Tuesday, December 9, 2008 7:12 PM
  • Edit: Never mind got I understand now

     

    Thanks! I'll keep working with this!

     

    EDIT2:

     

    Okay it's giving me this error when I tested it

     

    "Input string was not in a correct format."

     

    And its having trouble with this line:

     

    Number As Integer = Integer.Parse(Replace(ORName, "Track ", Nothing))

     

    Thanks!

    Tuesday, December 9, 2008 7:17 PM
  •  BowserYo wrote:

    Edit: Never mind got I understand now

     

    Thanks! I'll keep working with this!

     

    EDIT2:

     

    Okay it's giving me this error when I tested it

     

    "Input string was not in a correct format."

     

    And its having trouble with this line:

     

    Number As Integer = Integer.Parse(Replace(ORName, "Track ", Nothing))

     

    Thanks!

     

    Hi Again .

     

    Are you sure that every .mp3 file name starts with ( "Track " )<= ( has space ) and ends with a number > :

     

    Track 1

    Track 2

    Track 3

    Track 4

    Track 5

     

    Nor

     

    Track

    Track1

    Track2

    Track3

    Track4

    Track5

     

    ?

     

    something else :

     

    the Upper Case and the LowerCase ?

    Tuesday, December 9, 2008 7:30 PM
  • Okay, At first they didn't but I changed them and still get the same error.

     

    This is what I'm envisioning:

     

    http://img241.imageshack.us/img241/271/filerenameed2.jpg

     

    Panel 1 is the Folder explorer, or like a "Windows explorer". Panel 2 shows all the files within the selected folder.

     

    The text box labeled "Starts with" you would type "001" and it would go from there. Not a CLUE how I would code that though Sad

     

    Thanks!

     

    Tuesday, December 9, 2008 7:38 PM
  •  

    OK, if you've already something to browse the directories and get their files :

     

    4 Example ( FilesList ) As ListBox

    Code Snippet

     

    Private Sub BtnRename_Click( Byval Sender As System.Object _

    , Byval E As System.EventArgs ) Handles BtnRename,Click

    Dim Index As Integer = Integer.Parse( StartWith.Text )

    For Each FileName As String In FilesList.Items

    My.Computer.FileSystem.RenameFile(FileName _

    , Index .ToString("000") & "_Track" & Index.ToString() & ".mp3")

    Index += 1

    Next

    End Sub

     

     

     

    If this won't work, please write the 1st file name and the last one . << You don't have to

     

    Best Regards,

     

    BVS.

    Tuesday, December 9, 2008 7:52 PM
  • Okay, It is having a problem with this line

     

    Byval E As System.EventArgs ) Handles BtnRename,Click

    "BtnRename,Click"

     

    It says " '.' expected."

     

    I'm pretty much a newbie so please explain Big Smile .

     

    Thanks!

    Tuesday, December 9, 2008 8:11 PM
  •  BowserYo wrote:

    Okay, It is having a problem with this line

     

    Byval E As System.EventArgs ) Handles BtnRename,Click

    "BtnRename,Click"

     

    It says " '.' expected."

     

    I'm pretty much a newbie so please explain Big Smile .

     

    Thanks!

     

    I'm sorry <> I Started getting sleepy  .

     

    replace that comma with dot >>

     

    Private Sub BtnRename_Click( Byval Sender As System.Object, Byval E As System.EventArgs ) Handles BtnRename.Click

     

    Note : Replace ( BtnRename ) With ( Your Button's Name ) .

     

    Best Regards,

     

    BVS.

    Tuesday, December 9, 2008 8:16 PM
  • Okay cool got that down. Now I need to find a way to get a explorer type window in panel 1 and a listbox(?) in panel 2 to list all the files in the folder. Any idea's?

     

    Tuesday, December 9, 2008 8:22 PM
  • Explorer-Style Application Sample .

     

    I'll see you tomorrow Smile >> .

     

     

    Best Wishes,

     

    BVS.

    Tuesday, December 9, 2008 8:35 PM
  • Okay thanks!

    Tuesday, December 9, 2008 8:38 PM
  • Okay that example is going Waayy over my head! I think the WebBrowser may do the trick but is there anyway to set it to display in details instead of thumbnails?

    Wednesday, December 10, 2008 8:07 PM
  • Hi BowserYo,

    I have used sendmessage to modify the view option in webbrowser.

    Imports System.Runtime.InteropServices
    Imports System.Text
    Partial Public Class Form1
        Inherits Form
        ' All Folder View styles
        Private Const LV_VIEW_ICON As Integer = &H0
        Private Const LV_VIEW_DETAILS As Integer = &H1
        Private Const LV_VIEW_SMALLICON As Integer = &H2
        Private Const LV_VIEW_LIST As Integer = &H3
        Private Const LV_VIEW_TILE As Integer = &H4
        Private Const EM_HIDEBALLOONTIP As Integer = &H1504
        Private Const LVM_SETVIEW As Integer = &H108E
        Private Const ListViewClassName As String = "SysListView32"
        Private Shared ReadOnly NullHandleRef As HandleRef = New HandleRef(Nothing, IntPtr.Zero)
        <DllImport("user32.dll", ExactSpelling:=True)> _
        Private Shared Function EnumChildWindows(ByVal hwndParent As HandleRef, ByVal lpEnumFunc As EnumChildrenCallback, ByVal lParam As HandleRef) As Boolean
        End Function

        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        Private Shared Function SendMessage(ByVal hWnd As HandleRef, ByVal Msg As UInteger, ByVal wParam As Integer, ByVal lParam As Integer) As Integer
        End Function

        <DllImport("user32.dll", CharSet:=CharSet.Auto)> _
        Private Shared Function RealGetWindowClass(ByVal hwnd As IntPtr, <Out()> ByVal pszType As StringBuilder, ByVal cchType As UInteger) As UInteger
        End Function
        Private Delegate Function EnumChildrenCallback(ByVal hwnd As IntPtr, ByVal lParam As IntPtr) As Boolean
        Private listViewHandle As HandleRef
        Private Sub FindListViewHandle()
            Me.listViewHandle = NullHandleRef
            Dim lpEnumFunc As EnumChildrenCallback = New EnumChildrenCallback(AddressOf EnumChildren)
            EnumChildWindows(New HandleRef(Me.WebBrowser1, Me.WebBrowser1.Handle), lpEnumFunc, NullHandleRef)
        End Sub
        Private Function EnumChildren(ByVal hwnd As IntPtr, ByVal lparam As IntPtr) As Boolean
            Dim sb As StringBuilder = New StringBuilder(100)
            RealGetWindowClass(hwnd, sb, 100)
            If sb.ToString() = ListViewClassName Then
                Me.listViewHandle = New HandleRef(Nothing, hwnd)
            End If
            Return True
        End Function
        Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            WebBrowser1.Navigate("D:\") ' WebBrowser1 is used as File Explorer.
        End Sub
        ' Change Folder View style
        Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            FindListViewHandle()
            Dim view As Integer = LV_VIEW_LIST
            SendMessage(Me.listViewHandle, LVM_SETVIEW, view, 0)
        End Sub
        ' Change Folder View style
        Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs)
            FindListViewHandle()
            Dim view As Integer = LV_VIEW_ICON
            SendMessage(Me.listViewHandle, LVM_SETVIEW, view, 0)
        End Sub
    End Class

    Best regards,
    Riquel

    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, December 11, 2008 5:53 AM
    Moderator
  • Okay I'm starting to see how complicated this can be. The webbrowser is not working unfortunately so I'm going to go with something a little more simple.

    I figured out a way to display files in a dir, into a list box. Is there anyway to do something like this, (Im making up code)

    Private Sub BtnRename_click
    Get.filename in FilesList.Items
    Items.Rename = "001_" + (FileName)

    And then some how for every aditional item add +1, so (001_filename,002_filename ect..)

    Also is there anyway to change the location of the listbox? I can do this:

    Dim folderInfo As New IO.DirectoryInfo(nav.Text)

    But it only does that at the start of the program, even if I add that code to a private sub of a button. When I run BasharVS's code I get this error with this line:

    "Input string was not in a correct format."

    Index As Integer = Integer.Parse(Rename.Text)

    Thanks!

    Thursday, December 11, 2008 7:15 PM
  • Hi BrowseYo .

    You know what I think ?

    I think you're trying to parse ( _001 ) or ( 001_ ) as integer <> right ?

    Rename.Text = 2 < Or > 002

    Dim StartWith As Integer = Integer.Parse( Rename.Text ) ' with no errors :)
    For Each FileName As String In FileList.Items
    My.Computer.FileSystem.Rename( FileName.ToString() _
    , String.Format( "{0}_Track {1}.mp3", StartWith.ToString("000"), StartWith ) )
    StartWith += 1
    Next FileName


    Best Regards,

    BVS.

    BVS, Cos(), Sin() Can Make The Miracles
    • Edited by BVS89 Friday, December 12, 2008 12:22 AM StartWith InForeach .
    Thursday, December 11, 2008 10:24 PM
  • Okay I must be missing something because I'm still having problems :( . (Note: I had to change your code a little, I underlined what I changed and put numbers beside them.)
    --------------

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

    1. For Each FileName As String In FilesList.Items

    Dim StartWith As Integer = Integer.Parse(Rename.Text) ' with no errors :)

    2. My.Computer.FileSystem.RenameFile(FileName.ToString() _

    , String.Format("{0}_Track {1}.mp3", StartWith.ToString("000"), StartWith))

    StartWith += 1

    Next FileName

    --------------

    Here are the file names I have ("Track 1.mp3, Track 2.mp3, Track 3.mp3")

    I'm getting the same error, same line. Sorry It's probably just me :'( .

    Thanks!

    P.S here is my entire code just incase it's something above:
    --------------

    Public Class Form1

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim folderInfo As New IO.DirectoryInfo("C:\Documents and Settings\Computer\Desktop\Other\New Folder")

    Dim arrFilesInFolder() As IO.FileInfo

    Dim fileInFolder As IO.FileInfo

    arrFilesInFolder = folderInfo.GetFiles("*.*")

    For Each fileInFolder In arrFilesInFolder

    FilesList.Items.Add(fileInFolder.Name)

    Next

    End Sub

    Private Sub Rename_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Rename.Click

    For Each FileName As String In FilesList.Items

    Dim StartWith As Integer = Integer.Parse(Rename.Text) ' with no errors :)

    My.Computer.FileSystem.RenameFile(FileName.ToString() _

    , String.Format("{0}_Track {1}.mp3", StartWith.ToString("000"), StartWith))

    StartWith += 1

    Next FileName

    End Sub

    Private Sub ExitToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ExitToolStripMenuItem.Click

    Me.Close()

    End Sub

    End Class

    Thursday, December 11, 2008 11:49 PM
  • BowserYo said:

    Dim StartWith As Integer = Integer.Parse(Rename.Text) ' with no errors :)



    Hi Again .

    Isn't ( Rename ) a button?

    I have used it ( In my example ) as a TextBox .

    Ok Listen >> Controls { Rename( Button ), NUDRename( NumericUpDown ), FilesList( ListView ) }


    Public Class Form1

    Private Files() As String = IO.Directory.GetFiles("SD", "*.mp3", IO.SearchOption.TopDirectoryOnly)

    Private Sub Form1_Load( Byval Sender As System.Object, Byval E As System.EventArgs) Handles MyBase.Load
    For Each FileName As String In Files
    Dim LVI As New ListViewItem( IO.Path.GetFileNameWithoutExtension( FileName ) )
    LVI.Tag = FileName
    Next FileName
    End Sub

    Private Sub Rename_Click( Byval Sender As System.Object, Byval E As System.EventArgs) Handles Rename.Click
    Dim StartWith As Integer = Integer.Parse( NUDRename.Value ) ' with no errors :)
    For Each LVI As ListViewItem In FilesList.Items
    My.Computer.FileSystem.Rename( LVI.Tag.ToString() _
    , String.Format( "{0}_Track {1}.mp3", StartWith.ToString("000"), StartWith ) )
    StartWith += 1
    Next FileName
    End Sub

    End Class

    BVS, Cos(), Sin() Can Make The Miracles
    • Edited by BVS89 Sunday, December 14, 2008 2:22 PM IO.Directory.GetFiles()
    • Marked as answer by Riquel_DongModerator Wednesday, December 17, 2008 9:52 AM
    Friday, December 12, 2008 12:19 AM
  • I'm getting these errors:

    My.Computer.FileSystem.GetFiles(DesktopPath & "\New Folder", 2, New String() {"*.mp3"})

    Error 1 Value of type 'System.Collections.ObjectModel.ReadOnlyCollection(Of String)' cannot be converted to '1-dimensional array of String'. C:\Documents and Settings\Computer\Local Settings\Application Data\Temporary Projects\TestApp\Form1.vb 4 33 TestApp


    My.Computer.FileSystem.Rename

    Error 3 'Rename' is not a member of 'Microsoft.VisualBasic.MyServices.FileSystemProxy'. C:\Documents and Settings\Computer\Local Settings\Application Data\Temporary Projects\TestApp\Form1.vb 16 13 TestApp

    Thanks for your patience!

    Friday, December 12, 2008 8:58 PM
  • Hi BrowserYo.

    Please give it a try ^^^ Modified .

    Best Regards,

    BVS.

    BVS, Cos(), Sin() Can Make The Miracles
    Sunday, December 14, 2008 2:24 PM