虽然官方的FILE插件提供了一些基本功能,但功能不多。
你并不需要理解什么是fso模版,什么是vbs语句,直接套用下面的语句吧!
如果你看不懂,请参考下面的例子!
*** 作同一个文件红色部分必须相同
请认真看括号内的解释内容
定义一个名为fso的关系文件问题的对象,对于一个文件的 *** 作只需写一次,此句必须写
VBS Set fso = CreateObject("Scripting.FileSystemObject")
0.判断一个文件或文件夹是否存在(yn是返回值,文件存在返回1,不存在返回0)
VBS yn=fso.FileExists(判断文件的目录)
1.创建一个文件(蓝字定义该文本文件是否可以被下次写入覆盖,省略默认为ture)
VBS set ttfile=fso.createtextfile(创建的文件目录,ture|false)
2.打开一个已存在的文件(蓝字定义文件写入方式,分别为a.只读b.可读写,但每打开一次文件重写c.在文件末尾写)
注意:要想更改打开方式必须关闭文件重新打开
VBS set ttfile=fso.opentextfile(打开文件的目录,forreading|forwriting|forappending)
3.关闭一个打开的文件(红色部分要于已经打开的文件红色部分相同)
VBS ttfile.close
4.读取打开文件的一行并回车(红色部分ttfile要于已经打开的文件红色部分相同)
VBS read=ttfile.ReadLine
5.读取所有文件内容(红色部分ttfile要于已经打开的文件红色部分相同)
VBS read=ttfile.ReadAll
6.写入一行并回车(红色部分要于已经打开的文件红色部分相同)
VBS ttfile.writeline(自己要写入的内容)
7.删除指定文件(若已定义过ttfile则不需要第一句)
VBS set ttfile=fso.GetFile(要删的文件目录)
VBS ttfile.delete
8.判断输入标记是否在末尾(是返回-1,否则返回0)
VBS yn=ttfile.atendofstream
以下是一些文件夹的 *** 作
8.判断是否为根目录(yn是返回值,文件存在返回1,不存在返回0)
VBS yn=fso.IsRootFolder
9.读取文件夹
VBS set ttfile=fso.GetFolder(文件夹目录)
10.创建一个文件夹
VBS set ttfile=fso.creaFolder(创建的文件夹目录)
11.删除指定文件夹(若已定义过ttfile则不需要第一句)
VBS set ttfile=fso.GetFolder(要删的文件目录)
VBS ttfile.deletefolder
下面是其他一些经常用到的文件 *** 作(注意:可用于所有格式的文件。红字是你的上文脚本已经定义过的)
VBS ttfile.size 返回文件大小
VBS ttfile.type 返回文件类型
VBS ttfile.DateCreated 返回文件创建时间
VBS ttfile.DatelastAccessed 返回文件最近访问时间
VBS ttfile.DateLastModified 返回文件最后修改时间
VBS ttfile.name 返回文件名称
VBS ttfile.ShortPath 返回文件短路径名
VBS ttfile.path 返回文件物理地址
VBS Set fso = CreateObject("Scripting.FileSystemObject")
//判断d盘下是否有文件1.txt
VBS pd1=fso.FileExists(d:\1.txt)
if 0=pd1
//没有的话,在d盘下创建一个不可覆盖的文件1.txt
VBS set txtfile=fso.createtextfile("d:\1.txt",false)
//以在末尾写入的方式打开1.txt
VBS set txtfile=fso.opentextfile("d:\1.txt",forappending)
//写入一行“1234567890”
VBS txtfile.writeline("1234567890")
//关闭1.txt
VBS txtfile.close
endif
//以只读方式打开1.txt
VBS set txtfile=fso.opentextfile("d:\1.txt",forreading)
//读取第一行,并将其赋予变量read
VBS read=txtfile.ReadLine
//关闭1.txt
VBS txtfile.close
读第几行可以通过一个for语句来确定,循环部分为读一行
mid("abcdefghijkl",2,8)) //获取该字符串第2到第8个字符
没有完全列出所有函数,简单常用的
我需要在教本里比较两个文本文件(XM文件)的内容是否一致,就偷懒写了个简单的VBS的脚本:
// 比较两个文本文件(文件名)
Public Function CompTwoFiles(FileName1,FileName2)
Const ForReading = 1, ForWriting = 2, ForAppending = 8,vbTextCompare =1
Dim fso1, obj_file1,fso2, obj_file2
Dim FileContents1, FileContents2
Set fso1 = CreateObject("Scripting.FileSystemObject")
Set obj_file1 = fso1.OpenTextFile(FileName1, ForReading)
FileContents1 = obj_file1.ReadAll
Set fso2 = CreateObject("Scripting.FileSystemObject")
Set obj_file2 = fso2.OpenTextFile(FileName2, ForReading)
FileContents2 = obj_file2.ReadAll
obj_file1.Close
obj_file2.Close
CompTwoFiles= StrComp(FileContents1,FileContents2,vbTextCompare)
End Function
一方面直接share给大家,另外有更好的办法做到比较两个文本文件吗?
File in = new File("C:\\input.sql")File out = new File("C:\\out.sql")
BufferedReader reader = null
PrintWriter writer = null
try {
reader = new BufferedReader(new FileReader(in))
writer = new PrintWriter(new FileOutputStream(out))
String text = null
int line = 1
while ((text = reader.readLine()) != null) {
writer.println(line + " " + text)
line++
}
writer.flush()
} catch (FileNotFoundException e) {
e.printStackTrace()
} catch (IOException e) {
e.printStackTrace()
} finally {
if (writer != null) {
writer.close()
}
if (reader != null) {
try {
reader.close()
} catch (IOException e) {
e.printStackTrace()
}
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)