VideoHelp Forum
+ Reply to Thread
Results 1 to 3 of 3
Thread
  1. Rename a multiple of images within folders, each folder contains a multiple of images,
    each folder with a specific number, using text files, each text file with a folder number


    before

    Code:
    Multi Folder Images
    │
    │
    ├── E01
    │   ├── 0001.png
    │   ├── 0002.png
    │   ├── 0003.png
    │   ├── 0004.png
    │   └── 0005.png
    │     
    ├── E02
    │   ├── 0001.png
    │   ├── 0002.png
    │   ├── 0003.png
    │   ├── 0004.png
    │   └── 0005.png
    │ 
    │  
    ├── E03
    │   ├── 0001.png
    │   ├── 0002.png
    │   ├── 0003.png
    │   ├── 0004.png
    │   └── 0005.png

    Code:
    Multi Folder text
    
    │
    │
    ├── E01.txt   "inside text"
    │   ├── 0_00_01_713__0_00_05_081_01
    │   ├── 0_00_05_093__0_00_08_916_02
    │   ├── 0_00_08_926__0_00_12_339_03
    │   ├── 0_00_12_347__0_00_14_156_04
    │   └── 0_00_14_164__0_00_16_314_05
    │     
    ├── E02.txt
    │   ├── 0_00_16_324__0_00_17_348_06
    │   ├── 0_00_17_352__0_00_21_255_07
    │   ├── 0_00_21_260__0_00_23_501_08
    │   ├── 0_00_23_507__0_00_25_999_09
    │   └── 0_00_26_010__0_00_29_002_10
    │ 
    │  
    ├── E03.txt
    │   ├── 0_00_29_013__0_00_32_176_11
    │   ├── 0_00_32_182__0_00_35_982_12
    │   ├── 0_00_35_993__0_00_37_245_13
    │   ├── 0_00_37_253__0_00_40_177_14
    │   └── 0_00_40_190__0_00_43_717_15

    after renaming

    Code:
    Multi Folder Images
    │
    │
    ├── E01
    │   ├── 0_00_01_713__0_00_05_081_01.png
    │   ├── 0_00_05_093__0_00_08_916_02.png
    │   ├── 0_00_08_926__0_00_12_339_03.png
    │   ├── 0_00_12_347__0_00_14_156_04.png
    │   └── 0_00_14_164__0_00_16_314_05.png
    │     
    ├── E02
    │   ├── 0_00_16_324__0_00_17_348_06.png
    │   ├── 0_00_17_352__0_00_21_255_07.png
    │   ├── 0_00_21_260__0_00_23_501_08.png
    │   ├── 0_00_23_507__0_00_25_999_09.png
    │   └── 0_00_26_010__0_00_29_002_10.png
    │ 
    │  
    ├── E03
    │   ├── 0_00_29_013__0_00_32_176_11.png
    │   ├── 0_00_32_182__0_00_35_982_12.png
    │   ├── 0_00_35_993__0_00_37_245_13.png
    │   ├── 0_00_37_253__0_00_40_177_14.png
    │   └── 0_00_40_190__0_00_43_717_15.png

    after & before renaming
    https://app.box.com/s/da31306nr4hizwgezi47kn6bv941i1f5

    I have this script Works on one folder . Please help to edit script

    Code:
    import pathlib
    
    IMG_DIR = pathlib.Path('.\Images\E01')
    
    with open('E01.txt') as jabber:
        content = jabber.read().splitlines()
        director = sorted(IMG_DIR.iterdir())
        for src, dest in zip(director, content):
            src.rename(IMG_DIR / f'{dest}{src.suffix}')
    Quote Quote  
  2. assuming txt files are all in that root directory, like in your example Image directory
    Code:
    import pathlib
    
    ROOT_DIR =  pathlib.Path(r'F:\... path to root directory ...\Images')
    
    def rename_directory_files(directory, text_file, print_only=False):
        '''
        test it with print_only=True before actual renaming, if getting good results, proceed with renaming
        '''    
        with open(text_file) as f:
            new_names = f.read().splitlines()
        files = sorted([file for file in directory.iterdir() if file.is_file()])
        for file, new_name in zip(files, new_names): 
            if new_name and file:
                if print_only:
                    print(file, file.parent / f'{new_name}{file.suffix}')
                else:
                    file.rename(file.parent / f'{new_name}{file.suffix}')
    
    directories = [d for d in ROOT_DIR.iterdir() if d.is_dir()]
    for directory in directories:
        rename_directory_files(
                         directory = directory,
                         text_file = ROOT_DIR / f'{directory.name}.txt',
                         print_only = True
                         )
    Quote Quote  
  3. Thank you. It works
    Quote Quote  



Similar Threads

Visit our sponsor! Try DVDFab and backup Blu-rays!