gpt4 book ai didi

c# - 在我的输出中看到重复项。为什么?

转载 作者:太空宇宙 更新时间:2023-11-03 20:40:09 25 4
gpt4 key购买 nike

我的应用程序的输出生成了重复的文件名...我不是 100% 确定这是为什么。

我的应用程序通过在文件名中查找正则表达式模式来“清理”文件名。如果没有,它会将其转储到“正常”列表中并忽略它。

这是我用来显示输出的代码:[这一直向我显示重复的文件名!!]

public partial class DriveRecursion_Results : Form
{
List<string> paths = new List<string>();


public DriveRecursion_Results()
{
InitializeComponent();
}




public void DriveRecursion(string retPath)
{
string pattern = (@"[~#&!%+{}]+");

Regex regEx = new Regex(pattern);

string[] fileDrive = Directory.GetFiles(retPath, "*.*", SearchOption.AllDirectories);

List<string> normal = new List<string>();
List<string> fileNameOnlyList = new List<string>();


dataGridView1.Rows.Clear();
try
{
foreach (string fileNames in fileDrive)
{
string strippedFileName = System.IO.Path.GetFileName(fileNames);
fileNameOnlyList.Add(strippedFileName);

foreach (string nameOnly in fileNameOnlyList)
{
if (regEx.IsMatch(strippedFileName))
{
//string fileNameOnly = Path.GetFileName(fileNames);
string pathOnly = Path.GetDirectoryName(fileNames);

DataGridViewRow dgr = new DataGridViewRow();

dgr.CreateCells(dataGridView1);
dgr.Cells[0].Value = pathOnly;
dgr.Cells[1].Value = nameOnly;
dataGridView1.Rows.Add(dgr);
string pathforInvalidName = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(nameOnly), pathOnly);


paths.Add(pathforInvalidName);


}

else
{
normal.Add(strippedFileName);

}
}

}

}
catch (Exception e)
{
StreamWriter sw = new StreamWriter(retPath + "ErrorLog.txt");
sw.Write(e);

}


}


private void button1_Click_1(object sender, EventArgs e)
{

this.Close();

CleanNames clean = new CleanNames();
clean.Sanitizer(paths);
clean.Show();



}

一旦确定了哪些文件需要重命名,它就会清除“脏”名称:

public partial class CleanNames : Form
{
public CleanNames()
{
InitializeComponent();

}

public void Sanitizer(List<string> paths)
{
string regPattern = (@"[~#&!%+{}]+");
string replacement = " ";

Regex regExPattern = new Regex(regPattern);
Regex regExPattern2 = new Regex(@"\s{2,}");

StreamWriter errors = new StreamWriter(@"S:\Test\Errors.txt", true);
var filesCount = new Dictionary<string, int>();


dataGridView1.Rows.Clear();

try
{

foreach (string files2 in paths)
{

string filenameOnly = System.IO.Path.GetFileName(files2);
string pathOnly = System.IO.Path.GetDirectoryName(files2);
string sanitizedFileName = regExPattern.Replace(filenameOnly, replacement);
sanitizedFileName = regExPattern2.Replace(sanitizedFileName, replacement);
string sanitized = System.IO.Path.Combine(pathOnly, sanitizedFileName);


if (!System.IO.File.Exists(sanitizedFileName))
{
DataGridViewRow clean = new DataGridViewRow();
clean.CreateCells(dataGridView1);
clean.Cells[0].Value = pathOnly;
clean.Cells[1].Value = filenameOnly;
clean.Cells[2].Value = sanitizedFileName;
dataGridView1.Rows.Add(clean);

System.IO.File.Move(files2, sanitized);
}

else
{
if (filesCount.ContainsKey(sanitizedFileName))
{
filesCount[sanitized]++;
}
else
{
filesCount.Add(sanitized, 1);
}
string newFileName = String.Format("{0}{1}{2}",
System.IO.Path.GetFileNameWithoutExtension(sanitized),
filesCount[sanitized].ToString(),
System.IO.Path.GetExtension(sanitized));
string newFilePath = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(sanitized), newFileName);
newFileName = regExPattern2.Replace(newFileName, replacement);
System.IO.File.Move(files2, newFilePath);
sanitized = newFileName;

DataGridViewRow clean = new DataGridViewRow();
clean.CreateCells(dataGridView1);
clean.Cells[0].Value = pathOnly;
clean.Cells[1].Value = filenameOnly;
clean.Cells[2].Value = newFileName;

dataGridView1.Rows.Add(clean);

}




}
}
catch (Exception e)
{
errors.Write(e);
}


}

private void SanitizeFileNames_Load(object sender, EventArgs e)
{ }

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}

private void button1_Click(object sender, EventArgs e)
{
Application.Exit();
}

我在这里要做的是只显示需要重命名的文件(不是所有文件)。我想拿走那些脏文件名并用我的第二堂课清理它们。

有人知道为什么我在输出中看到多个相同的文件吗?任何人都知道如何解决这个问题?!?!

最佳答案

我的直接观察是您的 foreach (string nameOnly in fileNameOnlyList) 循环不应该嵌套在它所在的位置。你的逻辑看起来像这样。

For each filename:
Add it to the list.
For *everything in the list*...

因此您将添加一个。然后处理它。然后再添加一个。然后处理两者。然后加。然后处理所有三个。等等。

试试这个。

        foreach (string fileNames in fileDrive)
{
string strippedFileName = System.IO.Path.GetFileName(fileNames);
fileNameOnlyList.Add(strippedFileName);
}

foreach (string strippedFileName in fileNameOnlyList)
{
if (regEx.IsMatch(strippedFileName))
// ...
}

编辑

更好的是,为什么要有两个循环?

        foreach (string fileNames in fileDrive)
{
string strippedFileName = System.IO.Path.GetFileName(fileNames);
fileNameOnlyList.Add(strippedFileName);

if (regEx.IsMatch(strippedFileName))
// ...
}

关于c# - 在我的输出中看到重复项。为什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3237676/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com