Remove random strings from file name that begin with @ and end at .ext

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Janetlynn
    New Member
    • Dec 2014
    • 3

    Remove random strings from file name that begin with @ and end at .ext

    I need to rename files from filename@f{|56r dfyhf.txt to filename.txt. All file names contain a random string that begin with @ with random string up to .ext
    Thank for any help.
    Janet
  • twinnyfo
    Recognized Expert Moderator Specialist
    • Nov 2011
    • 3662

    #2
    Janet,

    If I understand you correctly, here is the situation you have:

    Code:
    [B][U]Original Filename[/U][/B]            [B][U]Renamed to
    [/U][/B]filename@f{|56rdfyhf.txt     filename.txt
    AnotherFile.doc              [Unchanged]
    WorkFile@abc123.xlsx         WorkFile.xlsx
    I just want to make sure I understand before we try to begin.

    Some additional questions: What happens if the newly renamed file already exists? Do you over-write or add a sequential number?

    The basics of your solution will be to cycle through all your files, determine whether they need to be renamed, if they do, change the name, if not, leave it alone.

    Because of some of the additional questions I have asked, this may end up being a little involved than what I initially imagined, but I am sure that we can point you in the right direction of a solution.

    Comment

    • Janetlynn
      New Member
      • Dec 2014
      • 3

      #3
      No duplicate files will be created

      Comment

      • Janetlynn
        New Member
        • Dec 2014
        • 3

        #4
        You have it right! There won't be any files in the folder that are named correctly. Thanks for your help!
        Janet

        Comment

        • twinnyfo
          Recognized Expert Moderator Specialist
          • Nov 2011
          • 3662

          #5
          Good! That actually makes some of this a bit easier.

          I will try to guide you through a generic process that you can apply to any folder. I will not write all the code, because I want you to try to work through some of this yourself. I will help you through any trouble spots. We prefer to work this way on this forum, because we want our posters to gain a better understanding of the process and the programming language than just "providing the solution". hopefully you will have a solution, but also you will understand it.

          So here is a skeleton of what your Function will look like:

          Code:
          Public Function RenameFiles(strFolder as String)
          On Error GoTo EH
              'Declare the variables you will use
              'You will probably need at least two strings:
              'One for the name of the old File, one for the new
              'You will probably need some sort of Integer pointer...
          
              'The Dir() function will get a list of files, one at a time
              'When you set a variable equal to Dir([FolderName]) you
              'will get the name of the first file in the folder
              'The next time you use Dir()--no arguments--you will get
              'the next file name.  When Dir() = "", you have run out
              'of files.  Use this to your advantage
          
              'So....... Find the first file name here:
          
              [Your Code]
          
              'You will want to loop through records at this point
              'to determine if we have run out of files
          
              'Find out if your File Name has "@" in it
              'An effective Function to use is Instr()
              'Instr([FileName], "@") will return the number of
              'character in the string the search element is found
              'That function returns 0 if it is not found
          
              'So, if you find the "@", then you also have to find
              'The extension (usually 3-4 characters, but may be more)
              'Several ways to do that--you could use Instr() again
              'as long as there are no more "."s in the file name
          
              'So, if you determine that the file name is 
              '"filename@f{|56rdfyhf.txt" and the "@" is character 9
              'and the extension is ".txt" then you must generate
              'the new file name to be:  "filename.txt"
          
              'Thus, Rename the file (since it is not being "opened"
              'This should be relatively easy:
          
              Name [OldFileName] As [NewFileName]
          
              'Now, you need to find the next file in the folder
              'Here is where you will loop back through your code
          
          
              Exit Function
          EH:
              MsgBox "There was an error renaming the Files!  Error:  " & _
                  Err.Desription 
              Exit Function
          End Function
          So, is this a solution? Well, not yet. But, I will be glad to continue to work you through understanding this problem. Hopefully, this will not take real long. I am glad to assist every step of the way.

          Comment

          Working...