简体   繁体   English

从列表中删除文件后,自动重命名filname无法正常工作

[英]Automatic rename filname not working fine after delete a file from list

I am saving file names in database. 我正在将文件名保存在数据库中。 Before saving I am checking to see if the filename exists in the database already. 保存之前,我正在检查文件名是否已存在于数据库中。 If it does, I want to append a unique ID after the file name, for example (1) or (2) and so on after the file name. 如果是这样,我想在文件名后附加一个唯一ID,例如,在文件名后附加(1)或(2),依此类推。

Everything is working fine, but when I delete a file from the retrieved list and from the database, sometimes a get a duplicate file name. 一切正常,但是当我从检索到的列表和数据库中删除文件时,有时会得到重复的文件名。 My code is: 我的代码是:

var count = dbitems().Count(x.Name == name || x.Name.Contains(name + "("));         
if (count > 0)
{
    name = name + string.Format("({0})", count);
}

Suppose I have saved 4 files with names 假设我保存了4个文件,名称

(1) File   
(2) File(1)    
(3) File(2)    
(4) File(3)    

And from this list I have deleted File(2). 从这个列表中,我删除了File(2)。 When I try to save a file with the name "File" my output is File(3), but this already exists. 当我尝试保存名称为“ File”的文件时,我的输出为File(3),但是该文件已经存在。 The required output should be "File(2)" (as (2) was deleted and this name can be allocated) or "File(4)". 所需的输出应该是“ File(2)”(因为已删除(2),并且可以分配此名称)或“ File(4)”。 I have tried with count + 1 also but this is also creating problem. 我也尝试过用count + 1,但这也造成了问题。

In simple words, I want behavior like windows explorer to create new name that automatically assigns a unique suffix. 简而言之,我希望像Windows Explorer这样的行为创建自动分配唯一后缀的新名称。

You have to do it in a loop: 您必须循环执行:

string origName = name;
int version = 0;
while(dbitems().Count(x.Name == name) > 0)         
{
    name = origName  + string.Format("({0})", ++version);
}

This tries name, name(1), name(2), ... until it finds a free "slot". 这将尝试name,name(1),name(2),...,直到找到可用的“插槽”为止。 It will also work if 如果

name, name(1), name(5), name(6) 名称,名称(1),名称(5),名称(6)

already exist 已经存在

Thank you all guys for your kind help. 谢谢大家的帮助。 I appreciate all of you. 谢谢大家 Here is what i implement as my solution. 这是我实现的解决方案。

      public string UniqueName(string name, int parentId)
        {
           var files = allFiles.Where(x => x.ParentId == parentId).ToList();               

            var newName = name;
            if (files.Count() > 0)
            {
                for (var i = 1; i <= matches; i++)
                {
                    if (files.Any(x => x.Name == newName 
                        newName = string.Format("{0}({1})", name, i);

                    else
                        return newName 

                }

            }

            return newName 
    }

You're going to need to check each candidate name, so maybe something more like: 您将需要检查每个候选人的姓名,所以也许更像是:

int count = 0;
string baseName = name;
while (dbitems().Any(x.Name == name))
{
    count++;
    name = string.Format("{1} ({0})", count, baseName);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM