File renames, en masse

Discussion in 'Scripting' started by Lanwench [MVP - Exchange], Jan 29, 2007.

  1. Hi - a client running a W2k domain w/XP Pro workstations (not that it's
    specifically relevant) has currently got a slew of files in their main
    shared folder tree, which they'd like to rename for organization/sorting
    purposes. There are probably about 3000 files in total, in multiple
    subfolders. They're mostly Office files (Word & Excel).

    Currently, the files are named similarly to the following:

    X-00-dd-mm-yy-keywords-author.doc

    ....where the "X" is some initial for the type of file (e.g. M=memo), "00" is
    the last two digits of the project number, etc.


    To make things easier to sort, they're looking at something like the
    following, instead:

    X-00-yyyy-mm-dd-keywords-author.doc


    .....I'd suggested that even if this is possible, it might be more useful to
    use


    yyyy-mm-dd-X-00-keywords-author.doc

    for sorting purposes. However, as I'm a complete moron when it comes to this
    sort of thing, I thought I'd post in here. I know how to do some extremely
    simple things with batch files, and that's about it. I'd do a complete full
    backup first, of course, and would run this overnight.

    Anyone want to help me look brilliant for this client? I won't steal credit
    for the idea; I told them I had no clue and was going to ask some smart
    people.
     
    Lanwench [MVP - Exchange], Jan 29, 2007
    #1
    1. Advertisements

  2. Lanwench [MVP - Exchange]

    Paul Randall Guest

    "Lanwench [MVP - Exchange]"
    Hi,
    I assume you already know how to recurse through all the subfolders of a
    starting folder. If not, google the VBscripting newsgroups. I also assume
    you know how to access the subfolders and files collections of the file
    system object's folder object. A file object's Name property can be used to
    get the file's current name as well as to give it a new name.

    It looks like your file names have three zones of interest:
    1. A fixed-length zone (X-00-) that does not change
    2. A fixed-length zone that increases in size by two bytes, with subzones
    that are rearranged.
    3. A variable length zone that does not change.

    On thing you didn't specify is what to do when changing the old year from xx
    to the new format of xxxx. Is the new year always 20xx or can it sometimes
    be 19xx?

    The four-line statement below will probably change the name to what you
    want.
    objFile.Name = Left(objFile.Name, 5) & _
    "20" & Mid(objFile.Name, 12, 2) & _
    Mid(objFile.Name, 5, 7) & _
    Right(objFile.Name, Len(objFile.Name) - 16

    -Paul Randall
     
    Paul Randall, Jan 29, 2007
    #2
    1. Advertisements

  3. In
    Hey, Paul - thank you for the reply.
    <whistles casually, looks around room> Alas, no - I'm a complete newb with
    this sort of thing. But I promise I'm not some kid trying to get someone to
    do my homework for me and will happily answer any of your burning Exchange
    questions :)
    Will do so as soon as I can figure out what to use for search parameters...


    Re the yyyy - I think they've got some older files in this tree, but it's
    also possible someone has already moved the pre-Y2k stuff out to another
    volume where they used to do their archiving.

    So, bearing in mind that I'm a complete tyro here, I'd take the above four
    lines and save them in a whatever.vbs file ...yes? And I need to find out
    how to run this against the tree so it will iteratively run through all the
    subfolders. Do you know a decent site I can check out that spells out this
    sort of thing (vbscripting, particularly) in introductory terms, for the lay
    person?

    I'm going to run the questions by my client (w/r/t the fixed-length
    question) and will report back. I'll probably grab a copy of a few
    folders/subfolders to play with, so I can test this without running the risk
    of making a huge mess I don't know how to clean up. The witness protection
    program won't take me on; I already asked.

    Again, your help is very much appreciated.
     
    Lanwench [MVP - Exchange], Jan 30, 2007
    #3
  4. Lanwench [MVP - Exchange]

    urkec Guest

    Before trying to rename files, I think you should check for files that don't
    match your naming scheme, and then decide what to do with those. Try running
    this on your root folder to see what names don't match the pattern:

    A-nn-nn-nn-nn-

    If necessary, you could also check if the day, month and year portions are
    valid.

    Replace "C:\test" with your root folder. File names are stored in
    "C:\badFileNames" and "C:\namesOK"

    This will not try to delete or rename anything, but test everything first.


    set fso = CreateObject _
    ("Scripting.FileSystemObject")
    set startFolder = fso.GetFolder ("C:\test")
    set badFileNames = fso_OpenTextFile _
    ("C:\badFileNames.txt", 2, true)
    set namesOK = fso_OpenTextFile _
    ("C:\namesOK.txt", 2, true)

    set r = new RegExp
    r.Pattern = "^[A-Z](-\d{2}){4}-"
    r.IgnoreCase = true

    checkNames (startFolder)

    Wscript.Echo "Done"

    sub checkNames (folder)
    for each file in folder.Files
    if r.Test (file.Name) then
    namesOK.WriteLine file.Path
    else
    badFileNames.WriteLine file.Path
    end if
    next
    for each f in folder.Subfolders
    checkNames (f)
    next
    end sub
     
    urkec, Jan 30, 2007
    #4
  5. Lanwench [MVP - Exchange]

    urkec Guest

    If you are not sure what naming scheme you're going to use, you could split
    each file name into an array, so:

    X-00-dd-mm-yy-keywords-author.doc, would be:

    arr(0) = X
    arr(1) = 00
    arr(2) = dd
    arr(3) = mm
    arr(4) = yy
    ....

    and then arrange the elements as you like.

    I tried to rearrange

    X-00-dd-mm-yy-keywords-author.doc

    to be like this

    X-00-yyyy-mm-dd-....

    but you could use some other pattern, rearranging array elements . I also
    didn't account for year < 2000, but that can be added. This takes file names
    from C:\test and subfolders,doesn't rename files, but writes
    old_name,new_name to C:\namesOK.txt . But be careful, I think you don't want
    to end up with only half of the files renamed. First check consistency of
    names.

    set fso = CreateObject _
    ("Scripting.FileSystemObject")
    set startFolder = fso.GetFolder ("C:\test")
    set badFileNames = fso_OpenTextFile _
    ("C:\badFileNames.txt", 2, true)
    set namesOK = fso_OpenTextFile _
    ("C:\namesOK.txt", 2, true)

    set r = new RegExp
    r.Pattern = "^[A-Z](-\d{2}){4}-"
    r.IgnoreCase = true

    changeNames (startFolder)

    Wscript.Echo "Done"

    sub changeNames (folder)
    for each file in folder.Files
    if r.Test (file.Name) then
    arr = Split (file.Name, "-")
    str = arr (0) & "-"
    str = str & arr (1) & "-"
    str = str & "20" & arr (4) & "-"
    str = str & arr (3) & "-"
    str = str & arr (2)
    for i = 5 to UBound (arr)
    str = str & "-" & arr (i)
    next
    namesOK.WriteLine file.Name & "," & str
    else
    badFileNames.WriteLine file.Path
    end if
    next
    for each f in folder.Subfolders
    changeNames (f)
    next
    end sub
     
    urkec, Jan 30, 2007
    #5
  6. Lanwench [MVP - Exchange]

    Paul Randall Guest

    "Lanwench [MVP - Exchange]"
    Yes, first you get a file system object for the root folder of interest,
    then you recurse through that folder and all its sub-folders, and loop
    through the collection of files in these folders, renaming them using code
    like those four lines.

    In case you don't know what a file system object is:
    VBScript is a simple programming language, that can manipulate numbers and
    strings and provisionaly execute sections of code based on comparisons. By
    itself, VBScript can ony interact with the world trough the keyboard and
    computer screen. But it does have the capability to interact with 'COM'
    objects. By default, Windows installs a bunch of such objects. They can
    give access to the file system, the network, other computers on the network,
    WWW, certain databases, most or all of the Microsoft Office applications
    installed on the computer, etc. VBScript's CreateObject statement can
    create, for example, file system objects, Internet Explorer objects, and
    Microsoft Excel object, if they have been installed on your system. The
    file system object has methods and properties that allow the script to do
    almost anything with a file, including rename it.
    Hi, Lanwench

    There are a few essentials you need to learn scripting.
    1. The scripting execution engine. If you have Internet Explorer, then you
    have some version of Microsoft's VBScript and JScript engines. If you have
    IE6, then you probably have the latest version. Microsoft says it will not
    provide any more updates to VBScript. If you want to upgrade due to an old
    version, you need to know that different versions of Windows may require
    different versions of the scripting engine. Download here:
    http://www.microsoft.com/downloads/...43-7e4b-4622-86eb-95a22b832caa&DisplayLang=en

    2. The documentation is definitely not perfect, but has both VBScript and
    JScript examples for nearly everything a beginner needs to learn the
    language. The examples are aimed towards script usage within HTML pages. I
    think this project is more of a stand-alone application. Some translation
    is needed to test the sample code in a stand-alone situation. Download docs
    here:
    http://www.microsoft.com/downloads/...48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en

    3. An editor that can save plain text files with the extension .VBS.
    Notepad will do, but debugging is tedious with Notepad. When an error
    occurs, the scripting engine tells you the line number and some other info
    about the problem. About the second time you have to find line 111 in a
    script you will understand the need for easily getting to a particular line.
    If you have any Microsoft Office applications installed, then you likely
    have an excellent editor called VBE installed and perhaps the Microsoft's
    script debugger. I currently use a free multi-scripting-language editor,
    PSPad, available from http://www.pspad.com/en/; it has few or no nags. A
    VBScript-only editor I've started using which can do more for VBScripters,
    but nags when you try to access a feature only available in the non-free
    version, is VBSEdit, from www.vbsedit.com.

    The best source of Windows scripting info is
    http://www.microsoft.com/technet/scriptcenter/default.mspx
    Novices should read the link for Scripting: Your First Steps

    There are a few places with tutorials on VBScript but they are usually aimed
    at VBScript in a web server and VBscript in a web page. After some
    stumbling around, I think you will quickly learn to ignore the stumbling
    blocks.

    Googleing for the word free followed by the phrase in quotes "vbscript code"
    will provide links to both code and tutorials.

    http://www.w3schools.com/ has a lot of good tutorials/sample code, but all
    is aimed at web pages. For playing with this, I sometimes use a free editor
    from www.coffeecup.com. It's line numbering feature is somewhat broken, but
    you can paste samples from HTML tutorials into the editing tab window and
    then switch to the preview tab to see that HTML rendered, and interact
    (click, fill in boxes, etc), just as if you had saved the code and opened it
    in a browser. Its quick and easy to switch between modifying a script in
    HTML and seeing the result of that modification.

    There are lots of places with sample code which can be cut and pasted into
    your projects. Often the sample code is poorly documented, but it is still
    valuable in getting you going in the right direction. There are many
    different ways to do the same thing. Ten commented statements
    understandable only to a scripting Guru might produce the same result as 100
    uncommented statements understandable by a novice. When things go wrong,
    its is nice to be working with code you kind of understand.

    As you browse Microsoft's newsgroups, pay particular attention to answers by
    MVPs. These people have demonstrated the ability to provide good
    information in the newsgroup. Scripts they post often contain useful
    routines and techniques.

    Many of the MVPs have web sites with excellent sample code like
    Alex Angelopolous' http://dev.remotenetworktechnology.com/ and
    Richard Mueller's http://www.rlmueller.net/products.htm
    They often list their web site in their signature.

    Go to Alex's site, navigate to the WSH and VBScript items and browse the few
    lines of comments in most of the routines so you can get an idea of what's
    there -- you will probably come back and get things of interest.
     
    Paul Randall, Jan 30, 2007
    #6
  7. Lanwench [MVP - Exchange]

    Paul Randall Guest

    "Lanwench [MVP - Exchange]"
    If Windows Explorer shows the correct date, VBScript can use it in the file
    name.
    Yes, starting at a base folder, the script can recurse through that folder
    and all levels of subfolders to rename the files within those folders.
    There are a few essentials you need to learn scripting.
    1. The scripting execution engine. If you have Internet Explorer, then you
    have some version of Microsoft's VBScript and JScript engines. If you have
    IE6, then you probably have the latest version. Microsoft says it will not
    provide any more updates to VBScript. If you want to upgrade due to an old
    version, you need to know that different versions of Windows may require
    different versions of the scripting engine. For W2K and WXP, download here:
    http://www.microsoft.com/downloads/...43-7e4b-4622-86eb-95a22b832caa&DisplayLang=en

    2. The documentation is definitely not perfect, but has an excellent index
    and examples of use of most key words in both VBScript and JScript. The
    examples are aimed towards script usage within HTML pages. I think this
    project is more of a stand-alone application. Some translation is needed to
    test the sample code in a stand-alone situation. Download docs here:
    http://www.microsoft.com/downloads/...48-207d-4be1-8a76-1c4099d7bbb9&DisplayLang=en

    Read the VBScript User's Guide section - its listed in the index.

    3. An editor that can save plain text files with the extension .VBS.
    Notepad will do, but debugging is tedious with Notepad. When an error
    occurs, the scripting engine tells you the line number and some other info
    about the problem. About the second time you have to find line 111 in a
    script you will understand the need for easily getting to a particular line.
    If you have any Microsoft Office applications installed, then you likely
    have an excellent editor called VBE installed and perhaps the Microsoft's
    script debugger. I currently use a free multi-scripting-language editor,
    PSPad, available from http://www.pspad.com/en/; it has few or no nags. A
    VBScript-only editor I've started using which can do more for VBScripters,
    but nags when you try to access a feature only available in the non-free
    version, is VBSEdit, from www.vbsedit.com.

    The best source of Windows scripting info is
    http://www.microsoft.com/technet/scriptcenter/default.mspx
    Novices should read the link for Scripting: Your First Steps

    There are a few places with tutorials on VBScript but they are usually aimed
    at VBScript in a web server and VBscript in a web page. After some
    stumbling around, I think you will quickly learn to ignore the stumbling
    blocks.

    Googleing for the word free followed by the phrase in quotes "vbscript code"
    will provide links to both code and tutorials.

    http://www.w3schools.com/ has a lot of good tutorials/sample code, but all
    is aimed at web pages. For playing with this, I sometimes use a free editor
    from www.coffeecup.com. It's line numbering feature is somewhat broken, but
    you can paste samples from HTML tutorials into the editing tab window and
    then switch to the preview tab to see that HTML rendered, and interact
    (click, fill in boxes, etc), just as if you had saved the code and opened it
    in a browser. Its quick and easy to switch between modifying a script and
    seeing the result of that modification.

    There are lots of places with sample code which can be cut and pasted into
    your projects. Often the sample code is poorly documented, but it is still
    valuable in getting you going in the right direction. There are many
    different ways to do the same thing. Ten commented statements
    understandable only to a scripting Guru might produce the same result as 100
    uncommented statements understandable by a novice. When things go wrong,
    its is nice to be working with code you kind of understand.

    As you browse Microsoft's newsgroups, pay particular attention to answers by
    MVPs. These people have demonstrated the ability to provide good
    information in the newsgroup. Scripts they post often contain useful
    routines and techniques.

    Many of the MVPs have web sites with excellent sample code like
    Alex Angelopolous' http://dev.remotenetworktechnology.com/ and
    Richard Mueller's http://www.rlmueller.net/products.htm
    They often list their web site in their signature.

    Go to Alex's site, navigate to the WSH and VBScript items and browse comment
    lines in most of the routines so you can get an idea of whats there -- you
    will probably come back and get things of interest.

    -Paul Randall
     
    Paul Randall, Jan 30, 2007
    #7
  8. Lanwench [MVP - Exchange]

    Paul Randall Guest

    Hi, Ayush
    I was hoping you would post some sample code. But since Lanwench is a
    novice to VBScript, I think using RegExp may be difficult to understand. On
    the other hand, since RegExp is so powerful and compact, Lanwench is a MVP,
    that may be a plus.

    -Paul Randall
     
    Paul Randall, Jan 30, 2007
    #8
  9. In

    Thank you again, Paul - I'll take all your advice under consideration. The
    client is now rethinking the decision to script this stuff anyway, as it
    turns out the file naming convention they've been using isn't actually very
    consistent - and this makes it somewhat a moot point for the moment. I do
    need to get up to snuff on this sort of thing, though, I think - and I
    appreciate your help. I do pay attention to MVP answers....it'd be silly for
    me not to. :)
     
    Lanwench [MVP - Exchange], Feb 1, 2007
    #9
  10. In
    <snip>

    Thank you, urkec - I appreciate the reply. For the time being the entire
    project's in a hold state but I'm learning a lot in here :)
     
    Lanwench [MVP - Exchange], Feb 1, 2007
    #10
  11. In
    <snip>

    Thank you much, Ayush - this is becoming very helpful info. Most kind of
    you.
     
    Lanwench [MVP - Exchange], Feb 1, 2007
    #11
  12. Lanwench [MVP - Exchange]

    Ayush Guest

    Replied to [Lanwench [MVP - Exchange]]s message :

    You are welcome. Glad to help.
     
    Ayush, Feb 1, 2007
    #12
    1. Advertisements

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments (here). After that, you can post your question and our members will help you out.