Help with program for renaming files

hello i’m new here .recently i was trying to improve my work life since it is really tiring to getting those bunch on files to be renamed . after 2 days working I realized there is a pattern for those file that rename before and after.

this the code I work with

import os

folder_path = r’D:\CSL excel\New folder’

intro_count = 1
full_count = 1
previous_file_renamed = None
last_renamed_ext = None

for dirpath, dirnames, filenames in os.walk(folder_path):
subfolder_name = os.path.basename(dirpath)
for filename in sorted(filenames):
file_path = os.path.join(dirpath, filename)
name, ext = os.path.splitext(filename)


    if ext == '.mp3':
        if last_renamed_ext == '.jpg' and 'FOTO_STNK' in name:
            new_name = f'{subfolder_name} STNK.mp3'
            previous_file_renamed = 'STNK'
        elif previous_file_renamed == None or previous_file_renamed.startswith('intro'):
            new_name = f'{subfolder_name} intro {intro_count}.mp3'
            intro_count += 1
            previous_file_renamed = 'intro'
        elif previous_file_renamed == 'misscall':
            new_name = f'{subfolder_name} STNK.mp3'
            previous_file_renamed = 'STNK'
        elif previous_file_renamed == 'STNK':
            new_name = f'{subfolder_name} Full {full_count}.mp3'
            full_count += 1
            previous_file_renamed = 'Full'
        else:
            new_name = f'{subfolder_name} Full {full_count}.mp3'
            full_count += 1
    elif ext == '.jpg':
        if 'FOTO_RESP' in name:
            new_name = f'{subfolder_name} misscall.jpg'
            previous_file_renamed = 'misscall'
        elif 'FOTO_STNK' in name:
            new_name = f'{subfolder_name} STNK.jpg'
            previous_file_renamed = 'STNK'
        elif 'GIFT_FOTO_GIFT' in name:
            new_name = f'{subfolder_name} gift.jpg'
            previous_file_renamed = 'gift'
    else:
        continue
    
    last_renamed_ext = ext
    new_path = os.path.join(dirpath, new_name)
    if not os.path.exists(new_path):
        os.rename(file_path, new_path)


    new_path = os.path.join(dirpath, new_name)
    if not os.path.exists(new_path):
        os.rename(file_path, new_path)

this is work but not as i intended.

4231820 - J10 - 6490/ <= (this is 1st subfolder ) AFTER I RUN THE CODE
3561_4231820_ID_1_17753527___undefined.mp3 => 4231820 - J10 - 6490 intro1
3561_4231820_ID_2_17753544___Z0-P12b.mp3 => 4231820 - J10 - 6490 intro2
3561_4231820_ID_3_17753562___Z0-P12b_1.mp3 => 4231820 - J10 - 6490 3
3561_4231820_ID_4_17753583__Z7c_FOTO_RESP.jpg => 4231820 - J10 - 6490 misscall .jpg
3561_4231820_ID_5_17753588___Z0-P12b_2.mp3 => 4231820 - J10 - 6490 STNK.mp3
3561_4231820_ID_8_17753596__P12b_FOTO_STNK.jpg => 4231820 - J10 - 6490 STNK .jpg
3561_4231820_ID_10_17753614___S1-DG5.mp3 => 4231820 - J10 - 6490 full1.mp3
3561_4231820_ID_11_17753618___Demografi.mp3 => 4231820 - J10 - 6490 full2. mp3
3561_4231820_ID_12_17753624__GIFT_FOTO_GIFT.jpg => 4231820 - J10 - 6490 gift.jpg
3561_4231820_ID_13_17753630___Demografi_1.mp3=> 4231820 - J10 - 6490 full3. mp3

4231822 - J10 - 6490/

3562_4231822_ID_1_17753642___undefined.mp3 => 4231822 - J10 - 6490 full4
3562_4231822_ID_2_17753646___Z0-P12b.mp3 => 4231822 - J10 - 6490 full5
3562_4231822_ID_3_17753650__Z7c_FOTO_RESP.jpg => 4231822 - J10 - 6490 Misscall
3562_4231822_ID_4_17753655___Z0-P12b_1.mp3 => 4231822 - J10 - 6490 STNK
3562_4231822_ID_5_17753661__P12b_FOTO_STNK.jpg => 4231822 - J10 - 6490 STNK
3562_4231822_ID_7_17753673___S1-DG5.mp3 => 4231822 - J10 - 6490 full 6
3562_4231822_ID_8_17753677___Demografi.mp3 => 4231822 - J10 - 6490 full 7
3562_4231822_ID_9_17753680__GIFT_FOTO_GIFT.jpg => 4231822 - J10 - 6490 full 8
3562_4231822_ID_10_17753683___Demografi_1.mp3 => 4231822 - J10 - 6490 full 9 

why in the second folder there no intro in it?

i want that after they that done with first folder the next folder should be have intro too i think .

please help…

Please wrap code in triple backticks to preserve the formatting:

```
if True:
    print(''Hello world!')
```

You can do that by selecting the code and then clicking the </> button.

It’s difficult to tell because of the messed-up formatting, but I think the problem is with this:

elif previous_file_renamed == None or previous_file_renamed.startswith('intro'):

You’re initialising previous_file_renamed = None before the first loop, so the condition is true for the first few of the files of the first folder.

However, after finishing that folder, it still has its last value, which is neither None nor 'intro'.

Therefore, for the second folder, the condition won’t be true for the first few files.

The fix is to reset previous_file_renamed back to None (if I understand the code correctly). Just move that line inside the outer loop.

import os

folder_path = r'D:\CSL excel\New folder'

intro_count = 1
full_count = 1
previous_file_renamed = None
last_renamed_ext = None

for dirpath, dirnames, filenames in os.walk(folder_path):
    subfolder_name = os.path.basename(dirpath)
    for filename in sorted(filenames):
        file_path = os.path.join(dirpath, filename)
        name, ext = os.path.splitext(filename)

        if ext == '.mp3':
            if last_renamed_ext == '.jpg' and 'FOTO_STNK' in name:
                new_name = f'{subfolder_name} STNK.mp3'
                previous_file_renamed = 'STNK'
            elif previous_file_renamed == None or previous_file_renamed.startswith('intro'):
                new_name = f'{subfolder_name} intro {intro_count}.mp3'
                intro_count += 1
                previous_file_renamed = 'intro'
            elif previous_file_renamed == 'misscall':
                new_name = f'{subfolder_name} STNK.mp3'
                previous_file_renamed = 'STNK'
            elif previous_file_renamed == 'STNK':
                new_name = f'{subfolder_name} Full {full_count}.mp3'
                full_count += 1
                previous_file_renamed = 'Full'
            else:
                new_name = f'{subfolder_name} Full {full_count}.mp3'
                full_count += 1
        elif ext == '.jpg':
            if 'FOTO_RESP' in name:
                new_name = f'{subfolder_name} misscall.jpg'
                previous_file_renamed = 'misscall'
            elif 'FOTO_STNK' in name:
                new_name = f'{subfolder_name} STNK.jpg'
                previous_file_renamed = 'STNK'
            elif 'GIFT_FOTO_GIFT' in name:
                new_name = f'{subfolder_name} gift.jpg'
                previous_file_renamed = 'gift'
        else:
            continue
        
        last_renamed_ext = ext
        new_path = os.path.join(dirpath, new_name)
        if not os.path.exists(new_path):
            os.rename(file_path, new_path)