Board logo

标题: 用VBS写个判断文件和文件夹的子程序 [打印本页]

作者: hackhd     时间: 2008-2-4 15:33    标题: 用VBS写个判断文件和文件夹的子程序
if pd("c:\temp.txt",1) then msgbox ("文件存在") else msgbox ("文件不存在") end if 我想写个这样的子程序来判断文件,可是。。可是我写错了。 function pd(dz,cs) if fso.fileexists(dz) then cs=1 if not fso.fileexists(dz) then cs=2 end function

作者: slore     时间: 2008-2-4 16:25
什么叫函数?函数(funciton)和过程(sub)区别知道麽? If pd("c:\temp.txt") Then MsgBox "文件存在" Else MsgBox "文件不存在" End If Function pd(dz) pd = 0 '或者在下面用else语句 If fso.fileexists(dz) Then pd = 1 End Function

作者: hackhd     时间: 2008-2-4 16:34
想用子程序是想更方便呀。你的语句用PD=1 后面if not fso.fileexists? if fso.folderexists?if not fso.folderexists?怎么处理? if pd("c:\temp.txt",1) then 我在后面加一个参数是想达到一个这样的效果 if pd("c:\temp.txt",1) then '为1就是如果文件存在则,为2就是如果文件不存在则 为3文件夹如果存在则 为4如果文件夹不存在则

作者: slore     时间: 2008-2-4 17:19
多种情况用if或者select判断,你一个if 能 判断麽?

作者: slore     时间: 2008-2-4 17:28
'返回值: ' 0----文件和文件夹都不存在 ' 1----存在文件 ' 2----存在文件夹 Function pd(dz) pd = 0 If fso.FileExists(dz) Then pd = 1 If fso.FolderExists(dz) Then pd = 2 End Function

作者: hackhd     时间: 2008-2-4 22:26
If pd("c:\temp.txt") Then '比如:如果我是要判断文件,实际情况下c盘下面没有temp.txt 但是有temp.txt文件夹。那么程序就会给我一个错误的判断,因为我是判断文件,程序检测到与条件名相同文件夹也会给我存在的报告。 我就说这代码看上去总离的意思还差点。自己改了下这样就行了。 Function pd(dz,cs) if cs=1 then If fso.FileExists(dz) Then pd=1 if cs=2 then If fso.FolderExists(dz) Then pd=2 if cs=3 then If not fso.FileExists(dz) Then pd=3 if cs=4 then If not fso.FolderExists(dz) Then pd=4 End Function

作者: slore     时间: 2008-2-4 23:23
用我的函数…… If pd("c:\temp.txt") = 1 Then MsgBox "文件存在" If pd("c:\temp.txt") = 2 Then MsgBox "文件夹存在"

作者: hackhd     时间: 2008-2-4 23:47
刚试了下,你上面的模式。貌似用在我的程序里不行。程序不能正常执行了。

作者: slore     时间: 2008-2-4 23:52
你确定函数用的是我的? 你的反正怎么看怎么怪。。。 if pd("c:\temp.txt",3) then '真了反而是文件不存在。就像速度不用s/m做单位一 '样。这样的话越小速度越快……同样是对的,但是和人们日常的习惯常识不相符。

作者: hackhd     时间: 2008-2-4 23:58
对。我就是用的 If pd("c:\temp.txt") = 1 Then MsgBox "文件存在" If pd("c:\temp.txt") = 2 Then MsgBox "文件夹存在" if pd("c:\temp.txt",3) then 我的意思很简单。为3就调用第三句来判断,第三句是什么。第三句是如果文件不存在则... Function pd(dz,cs) if cs=1 then If fso.FileExists(dz) Then pd=1 if cs=2 then If fso.FolderExists(dz) Then pd=2 if cs=3 then If not fso.FileExists(dz) Then pd=3 if cs=4 then If not fso.FolderExists(dz) Then pd=4 End Function [ Last edited by hackhd on 2008-2-5 at 12:02 AM ]

作者: slore     时间: 2008-2-5 00:33
你的函数我能看懂。。。我说那个意思,如果你用3 如果函数返回真(3)说明文件不存在……真了反而不存在。如果假了说明存在。 你的cs建议用select语句,不然你每次过程都4个判断下。。。(虽然时间很快,但是代码最好不要这样写)

作者: hackhd     时间: 2008-2-5 05:00
恩。好的。