vb 多级路径创建,读取文件或文件夹[vb类写法]
2014-12-22 19:24
453 查看
1.创建一个类,命名为:myfile ,复制下面代码进去
2.然后可以这样调用
'判断文件是否存在 Public Function isFileExis(path As String) As Boolean If Dir(path, vbDirectory) = "" Then '不存在 isFileExis = False Else '存在 isFileExis = True End If End Function '作用:根据路径逐级创建文件夹 '参数: path 路径 f:\文件夹a\文件夹b '返回: 无; Public Function createFolder(path As String) 'C:\Users\Administrator\Desktop\类操作 '\\A352337697\d\My Documents Dim arr, a, temPath As String, n arr = Split(path, "\") For a = 0 To UBound(arr) temPath = temPath & arr(a) & "\" If Len(arr(a)) > 0 Then n = n + 1 If n > 1 Then 'MsgBox temPath & "_________" & isFileExis(temPath) If isFileExis(temPath) = False Then MkDir temPath End If End If End If Next a End Function '作用: 写入文件内容,如果文件不存在,则逐级创建出路径,直到创建此路径的文件,再写入 Public Function writeFile(path As String, content As String) 'MsgBox path '//取得文件所在路径 Dim fPath As String fPath = Replace(path, "\" & Split(path, "\")(UBound(Split(path, "\"))), "") '//生成文件夹路径 Call createFolder(fPath) 'MsgBox fPath '//生成文件 '//写入 Open path For Output As #1 Print #1, content; Close #1 End Function '作用: 写入文件内容,如果文件不存在,则逐级创建出路径,直到创建此路径的文件,再写入 Public Function writeFileAppend(path As String, content As String) 'MsgBox path '//取得文件所在路径 Dim fPath As String fPath = Replace(path, "\" & Split(path, "\")(UBound(Split(path, "\"))), "") '//生成文件夹路径 Call createFolder(fPath) 'MsgBox fPath '//生成文件 '//写入 Open path For Append As #1 Print #1, content; Close #1 End Function '作用: 读取文件内容,如果文件不存在,则逐级创建出路径,直到创建此路径的文件,再读取 '参数:path '返回:文件内容; Public Function readFile(path As String) 'MsgBox path '//取得文件所在路径 Dim fPath As String fPath = Replace(path, "\" & Split(path, "\")(UBound(Split(path, "\"))), "") '//生成文件夹路径 Call createFolder(fPath) 'MsgBox fPath '//生成文件 Open path For Output As #1 Print #1, ""; Close #1 '//读取 Open path For Input As #1 readFile = StrConv(InputB$(LOF(1), 1), vbUnicode) Close #1 End Function
2.然后可以这样调用
Private Sub Command1_Click() Dim ff As myfile '搜索'声明类 Set ff = New myfile '实例化..这样就可以使用这个类了 Call ff.writeFileAppend("f:\临时文件夹\真的吗\不要紧吧.txt", "真的吗") '调用这个实例的方法 Set ff = Nothing '释放对象 End Sub
相关文章推荐
- 在指定路径创建文件夹,读取图片文件,缩放图片,判断文件夹是否存在,
- linux 根据文件路径自动创建文件夹
- bat读取文件夹下某一类文件的路径信息表
- 【C#】C#判断文件路径是否存在,不存在则创建文件夹
- Java 在给定路径上创建文件,所在文件夹不存在时,如何正确创建。
- springmvc 上传图片文件和创建图片虚拟路径读取图片
- 读取指定文件夹下的全部文件,可通过正则进行过滤,返回文件路径数组 -- 基于node的一个函数
- 在磁盘指定路径,创建文件夹及文件
- java文件操作 之 创建文件夹路径和新文件
- 常用公共函数库(验证mac地址、判断ip所属网段、创建文件夹、读取文件、获取移动客户端类型等)
- 【Java】读取其下所有文件夹与文件的路径
- C#读取文件夹内文件名和路径并拷贝文件内容
- C# 读取客户端文件路径和文件夹路径
- IOS在Document目录下创建文件夹、保存、读取、以及删除文件
- 用递归方法读取嵌套文件夹下的文件路径
- 在ios中的Document中创建多级文件夹以及文件
- vb 创建多级文件夹
- 在ios中的Document中创建多级文件夹以及文件
- 针对含有多级文件夹时,文件创建
- 在ios中的Document中创建多级文件夹以及文件