none
Bulk change file names RRS feed

  • Question

  • to whom:

    I have hundreds of .pdfs that I downloaded that have names that are not helpful at first glance.  Part of the download was a 'key' that lets me know which file belongs to which person (I have this in excel).  I'd like to know if there is a vb script out there (or one I could get help writing) that would look in a folder and rename the files based on the accompanying spreadsheet.

    Is this possible?

    Any thoughts or suggestions would be of great help.

    doug

    Wednesday, January 25, 2012 6:44 PM

Answers

  • I'm not sure what you mean by bulk - it implies all done in one action, but I imagine this could be done in this logical order using VB.NET (NOTE:  this is not a vb script forum):

    1. Create a dictionary based on the rename values in the spreadsheet
    2. Get the list of file names in the directory
    3. Loop through those names, for each iterated file name...
    4.     Find the replacement file name based on the dictionary of person's name/key in the pdf file name
    5.     Perform a rename on the file based on the new file name value

    You should familiarize yourself with the classes and methods inside the System.IO namespace, particularly the Directory.GetFiles method (http://msdn.microsoft.com/en-us/library/07wt70x2.aspx) and the File.Movemethod (http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx) to effectively rename the files.

    This link provides a short overview of common operations:

    http://www.adventuresindevelopment.com/2009/05/18/using-systemio-to-create-read-rename-and-delete-files-and-folders-in-c-and-visual-basic/

    Wednesday, January 25, 2012 7:01 PM
  • Doug,

    You have in my perception got some very good idea, but for me it is unclear what part you miss.

    However, I made for myself a simple tool to rename files because I'don't like the way in Windows Explorer (Maybe I do that always wrong).

    I think this piece of code can help you. I use a wpf listview like is done with Windows Explorer.

     

           Dim thePath As String = ""
            If txtPaternSearch.Text <> thePath AndAlso txtNewPatern.Text <> thePath Then
                For Each lsv As ListViewItem In lstFiles.SelectedItems
                    Dim fullName = DirectCast(lsv.Content, FileSystemInfo).FullName
                    thePath = Path.GetDirectoryName(fullName)
                    Dim oldName = Path.GetFileName(fullName)
                    Dim newName = oldName.Replace(txtPaternSearch.Text, txtNewPatern.Text)
                    Dim newFullPath = IO.Path.Combine(thePath, newName)
                    If Not File.Exists(newFullPath) Then
                        File.Move(fullName, newFullPath)
                    End If
                Next
            End If
    

     

    Be aware if you have already the name the replace is not needed and you can simply set it.

     


    Success
    Cor
    Wednesday, January 25, 2012 8:00 PM

All replies

  • I'm not sure what you mean by bulk - it implies all done in one action, but I imagine this could be done in this logical order using VB.NET (NOTE:  this is not a vb script forum):

    1. Create a dictionary based on the rename values in the spreadsheet
    2. Get the list of file names in the directory
    3. Loop through those names, for each iterated file name...
    4.     Find the replacement file name based on the dictionary of person's name/key in the pdf file name
    5.     Perform a rename on the file based on the new file name value

    You should familiarize yourself with the classes and methods inside the System.IO namespace, particularly the Directory.GetFiles method (http://msdn.microsoft.com/en-us/library/07wt70x2.aspx) and the File.Movemethod (http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx) to effectively rename the files.

    This link provides a short overview of common operations:

    http://www.adventuresindevelopment.com/2009/05/18/using-systemio-to-create-read-rename-and-delete-files-and-folders-in-c-and-visual-basic/

    Wednesday, January 25, 2012 7:01 PM
  • Thanks much.  I appreciate the help.
    Wednesday, January 25, 2012 7:02 PM
  • No problem.  Let us know if you have any specific questions regarding any of these steps.
    Wednesday, January 25, 2012 7:07 PM
  • As this is a forum for VB.NET, I'll make a suggestion based on that.

    If this is a one-off task, I'd save the Excel file in csv format and read it line-by-line with a StreamReader, use String.Split on each line, create folders based on the people's names, and copy the files with the new names to the appropriate folders.

    If it's going to be a frequent task, I'd look into using the Excel file with a database connection (see the Data Files section at www.connectionstrings.com for connection strings) to skip the saving as a csv file and continue as above with the creating folders.

    HTH,

    Andrew

    Wednesday, January 25, 2012 7:08 PM
  • Doug,

    You have in my perception got some very good idea, but for me it is unclear what part you miss.

    However, I made for myself a simple tool to rename files because I'don't like the way in Windows Explorer (Maybe I do that always wrong).

    I think this piece of code can help you. I use a wpf listview like is done with Windows Explorer.

     

           Dim thePath As String = ""
            If txtPaternSearch.Text <> thePath AndAlso txtNewPatern.Text <> thePath Then
                For Each lsv As ListViewItem In lstFiles.SelectedItems
                    Dim fullName = DirectCast(lsv.Content, FileSystemInfo).FullName
                    thePath = Path.GetDirectoryName(fullName)
                    Dim oldName = Path.GetFileName(fullName)
                    Dim newName = oldName.Replace(txtPaternSearch.Text, txtNewPatern.Text)
                    Dim newFullPath = IO.Path.Combine(thePath, newName)
                    If Not File.Exists(newFullPath) Then
                        File.Move(fullName, newFullPath)
                    End If
                Next
            End If
    

     

    Be aware if you have already the name the replace is not needed and you can simply set it.

     


    Success
    Cor
    Wednesday, January 25, 2012 8:00 PM