Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: Create folders based on part of a filename  (Read 3663 times)

0 Members and 1 Guest are viewing this topic.

jrosalez

    Topic Starter


    Newbie

    • Experience: Familiar
    • OS: Windows 7
    Create folders based on part of a filename
    « on: November 22, 2012, 10:26:16 AM »
    I'm really pulling my hair out on this one. I collect pictures of motorcycles, hot rod cars (especially Mustangs), and racing boats. I use bulk file renamer to place them in the following format:

    KawasakiNinja300-001-001.jpg
    KawasakiNinja300-001-002.jpg
    KawasakiNinja300-001-003.jpg
    KawasakiNinja300-002-001.jpg
    KawasakiNinja300-002-002.jpg
    KawasakiNinja300-002-003.jpg
    KawasakiNinja300-161-150.jpg

    Sometimes the second part of the file name will not contain 3 numbers but sometimes 2,1, or even 4. For example:

    1990BlackMustangGT-01-01.jpg

    LamborghiniCountach-1-01.jpg

    And the list goes on with thousands of files in their respective directories.
    The first part of the filename is the make and model of the vehicle. I use a hyphen as a separator to denote the numbered series that it belongs to and another hyphen to denote the individual picture within the series. So essentially the filename is broken down into three parts using a hyphen as a separator.

    I'm looking for a batch file that will move each file into a directory named after the numbers after the first hyphen and before the second hyphen (the second part of the filename).

    Thank you so much.

    Jason


    foxidrive



      Specialist
    • Thanked: 268
    • Experience: Experienced
    • OS: Windows 8
    Re: Create folders based on part of a filename
    « Reply #1 on: November 22, 2012, 03:19:42 PM »
    Try this on a sample of your files - not the real set.  It's untested.  Run it from the root folder of your collection after testing.

    If it works ok - remember not to run it twice on the same set of folders or it will create an extra level deep of folders.

    Where it will have an issue is if any of the descriptive parts of the filename has a - in it.    Say Kawasaki-Jawa-001-002.jpg

    Code: [Select]
    @echo off
    for /f "delims=" %%z in ('dir /ad /b /s') do (
    pushd "%%z"
    for %%a in (*.jpg) do (
    for /f "tokens=2 delims=-" %%b in ("%%a") do (
    md "%%b" 2>nul
    move "%%a" "%%b" >nul
    )
    )
    popd
    )
    pause
    « Last Edit: November 22, 2012, 03:52:20 PM by foxidrive »