您的位置:首页 > 其它

文件夹批量建立助手

2011-08-29 21:34 453 查看
陆陆续续几年的时间,出来这个版本。

vs2010&c#

1.可以生成blumind软件的bmd格式图形化显示(谢谢blumind作者给我们提供了这么好用的小软件)

2.可以直接利用blumind绘制目录结构,然后软件批量建立

3.或者将已有的文件夹结构保存成bmd格式。(当然可以稍加修改变成其他格式)

4.可以利用xml文件保存自己认为好的结构

5.自己内置一套描述语法

6.利用多线程避免UI无响应

7.支持拖放

说了这么多,说简单点软件在对硬盘进行规划、整理的时候非常有用,

可以非常方便的实现大规模文件夹相关操作,

比如安排调整电子书的目录。













对于上万个文件夹的操作也毫不畏惧哦。

现在将核心类分享给大家:

C#语言: 文件夹助手
/*

* Directories create helper class.

* This class can copy directories structures to self grammer,

* also can resolve bmd file or create bmd file(created by blumind).

* vs2010 & c#

* collated and improved by winxos At 2011-08-22

*/

using System;

using System.Collections.Generic;

using System.Text;

using System.Xml; //xml resolve

using System.IO; //directory

namespace DirCreater

{

public class DirHelper

{

private string _path;

private string _context;

public string Context

{

set { _context = value; }

}

public string Expression

{

get { return _context; }

}

public DirHelper()

{ }

public DirHelper(string s)

{

_path = s;

}

//create the right brankets when leave

private string addk(int n)

{

string ret = ":";

for (int i = 0; i < n; i++)

{

ret += "):";

}

return ret;

}

//return the related path name.

public string ChopParent(string str)

{

int pos = str.LastIndexOf("\\") + 1;

if (pos < str.Length) //if had

{

str = str.Substring(pos, str.Length - pos);

}

else //not found

{

str = str.Substring(0, str.Length - 2);

}

return str;

}

//return the directories describe.

public string GetDirStruct(string dirName)

{

string ret="";

_getDirStruct(dirName, ref ret);

ret = ChopParent(dirName) + ":"+ret;

_context = ret;

return ret;

}

//create the structure of directories.

private bool _getDirStruct(string dirName, ref string dirstr)

{

try

{

if (dirName.StartsWith("$")) return false;

string[] d = Directory.GetDirectories(dirName);

if (d.Length > 0) //include sub directories

{

dirstr += "(:";

foreach (string s in d)

{

//skip the system folders

if (s.IndexOf("RECYCLE") > 0 || s.IndexOf("System Volume Information") > 0)

{

continue;

}

dirstr += ChopParent(s) + ":";

_getDirStruct(s, ref dirstr);

}

dirstr += "):";

}

return true;

}

catch { }

return false;

}

//resolve bmd file(created by blumind).

public string ResolveBmd(string s)

{

string ans = "";

try

{

XmlDocument xd = new XmlDocument();

xd.Load(s);

XmlNodeReader xnr = new XmlNodeReader(xd);

while (xnr.Name != "node") xnr.Read();

ans = xnr.GetAttribute("text");

int olddepth = xnr.Depth;

int firstdepth, lastdepth;

firstdepth = lastdepth = olddepth;

while (xnr.Read())

{

if (xnr.NodeType == XmlNodeType.Element && xnr.Name == "node")

{

if (xnr.Depth > olddepth)

{

ans += ":(:" + xnr.GetAttribute("text");

}

else if (xnr.Depth == olddepth)

{

ans += ":" + xnr.GetAttribute("text");

}

else

{

ans += addk(olddepth - xnr.Depth-1) + xnr.GetAttribute("text");

}

olddepth = xnr.Depth;

lastdepth = xnr.Depth;

}

}

ans += addk(lastdepth - firstdepth-1);

}

catch { }

_context = ans;

return ans;

}

//Make the directories.

public void CreateDirs(string rootPath)

{

try

{

Directory.SetCurrentDirectory(rootPath);

string[] cmd =_context.Split(':');

for (int i = 0; i < cmd.Length; i++)

{

string tmp = cmd[i];

switch (tmp)

{

case "(":

Directory.SetCurrentDirectory(Directory.GetCurrentDirectory() + "/" + cmd[i - 1]);

break;

case ")":

Directory.SetCurrentDirectory(Directory.GetParent(Directory.GetCurrentDirectory()).FullName);

break;

default:

Directory.CreateDirectory(tmp);

break;

}

}

}

catch (Exception)

{ }

}

//create the core xml for the grammer.

private string createbmdnodes()

{

string ret = "";

if (_context != "")

{

string[] cmd = _context.Split(':');

for (int i = 0; i < cmd.Length - 1; i++)

{

if (cmd[i + 1] == "(")

ret += string.Format("<node text=\"{0}\">", cmd[i]);

else

{

if (cmd[i] == "(") ret += "<nodes>";

else if (cmd[i] == ")") ret += "</nodes></node>";

else if (cmd[i] != "")

{

ret += string.Format("<node text=\"{0}\"/>", cmd[i]);

}

}

}

}

return ret;

}

public void CreateBmd(string name)

{

try

{

//create the bmd raw text.

string str = string.Format(File.ReadAllText("template.bmd"), createbmdnodes());

XmlDocument xb = new XmlDocument();

str = str.Replace("&", "&"); //avoid EntityName error

xb.LoadXml(str);

StringWriter sw = new StringWriter();

using (XmlTextWriter wt = new XmlTextWriter(sw))

{

//format the bmd file.

wt.Indentation = 2;

wt.Formatting = Formatting.Indented;

xb.WriteTo(wt);

wt.Close();

}

File.WriteAllText(name, sw.ToString());

}

catch (Exception ex)

{

System.Windows.Forms.MessageBox.Show(ex.Message);

}

}

}

}

本文出自 “winxosのcoding world” 博客,请务必保留此出处http://winxos.blog.51cto.com/1671107/652373
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: