我有几个由文件夹路径组成的文件,看起来像
//Fs1/userA/
//Fs1/userA/documents/
//Fs1/userB/
//Fs2/userC/documents
我想分析哪些文件夹有x个直接子文件夹。例如。 userD直接在其下有120个子文件夹,不包括更深层的文件夹。
我收集了大量此类文件,其中一些文件包含几百k行。
我曾想过使用基数树,但如果我必须遍历所有子项以查找直接子文件夹的数量,它就不会执行。
还考虑将其放入数据库服务器进行查询,这可能更容易实现,但我想尝试使用.Net进行分析而不使用sql server。有什么帮助吗?
答案 0 :(得分:0)
我接受了我的xml项目并删除了xml。现在我得到总数。也许这对你有用。代码检查所有文件的文件夹和子文件夹,当文件大小或数量超过限制时,将结果放入树视图中。
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
namespace CheckFileSizes
{
public partial class Form1 : Form
{
const int FILE_SIZE_CHECK = 10000000;
const int FILE_COUNT_CHECK = 1000;
public Form1()
{
InitializeComponent();
folderBrowserDialog1.SelectedPath = @"c:\temp";
textBoxTotalFolderSizeMin.Text = FILE_SIZE_CHECK.ToString();
textBoxNumberOfFilesMin.Text = FILE_COUNT_CHECK.ToString();
}
private void buttonBrowseForFolder_Click(object sender, EventArgs e)
{
folderBrowserDialog1.ShowDialog();
textBoxFolderName.Text = folderBrowserDialog1.SelectedPath;
}
private void buttonMakeTree_Click(object sender, EventArgs e)
{
if (Directory.Exists(textBoxFolderName.Text))
{
MyDirectory root = MyDirectory.root;
TreeNode rootNode = MakeTreeRecursive(textBoxFolderName.Text, root);
textBoxTotalNumberOfFiles.Text = root.totalNumberOfFiles.ToString();
textBoxTotalSize.Text = ((long)root.totalSize).ToString();
if (rootNode == null)
{
string rootNodeText = string.Format("Folder: '{0}', Number of Files: '{1}', File Size: '{2}'",
textBoxFolderName.Text,
textBoxTotalNumberOfFiles.Text,
textBoxTotalSize.Text
);
rootNode = new TreeNode(rootNodeText);
}
treeView1.Nodes.Add(rootNode);
treeView1.ExpandAll();
}
}
private TreeNode MakeTreeRecursive(string folder, MyDirectory myDirectory)
{
TreeNode node = null;
myDirectory.name = folder.Substring(folder.LastIndexOf("\\") + 1);
DirectoryInfo dInfo = new DirectoryInfo(folder);
myDirectory.numberOfFiles = 0;
myDirectory.size = 0.0f;
foreach (FileInfo fInfo in dInfo.GetFiles())
{
try
{
float fSize = fInfo.Length;
myDirectory.size += fSize;
myDirectory.numberOfFiles += 1;
}
catch (Exception e)
{
Console.WriteLine("Error : CAnnot Access File '{0}'", fInfo.Name);
}
}
myDirectory.totalSize = myDirectory.size;
myDirectory.totalNumberOfFiles = myDirectory.numberOfFiles;
foreach (string subFolder in Directory.GetDirectories(folder))
{
if (myDirectory.children == null) myDirectory.children = new List<MyDirectory>();
MyDirectory childDirectory = new MyDirectory();
myDirectory.children.Add(childDirectory);
TreeNode childNode = MakeTreeRecursive(subFolder, childDirectory);
if (childNode != null)
{
if (node == null)
{
node = new TreeNode();
}
node.Nodes.Add(childNode);
}
myDirectory.totalSize += childDirectory.totalSize;
myDirectory.totalNumberOfFiles += childDirectory.totalNumberOfFiles;
}
if ((myDirectory.totalNumberOfFiles >= long.Parse(textBoxNumberOfFilesMin.Text)) || myDirectory.totalSize >= float.Parse(textBoxTotalFolderSizeMin.Text))
{
if (node == null)
{
node = new TreeNode();
}
string childNodeText = string.Format("Folder: '{0}', Number of Files: '{1}', File Size: '{2}'",
folder,
myDirectory.totalNumberOfFiles,
myDirectory.totalSize
);
node.Text = childNodeText;
}
return node;
}
}
public class MyDirectory
{
public static MyDirectory root = new MyDirectory();
public List<MyDirectory> children { get; set; }
public string name { get; set; }
public long totalNumberOfFiles = 0;
public int numberOfFiles = 0;
public float size = 0.0f;
public float totalSize = 0.0f;
}
}