VBS递归遍历文件和dictionary使用

简介:

最近在做一个VBS脚本过滤文件的操作,发现确实遇到了不少问题。就简单的遍历文件操作是很简单的,相信大家也都会。但是却在过滤文件的操作中出现了不少的问题。

1,将过滤到的文件名称存放在dictionary中,发现在递归中dictionary每次都会再次生成一个dictionary对象,所以始终dictionary的数据是变动的,而且是最后一次操作文件夹或者是文件的数据集。过滤文件失败!

2,采用function的返回值的时候,发现function在遇到递归的操作的时候function返回的值并不能保存起来,也就是vbs的变量都是临时的不是保存在内存中的,所以function在递归多次操作的始终返回的是empty,糟糕之极!过滤文件返回字符串失败!

3,for循环中的返回值,需要对于要返回的值赋值给另一个变量,然后将function的返回值对应于该变量;

4,直接跳出当前的function的方法为wscript.quit,而对于exit function是不起作用的。

最终不能再想到别的解决方法了,于是就采用了下面的一个不愿意的方式。

将所有的过滤的文件都保存在一个永久储存的地方,就是文件中。

以后如果想要获取这些过滤到的文件直接读取文件中的过滤结果即可。

以下是我的一个设计方案,遍及指定的文件夹,过滤掉文件名是某某的文件。

'*******************************************************************************************
'
'功能:遍历环境变量中预设的数据值路径,然后过滤找到指定的文件名,并存放在text文件中
'参数:datapath表示查找文件的路径,filename表示需要查找的文件名称,文件名支持模糊名称
'返回值:无
'*******************************************************************************************
Function tempFilePath(datapath,filename)
   On error resume next
   Set fso=createobject("scripting.filesystemobject")
   If fso.FolderExists(datapath) Then
       Set firstsub=fso.GetFolder(datapath)
       For each testfolder in firstsub.SubFolders
           strpath=datapath&"\"&testfolder.name
           tempFilePath strpath,filename
       Next
       For each testfile in firstsub.Files
          ' print testfile.name
           If instr(1,testfile.name,filename,1)<>0 Then
                logpath=environment.Value("RunLogs")
                'If not fso.FileExists(logpath&"\FilePaths.txt") Then
                    set logfile=fso.CreateTextFile(logpath&"\FilePaths.txt",true)
                    logfile.WriteLine testfile.path
            else
                'ReportProgress(Text, Title, TimeOut)
                 'ReportProgress "在指定的路径下没有找到你需要的文件"
           End If
       Next
    else
      'ReportProgress(Text, Title, TimeOut)
            ReportProgress "enviroment文件中的测试路径找不到,请确认你的environment文件配置正确!",2
   End If
     logfile.Close
     Set logfile=nothing
     Set firstsub=nothing
    Set fso=nothing
End Function



本文转自hcy's workbench博客园博客,原文链接:http://www.cnblogs.com/alterhu/archive/2012/03/25/2416743.html ,如需转载请自行联系原作者。

目录
相关文章
|
Python
python遍历 truple list dictionary的几种方法
def TestDic1(): dict2 ={'aa':222,11:222} for val in dict2: print val def TestDic2(): dict2 ={'aa':222,11:222} for (key,val) in dict2.
842 0
|
程序员 C++ Python
代码练习 简单文件读写 字符串 数组的处理 list Dictionary
func.py # -*- coding: GBK -*- """ 在Python中默认是 Ansi编码格式 要使用中文需要 明确指定编码 数组分为动态数组和静态数组 动态数组可以动态添加 元素 静态数组不能改变 数据结构 def 定义function def Func: ...
870 0
(小常识)Dictionary的遍历
Dictionary objDictionary = new Dictionary();             objDictionary.Add(1, "张三");             objDictionary.
889 0
[AS3] 问个很囧的问题: 如何遍历Dictionary?
可以使用 for...in 循环或 for each...in 循环来遍历 Dictionary 对象的内容。   for...in 循环用于基于键进行遍历; 而 for each...in   循环用于基于与每个键关联的值进行遍历
遍历并修改dictionary的值
var dictionary = clb_Select.Items.Cast().ToDictionary(item => item.ToString(), item => false); foreach (var checkedItem in clb_Select.
731 0
Dictionary的遍历和修改
/// /// 初始化一个Dic /// public static void mainTest() { Dictionary dic = new Dictionary(); dic.
849 0
.net 中Dictionary的遍历
            Dictionary dict = new Dictionary();             dict.Add(200, 1);             dict.Add(100, 2);             dict.
588 0
|
2月前
|
存储 Swift
在Swift编程语言中,字典(Dictionary)
在Swift编程语言中,字典(Dictionary)
48 3