文件夹批量建立助手
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
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
相关文章推荐
- 批量建立文件夹:用EXCEL 批量提取当前文件夹内文件名:txt
- 批量建立文件夹,为Serv_U批量创建用户
- 批处理_批量建立文件夹
- 为用户批量建立私人文件夹并设定权限
- 为用户批量建立私人文件夹并设定权限的批处理
- 批量根据文件建立文件夹并将文件移入文件夹
- Excel VBA - 遍历某个文件夹中文件、文件夹及批量建立txt
- Excel VBA - 遍历某个文件夹中文件、文件夹及批量建立txt
- Excel VBA - 遍历某个文件夹中文件、文件夹及批量建立txt(转载)
- 利用DIR命令批量输出文件夹名或文件名的代码
- 将指定文件夹下结构相同的EXCEL文件(支持2003和2007版本)批量导入到SQL数据库中指定的表
- OpenCV 实践程序13——实现文件夹下图像批量归一化
- 用Java来批量修改文件夹下的文件名
- Python 批量删除文件夹下面指定类型的所有文件
- linux 批量指定文件夹压缩,排除某个不压缩,查看文件大小,查看磁盘空间
- 根据pix飞控log文件和photo文件夹建立pos文件,提取pos数据小程序
- Python 入门学习(贰)文件/文件夹正则表达式批量重命名工具
- 解决Eclipse建立Maven项目后无法建立src/main/java资源文件夹的办法
- 批量修改文件夹属性
- linux下批量修改目录下所有文件夹、文件名称及文件内容