- 浏览: 64799 次
- 性别:
- 来自: 杭州
最新评论
-
meimei_123abc:
你好,可以把这个完整的代码给我发一个学习下吗,刚开始接触这一块 ...
在JNI中调用本地带结构体参数的函数 -
yangdong:
sorry,之后一直没再碰过 JNI,没法再写了。
在JNI中调用本地带结构体参数的函数 -
ihopethatwell:
楼主,能写一个传递数组的结构体?
在JNI中调用本地带结构体参数的函数 -
yangdong:
谢谢。我只是追求形似,所以找不到对应的写法。
Stream in Clojure -
jamesqiu:
Clojure的lazy-cat和Scala的Stream实现 ...
Stream in Clojure
我经常会遇到进行批量文件修改的情况。Windows 脚本我十分不精通,以前都是靠现写一个 C# 程序。遇到 Ruby 后,我十分喜欢它语法上的灵活性。(虽然我认为太灵活不一定好)而且它还是一种脚本语言,很方便。考虑到我所遇到的情况,我想写一个类来支持对文件名(文件夹名)或全路径名进行正则表达式匹配。同时也支持反向过滤。即,保留那些没有匹配上的文件或文件夹。
举一个例子。比如用备份工具备份“我的文档”再还原后,很多隐藏文件现在都会显现出来。比如 thumbs.db 和 picasa.ini(因为我用 Google 的 Picasa)。我想把 picasa.ini 重新隐藏起来并删除 thumbs.db。可以这样写:
我认为这样还算是比较容易的吧 如果只是想遍历一下,更简单,直接
FileTree的源代码如下:
有点长,不过一半是注释。如果有的地方的意图看不明白,可以参考下面的测试代码。测试使用与测试代码文件同级的一个“test_folder”文件夹。它的目录结构如下:
C:/netbeans-proj/file_tree/test/test_folder/test
C:/netbeans-proj/file_tree/test/test_folder/test/readme.txt
C:/netbeans-proj/file_tree/test/test_folder/test/src
C:/netbeans-proj/file_tree/test/test_folder/test/src/Assert.java
C:/netbeans-proj/file_tree/test/test_folder/test/src/Entry.java
如果要运行此测试,要先把这个文件结构构造出来才可以。也请保证“test_folder”的上级目录中没有包含src、assert、entry、readme这几个字符串的。不然,测试可能会出问题。
举一个例子。比如用备份工具备份“我的文档”再还原后,很多隐藏文件现在都会显现出来。比如 thumbs.db 和 picasa.ini(因为我用 Google 的 Picasa)。我想把 picasa.ini 重新隐藏起来并删除 thumbs.db。可以这样写:
ftree = FileTree.new("c:\documents and settings\username\my documents") free.traverse( [ /^picasa.ini$/i, /^thumbs.db$/i ], { :entry_type => :file :for_basename_only => true } ) do |file| if file =~ /picasa/i `attrib +h #{file}` else `attrib -s -h -r #{file}` `del #{file}` end end
我认为这样还算是比较容易的吧 如果只是想遍历一下,更简单,直接
FileTree.new("c:\dummy").traverse /file_name_pattern/
FileTree的源代码如下:
# author: Yang Dong # date: 2008-6-15 # # this class is designed to convient the traverse of file trees. you can # just output the whole structure or you can specify some regular expressions # to filter the unwanted files or directories, and customize the actions # against them, plus some additional controls. # # examples to use (based on windows os): # 1) say, you want to see the whole file structure. just write: # FileTree.new("c:\dummy_directory").traverse # # 2) say, you want to hide all the picasa.ini files, write: # ftree = FileTree.new("c:\dummy_dir") # ftree.traverse( # /^picasa.ini$/i, # { # :entry_type => :file, # :for_basename_only => true # } # ) do |file| # `attrib \"#{file}\" -s -h -r` # end class FileTree require "pathname" def initialize(dir) @dir = dir.chomp.gsub(/\\/, '/') pn = Pathname.new(@dir) pn.cleanpath raise "no such directory" unless pn.exist? && pn.directory? end # traverse the given directory. use filter_patterns to specify # what kind of file name you would like to match. attach a # block if you want to give some actions against the matched # files other than just put them out on the standard out. # the block takes one argument indicating the absolute file path # of the matched one. # # the filter_patterns is an array containing regular expression # objects. # # the options give some additional control over filtering. # for details about filter_patterns and options, refer to the # filter method. # # caution: the patterns and actions will not be applied to the root folder # given. def traverse(filter_patterns = nil, options = nil, &block) trav @dir, filter_patterns, options, &block end private def trav(dir, filter_patterns = nil, options = nil, &block) pn = Pathname.new(dir) children = pn.children children.each do |child| if filter(child, filter_patterns, options) if block block.call child.realpath.to_s else puts child.realpath.to_s end end if child.exist? and child.directory? trav child.realpath.to_s, filter_patterns, options, &block end end end # filters the given entry. if entry passed the filter, returns true. # otherwise false. # # the filter_patterns is an array containing regular expression # objects. # # options is a hash which supports the following options: # entry_type: # use this to specify to filter file or directory. if you only want # to do something with files, then use { :entry_type => :file }. # otherwise, use { :entry_type => :dir }. default is nil, which means # either will be okay. # exclude_matched: # specify true to indicate that the matched file entries (including # directories) will not pass the filter. this can be used when you want # to do something with most of the entries in your folder but with some # exceptions. default is set to false. # for_basename_only: # indicates whether the regular expression pattern will be comparing with # the directory or file name only. the default is false, which means not # only the name will be compared, but also the whole path will be # compared. # def filter(entry, filter_patterns = nil, options = nil) # defines a series of default options. options = {} if options.nil? if options[:entry_type] == :file return false unless entry.file? elsif options[:entry_type] == :dir return false unless entry.directory? end filter_patterns = [ // ] if filter_patterns == nil unless filter_patterns.is_a?(Array) filter_patterns = ([] << filter_patterns) end filter_patterns.each do |filter_pattern| if options[:exclude_matched] if options[:for_basename_only] return false if entry.basename.to_s =~ filter_pattern else return false if entry.realpath.to_s =~ filter_pattern end else if options[:for_basename_only] return true if entry.basename.to_s =~ filter_pattern else return true if entry.realpath.to_s =~ filter_pattern end end end if options[:exclude_matched] return true else return false end end end
有点长,不过一半是注释。如果有的地方的意图看不明白,可以参考下面的测试代码。测试使用与测试代码文件同级的一个“test_folder”文件夹。它的目录结构如下:
C:/netbeans-proj/file_tree/test/test_folder/test
C:/netbeans-proj/file_tree/test/test_folder/test/readme.txt
C:/netbeans-proj/file_tree/test/test_folder/test/src
C:/netbeans-proj/file_tree/test/test_folder/test/src/Assert.java
C:/netbeans-proj/file_tree/test/test_folder/test/src/Entry.java
如果要运行此测试,要先把这个文件结构构造出来才可以。也请保证“test_folder”的上级目录中没有包含src、assert、entry、readme这几个字符串的。不然,测试可能会出问题。
require 'test/unit' require "file_tree" class FileTreeTest < Test::Unit::TestCase def setup @root = "#{File.dirname(__FILE__).gsub(/\\/, "/")}/test_folder" @file_tree = FileTree.new(@root) end def test_simple_traverse output = "" @file_tree.traverse do |entry| output += "#{entry}\n" end expected_output = <<TAG #{@root}/test #{@root}/test/readme.txt #{@root}/test/src #{@root}/test/src/Assert.java #{@root}/test/src/Entry.java TAG assert_equal expected_output, output end def test_entry_type output = "" @file_tree.traverse(nil, :entry_type => :file) do |file| output += "#{file}\n" end expected_output = <<TAG #{@root}/test/readme.txt #{@root}/test/src/Assert.java #{@root}/test/src/Entry.java TAG assert_equal expected_output, output ########################################## output = "" @file_tree.traverse(nil, :entry_type => :dir) do |dir| output += "#{dir}\n" end expected_output = <<TAG #{@root}/test #{@root}/test/src TAG assert_equal expected_output, output end def test_exclude_matched output = "" @file_tree.traverse(nil, :exclude_matched => true) do |entry| output += "#{entry}\n" end assert_equal "", output ############################################### output = "" @file_tree.traverse(/src/, :exclude_matched => true) do |entry| output += "#{entry}\n" end expected_output = <<TAG #{@root}/test #{@root}/test/readme.txt TAG assert_equal expected_output, output end def test_for_basename_only output = "" @file_tree.traverse(/src/, :for_basename_only => true) do |entry| output += "#{entry}\n" end expected_output = <<TAG #{@root}/test/src TAG assert_equal expected_output, output end def test_multiple_patterns output = "" @file_tree.traverse [ /assert/i, /readme/i ] do |entry| output += "#{entry}\n" end expected_output = <<TAG #{@root}/test/readme.txt #{@root}/test/src/Assert.java TAG assert_equal expected_output, output end def test_complicated_traverse output = "" @file_tree.traverse( [ /assert/i, /readme/i ], { :entry_type => :file, :exclude_matched => true, :for_basename_only => true } ) do |file| output += "#{file}\n" end expected_output = <<TAG #{@root}/test/src/Entry.java TAG assert_equal expected_output, output end end
评论
2 楼
yangdong
2008-06-16
非常感谢!这个就当是练习了……
1 楼
QuakeWang
2008-06-16
ruby自带find功能就是用来做路径遍历的,不需要自己写
# find.rb: the Find module for processing all files under a given directory.
# The +Find+ module supports the top-down traversal of a set of file paths.
引用
# find.rb: the Find module for processing all files under a given directory.
# The +Find+ module supports the top-down traversal of a set of file paths.
发表评论
-
TimSort 中的核心过程
2011-09-11 20:19 4234TimSort 是 Python 中 list.sor ... -
模拟实现一个 Ruby 的 TCO
2011-05-15 00:24 984今天搜了一下,发现 ruby 只有一部分 VM 实现支持 TC ... -
解决Ruby脚本在Windows命令行乱码
2011-05-08 16:08 1283如果把 ruby 当脚本使用,在 windows 命令行下面输 ... -
完全由不变体写出来的 Register Machine Simulator
2010-11-19 09:41 1301SICP 第二版 5.2 节提到的 Register Mach ... -
在 Clojure 中处理异常
2010-11-18 23:54 1477Update: As of Clojure 1.3, Cloj ... -
Named arguments in Clojure
2010-11-18 23:54 1044Clojure doesn't provide direct ... -
快速排序
2010-11-18 23:51 1141看了《The Joy of Clojure》的快速排序,觉得比 ... -
求对数
2010-11-18 23:46 1078下面是求以 1.12 为底,2.7 的对数。本方法极其低效,仅 ... -
Stream in Clojure
2010-11-18 23:40 1319(define fibs (stream-cons ... -
[Groovy] this 语义的陷阱
2009-07-30 21:46 916groovy 中 this 的语义有问题。下面的代码会报异常, ... -
两个 JavaScript 面向对象的方法
2008-10-02 07:06 1329准备工作 为了演示或者您试验,请先准备好下面的 HTML 模 ... -
firstChild 把我玩了
2008-07-31 23:23 1296加班两三个小时,就是因为被一个 DWR 的函数给玩了。dwr. ...
相关推荐
实现文件树遍历程序myfind,参照UNIX环境高级编程中的例子
java全硬盘文件遍历,添加到树中,在面板中显示,没有事件处理
将Dir.class放在任意需要遍历的文件目录,直接命令窗口运行java Dir生成Dir.txt文件
VC 文件目录遍历生成树菜菜单,生成目录树的VC 源码范例,自动读取指定文件夹下的所有目录和文件,并生成Tree目录树结构。PS注:示例程序读取的是“成绩表”文件夹下的目录和文件,因此在测试时候要把生成的exe从...
目的: 掌握与文件和目录树有关的系统调用和库函数。 要求: 1、编写程序myfind ...3、遍历目录树时,访问结点(目录项)的具体操作应当由遍历函数dopath携带的函数指针参数决定。这样程序的结构清晰,可扩充性好。
遍历文件夹并建成目录树__遍历文件夹并建成目录树
利用栈实现非遍历文件目录树
用java实现遍历文件夹中文件并且以树形结构输出
用c#实现的磁盘文件遍历程序,输入路径后可以遍历出整个该路径下的所有文件,更适合文件的查找!
函数填空:层次遍历多元树(在文件tree.cpp中3个空)、先根遍历、后根遍历的递归函数(在文件tree.h中2个空);
利用VC++6.0树形控件开发的遍历WINDOWS文件
本代码包括三部分的内容,其一是Java文件遍历,其二是Java的非递归前序,中序以及后序遍历,最后是前后序编码的生成问题。
使用libxml2库对xml文件进行xml遍历, 使用递归算法和非递归算法两种方法实现xml树的遍历。
遍历文件夹自动生成目录树
java设计模式【之】组合模式【源码】【场景:遍历目录树】 * 组合模式 * 将对象组合成树结构,表示 “部分与整体” 的关系 * 要求 部分与整体,具备相同的父类 * * 代码实现 * 输出当前目录下,全部目录层级...
遍历整个目录,然后根据遍历结果生成树.是学习文件夹操作及树控件使用的很好的例子
一个类,可以遍历一个目录,将该目录下所有文件以及子目录及其文件都遍历,生成一个层次分明的数组,还可以将遍历的结果生成一个树状的字符串,直接echo到浏览器。 |-|a.txt |-|b.txt |-|c目录 |---|d.txt |---|c1...
C#遍历系统文件夹(TreeView控件),递归方式遍历 顺便使用树控件
VC递归遍历指定文件夹下的所有子文件夹与文件,并生成树结构的示例代码。
易语言树形框遍历文件夹内文件源码例程演示了树形框和拖放组件的使用。 点评:源码演示了树形框和拖放组件的使用。