[Request] Script for creating folders based on filename

Discussion in 'Scripting' started by mad_max, Nov 20, 2020.

  1. mad_max

    mad_max MDL Junior Member

    Mar 31, 2014
    76
    40
    0
    Hello,
    I've got lot of tiff files. I need to create batch file (Windows only - if it is not possible linux bash script will be fine too) that will be creating folders based on filename.
    For example:
    File name is 406-1-0-0000-00.tiff so I need to create directory X:\406\1\0\0000\00\ and put that file there.
    406-1-2-7019-03.tiff -> create directory X:\406\1\2\7019\03\ and copy this file to that directory
    ...and so on.

    Thanks in advance
     
  2. mad_max

    mad_max MDL Junior Member

    Mar 31, 2014
    76
    40
    0
    Thanx @freddie-o but I need something different.
    In my case there should be more folders - not just one based on a filename.
    For example my file is "406-1-2-7019-03.tiff" so I need something like that:
    Code:
    mkdir 406
    cd 406
    mkdir 1
    cd 1
    mkdir 2
    cd 2
    mkdir 7019
    cd 7019
    mkdir 03
    cd 03
    copy x:\406-1-2-7019-03.tiff z:\406\1\2\7019\03\
    or just
    Code:
    mkdir z:\406\1\2\7019\03
    copy x:\406-1-2-7019-03.tiff z:\406\1\2\7019\03\
    
    and so on for about 300 files with different names (but same "naming method").

    I think my batch file should:
    1) take all of "-" from filename and change it to "\"
    so when we have got file 406-1-5-3011-00.tiff script create variable \406\1\5\3011\00
    2) create folder based on 1)
    3) copy file (with original name) to that folder made in 2)

    But I have no idea how to do it :confused:
     
  3. kaljukass

    kaljukass MDL Guru

    Nov 26, 2012
    3,396
    1,322
    120
    Nobody can't because file, folder, directory names can't contain such a symbols:
    Code:
    \ / * : ? | " < >
    If You want use this z:\406\1\2\7019\03\, then it means
    z:\ - z-drive or partition
    406 - folder
    1 -folder what located in folder 406
    2 - folder what located in floder 1 and the folder 1 located in folder 406 on z-drive or partition.
    etc
     
    Stop hovering to collapse... Click to collapse... Hover to expand... Click to expand...
  4. mad_max

    mad_max MDL Junior Member

    Mar 31, 2014
    76
    40
    0
    That is exacly what I'm trying to achive.
    My files contains only "-" symbols so I want to create 406 folder and 1 which is located inside that folder and 1 inside (and so on) and than copy file 406-1-2-7019-03.tiff to that folder.
     
  5. GezoeSloog

    GezoeSloog knows a guy, who knows another guy.

    Feb 10, 2012
    864
    8,154
    30
    Code:
    setlocal EnableDelayedExpansion
    
    for %%F in (*.tiff) do (
      set sub=%%~nF
      set sub=X:\!sub:-=\!
      md !sub!
      copy /b %%F !sub!
    )
    
     
  6. mad_max

    mad_max MDL Junior Member

    Mar 31, 2014
    76
    40
    0
    Great work @GezoeSloog ! I'm very greatful. Thank you!:worthy: