Results 1 to 7 of 7
  1. #1
    New Lounger
    Join Date
    Nov 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Lightbulb Bulk rename folders by file's first word?

    Hello everyone,

    My problem is somewhat weird but I think there has got to be a way around it.
    Suppose I have the following files on my Windows 7 Home Premium:

    Tokyo - Madrid.doc
    Tokyo - New York.doc
    London Tokyo New York.doc
    2011 London New York.doc
    2011 - Anonymous.doc

    I would like to create new folders named by the first word of the file only. And then move these files into them. So in this case I will only have 3 folders for the 5 files:

    Tokyo> Tokyo - Madrid.doc
    Tokyo> Tokyo - New York.doc
    London> London Tokyo New York.doc
    2011> 2011 London New York.doc
    2011> 2011 - Anonymous.doc


    I have thousands of these files and I would really appreciate if someone showed me if this can be done in BRU. I googled it and my problem seems unique.

    Thanks..

  2. #2
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    828
    Thanks
    4
    Thanked 38 Times in 34 Posts
    BRU? Bus Riders Union? Brussles Airport? (that what Google tells me)

    Try using this PowerShell script:
    filter moveFile {
    $firstWord = $_.Name.Split()[0]
    New-Item $firstWord -type directory
    Move-Item $_.Name $firstWord
    }

    Get-ChildItem | Where-Object {!$_.PSisContainer} | moveFile

  3. #3
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Cafed00d,

    For those of us trying to learn PS could you please explain your wizardry?
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  4. #4
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    828
    Thanks
    4
    Thanked 38 Times in 34 Posts
    Sure.

    The first 5 lines are a filter. Filters are much like functions with one significant difference - if you pipe a collection to a filter, the filter is called once for each object (unlike a function, in which case the collection is piped to it). Thus the filter needs to deal with only the current object passed to it. That object can be accessed using "$_".

    The moveFile filter assumes that it will be passed a File object. Thus the $_ represents the current file. The first line of the filter uses the String.Split() method to split the file's Name into individual words. String.Split() returns a collection of words, the "[0]" index returns the first word. Thus if $_ is "some file name.txt", then $firstWord gets set to "some".

    The next line (New-Item) creates a new directory using the $firstWord of the name.

    The Move-Item line moves the file to the newly created directory.

    Note that what I have done is reduced the task into handling a single file. So rather than thinking about the complexity of handling multiple files, I only worry about one file at a time.

    Now to tackle the larger problem of handling all of the file in a directory.

    The Get-ChildItem cmdlet gets all of the contents of the current directory. Those contents are piped, one at a time, to the Where-Object cmdlet which filters out the directories. Thus if it is a directory, it is not passed on. If it is a plain file, it is passed to the moveFile filter which then does it magic as described above.

    Note that this script runs only in the current directory. Simple modifications to the script will allow it to run on any directory, and even enable it to run recursively through all subdirectories.

  5. The Following User Says Thank You to cafed00d For This Useful Post:

    RetiredGeek (2011-12-03)

  6. #5
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,434
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Cafed00d,

    Thank you so much for that most lucid explanation. I now not only understand your code but also understand the use of filters in PS. Again THANKS!

    One more question if I may? What happens if there are 2 files with the same 1st word? Does the filter generate an error when it tries to re-create the directory and if so how is this handled?
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  7. #6
    5 Star Lounger
    Join Date
    Jan 2010
    Location
    Los Angeles, CA
    Posts
    828
    Thanks
    4
    Thanked 38 Times in 34 Posts
    Yes, that is a bug in the script. The New-Item cmdlet will fail if the directory already exists. Change that line to read:

    New-Item $firstWord -type directory -ErrorAction:SilentlyContinue

    The extra parameter will cause New-Item to ignore the failure to create a directory of the same name. An alternate mechanism is to first test if the directory already exists and not create it if it does. Which one you do depends on your preference and also on the rest of the sript. For example, if I have several lines related to create a new directry, I would first check if the directory exited. But since only the New-Item line depends on that condition, ignoring the error is the simplest.

  8. #7
    Gold Lounger
    Join Date
    Oct 2007
    Location
    Johnson City, Tennessee, USA
    Posts
    3,202
    Thanks
    37
    Thanked 215 Times in 202 Posts
    Quote Originally Posted by FlexibleCogito View Post
    Hello everyone,

    My problem is somewhat weird but I think there has got to be a way around it.
    "Flex",
    Hello... I found this ...but you have to act quickly.....only for today (12\6\11) this program (Batch File Renamer) is being offered for Free "Givaway of the Day" ... don't know if it will help or not as i have never used it, and the "Power Shell" is above my pay grade.... Regards Fred
    PlainFred

    None are so hopelessly enslaved as those who falsely believe they are free (J. W. Von Goethe)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •