how to get new name for creating new folder, with new name to new folder

Viewed 35

On Python, I am using re library to get new name for creating new folder, with new name to new folder, if folder name already exists that you want to create, then it will return new name :
Example:

  1. New Folder_Copy2 --> New Folder_Copy3
  2. New Folder_Copy --> New Folder_Copy 1
  3. New Folder_Copy 1 --> New Folder_Copy 2
  4. copy1, copy2
import re

def get_new_copy_name(name, exists_name_list, post_fix):
    """
        # this function, for getting duplicate name
        # Parameter:
            name: user want to create copy
            exists_name_list: already exists name
            post_fix: _Copy
    """
    tmp_num = 0
    exists_name_list = [e_n_l for e_n_l in exists_name_list if name.split(post_fix)[0] in e_n_l]
    if len(exists_name_list) == 0:
        exists_name_list = [name]

    for e_n_l in exists_name_list:
        # regex filter
        regex = re.compile(r"" + f"(?i){post_fix} ?(([0-9]+)?)$")
        has = regex.search(e_n_l)

        if has is not None:
            last_str = has.group()
            num = has.groups()[0]
            if num == "":
                num = 1
            else:
                num = int(num)+1

        else:
            last_str = ""
            num = 0
        
        if tmp_num < num:
            tmp_num = num
        
            
    if tmp_num == 0:
        tmp_num = ""
    new_name = f"{e_n_l.replace(last_str, '')}{post_fix}{tmp_num}"
    return new_name

post_fix="_Copy"
name="Exp123_Copy(9)"
exists_name_list = [
    "Exp123_Copy20",
    "Exp123_Copy3",
    "Exp123_Copy19",
    "Exp123_Copy6",
    "Exp345",
    "Exp123",
    "Exp123_Copy1005",
    "Exp123_Copy6",
    "Exp345_Copy60",
]
print(get_new_copy_name(name, exists_name_list, post_fix))

Output: Exp123_Copy1006

2 Answers

On Python, I am using re library to get new name for creating new folder, with new name to new folder, if folder name already exists that you want to create, then it will return new name :
Example:

  1. New Folder --> New Folder_Copy
  2. New Folder_Copy --> New Folder_Copy(1), New Folder_Copy(2), New Folder_Copy(3)
  3. New Folder_Copy(2) --> New Folder_Copy(3)
  4. copy(1), copy(2)
import re

def get_new_copy_name(name, exists_name_list, post_fix):
    """
        # this function, for getting duplicate name
        # Parameter:
            name: user want to create copy
            exists_name_list: already exists name
            post_fix: _Copy
    """
    tmp_num = 0
    exists_name_list = [e_n_l for e_n_l in exists_name_list if name.split(post_fix)[0] in e_n_l]

    if len(exists_name_list) == 0:
        exists_name_list = [name]

    for e_n_l in exists_name_list:
        # regex filter
        regex = re.compile(r"" + f"(?i){post_fix} ?((\([0-9]+\))?)$")
        has = regex.search(e_n_l)

        if has is not None:
            last_str = has.group()
            num = has.groups()[0]
            num = num[1:-1]
            if num == "":
                num = 1
            else:
                num = int(num)+1

        else:
            last_str = ""
            num = 0
        
        if tmp_num < num:
            tmp_num = num
        
            
    if tmp_num == 0:
        tmp_num = ""
    else:
        tmp_num = f"({tmp_num})"
    new_name = f"{e_n_l.replace(last_str, '')}{post_fix}{tmp_num}"
    return new_name

post_fix="_Copy"
name="Exp1232_Copy(629)"
exists_name_list = [
    "Exp123_Copy(20)",
    "Exp123_Copy(3)",
    "Exp123_Copy(2)",
    "Exp123_Copy(19)",
    "Exp123_Copy(6)",
    "Exp345",
    "Exp123",
    "Exp123_Copy(1005)",
    "Exp123_Copy(6)",
    "Exp345_Copy(60)",
]
print(get_new_copy_name(name, exists_name_list, post_fix))

Output: Exp1232_Copy(630)

On Python, I am using re library to get new name for creating new folder, with new name to new folder, if folder name already exists that you want to create, then it will return new name :
Example:

  1. New Folder --> New Folder_Copy
  2. New Folder_Copy --> New Folder_Copy (1), New Folder_Copy (2), New Folder_Copy (3)
  3. New Folder_Copy (2) --> New Folder_Copy (3)
  4. copy (1), Copy (2)
import re

def get_new_copy_name(name, exists_name_list, post_fix):
    """
        # this function, for getting duplicate name
        # Parameter:
            name: user want to create copy
            exists_name_list: already exists name
            post_fix: _Copy
    """
    tmp_num = 0
    exists_name_list = [e_n_l for e_n_l in exists_name_list if name.split(post_fix)[0] in e_n_l]

    if len(exists_name_list) == 0:
        exists_name_list = [name]

    for e_n_l in exists_name_list:
        # regex filter
        regex = re.compile(r"" + f"(?i){post_fix} ?((\([0-9]+\))?)$")
        has = regex.search(e_n_l)

        if has is not None:
            last_str = has.group()
            num = has.groups()[0]
            num = num[1:-1]
            if num == "":
                num = 1
            else:
                num = int(num)+1

        else:
            last_str = ""
            num = 0
        
        if tmp_num < num:
            tmp_num = num
        
            
    if tmp_num == 0:
        tmp_num = ""
    else:
        tmp_num = f" ({tmp_num})"
    new_name = f"{e_n_l.replace(last_str, '')}{post_fix}{tmp_num}"
    return new_name

post_fix="_Copy"
name="Exp123_Copy (2)"
exists_name_list = [
    "Exp123_Copy (20)",
    "Exp123_Copy (3)",
    "Exp123_Copy (2)",
    "Exp123_Copy (19)",
    "Exp123_Copy (6)",
    "Exp345",
    "Exp123",
    "Exp123_Copy (1005)",
    "Exp123_Copy (6)",
    "Exp345_Copy (60)",
]
print(get_new_copy_name(name, exists_name_list, post_fix))

Output: Exp123_Copy (1006)

Related