Rename then move, or move then rename a file using wild cards

  • Is there a way to rename then move, or move then rename a file using wild cards for the file name, or using only part of the file name.


    I know wild cads are risky, but I am certain that only the files in question will be altered.


    Many of these files are media of one kind or another, so the file extensions vary, .wav, .avi, .mp4 and the same for music files. In some cases there is no extension at all, but all of these are .lnk files or .url files, but it is not only the extension that requires the wild card. The file naming system of the past was willy nilly, no apparent rhyme or reason for the current file name. I would like to rename these files using a file naming system that would follow a standard has some significance to the file itself, other than just a name


    I have found several leads on the net, the best given here -
    http://www.ozgrid.com/forum/showthread.php?t=162768&p=746639#post746639


    given by [FONT="Verdana"]jindon,
    [/FONT]

    which uses


    Code
    Sub test() 
        Dim fn As String 
        Const myDir As String = "c:\testA\" '<- alter here
        Const newFolder As String = "c:\testB\" '<- alter here
        fn = Dir(myDir & "red_*.csv") 
        Do While fn <> "" 
            FileCopy myDir & fn, newFolder & Replace(fn, "red", "blue") 
            fn = Dir 
        Loop 
    End Sub


    This at least allows the use of wild cards. However, the code makes a copy first, which takes time for the larger files.


    Any help would be appreciated


    awsmitty

  • Re: Rename then move, or move then rename a file using wild cards


    I assume you mean examples of the file names


    Prudhoe Bay, 1988(a), J. Dalton.avi - date is incomplete


    Prudhoe Bay follow up, J.D.avi - no date - there are several follow ups


    Yates Oil Field, J.D..avi


    West Texas 101.344.wmv - no date, no author


    West Texas 101.345.wmv - no date, no author


    As you see, some have dates, which we need, some person writing the report usedinitials only, this I can correct (new file name) from another workbook of employees, some nothing but an identification number, which again I canretrieve the correct title from another worknbook. In other words, the person doing the archiving not only needs these other workbooks, but one spends sometime reading the archive before realizing that this isn't what was being sought. Where as if the title were a little more informative, one could find precisely the article being sought and omit reading the unnecessary.


    So, let's take the last example. I can cross reference this using another workbook that will provide a more informative title/filename using the 101.345. Hope that helps clarify the problem at hand. Almost all the new titles will in part come from other sources and will be generated via VBA. Also, I was going to use a msgbox showing the current title and the new title so I can verify that the change is correct, if not, hand type one in. I also intend to use a suffix of abbreviations which will be a shorthand summarizing the conclusions of the report. This too will be generated via VBA.


    Hope that helps

  • Re: Rename then move, or move then rename a file using wild cards


    Quote


    Rename then move, or move then rename a file using wild cards


    change

    Code
    FileCopy myDir & fn, newFolder & Replace(fn, "red", "blue")


    to

    Code
    name myDir & fn as newFolder & Replace(fn, "red", "blue")
  • Re: Rename then move, or move then rename a file using wild cards


    Jindon, Excel Samurai (It makes me chuckle, but appropriate)


    Your code works perfectly. Very fast, faster than I can push F8 then go to the destination folder, way faster than making a copy.


    I know a few days ago I tried something like this, but I couldn't get it to work. I have most of my old code now as comments and have checked for typos but still haven't found my error. I have no idea what I was doing wrong.


    Many thanks. By the way, keep the title, Excel Samurai. I think it's great!


    awsmitty

Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!