![]() |
中国DOS联盟-- 联合DOS 推动DOS 发展DOS --联盟域名:www.cn-dos.net 论坛域名:www.cn-dos.net/forum |
| 游客 | 登录 | 注册 | 会员 | 搜索 | 中国DOS联盟 |
|
中国DOS联盟论坛 现在时间是 2026-08-07 14:21 |
共 48,040 主题排行 / 350,125 发帖 / 今日 0 篇 / 48,252 会员排行 |
| DOS批处理 & 脚本技术(批处理室) » 用VBS写个判断文件和文件夹的子程序 |
| 可打印版本 1,130 / 11 |
| 第1楼 hackhd | 发表于 2008-02-04 15:33 |
| 中级用户 发帖 102 积分 231 | |
|
用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 |
|
| 第2楼 slore | 发表于 2008-02-04 16:25 |
| 铂金会员 发帖 2,478 积分 5,212 | |
|
什么叫函数?函数(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 |
|
| 第3楼 hackhd | 发表于 2008-02-04 16:34 |
| 中级用户 发帖 102 积分 231 | |
|
想用子程序是想更方便呀。你的语句用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如果文件夹不存在则 |
|
| 第4楼 slore | 发表于 2008-02-04 17:19 |
| 铂金会员 发帖 2,478 积分 5,212 | |
|
多种情况用if或者select判断,你一个if 能 判断麽?
|
|
| 第5楼 slore | 发表于 2008-02-04 17:28 |
| 铂金会员 发帖 2,478 积分 5,212 | |
|
'返回值:
' 0----文件和文件夹都不存在 ' 1----存在文件 ' 2----存在文件夹 Function pd(dz) pd = 0 If fso.FileExists(dz) Then pd = 1 If fso.FolderExists(dz) Then pd = 2 End Function |
|
| 第6楼 hackhd | 发表于 2008-02-04 22:26 |
| 中级用户 发帖 102 积分 231 | |
|
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 |
|
| 第7楼 slore | 发表于 2008-02-04 23:23 |
| 铂金会员 发帖 2,478 积分 5,212 | |
|
用我的函数……
If pd("c:\temp.txt") = 1 Then MsgBox "文件存在" If pd("c:\temp.txt") = 2 Then MsgBox "文件夹存在" |
|
| 第8楼 hackhd | 发表于 2008-02-04 23:47 |
| 中级用户 发帖 102 积分 231 | |
|
刚试了下,你上面的模式。貌似用在我的程序里不行。程序不能正常执行了。
|
|
| 第9楼 slore | 发表于 2008-02-04 23:52 |
| 铂金会员 发帖 2,478 积分 5,212 | |
|
你确定函数用的是我的?
你的反正怎么看怎么怪。。。 if pd("c:\temp.txt",3) then '真了反而是文件不存在。就像速度不用s/m做单位一 '样。这样的话越小速度越快……同样是对的,但是和人们日常的习惯常识不相符。 |
|
| 第10楼 hackhd | 发表于 2008-02-04 23:58 |
| 中级用户 发帖 102 积分 231 | |
|
对。我就是用的
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 ] |
|
| 第11楼 slore | 发表于 2008-02-05 00:33 |
| 铂金会员 发帖 2,478 积分 5,212 | |
|
你的函数我能看懂。。。我说那个意思,如果你用3
如果函数返回真(3)说明文件不存在……真了反而不存在。如果假了说明存在。 你的cs建议用select语句,不然你每次过程都4个判断下。。。(虽然时间很快,但是代码最好不要这样写) |
|
| 第12楼 hackhd | 发表于 2008-02-05 05:00 |
| 中级用户 发帖 102 积分 231 | |
|
恩。好的。
|
|
|
[ 联系联盟系统管理团队 -
中国DOS联盟 -
标准版 ] Sponsored by ifanr Inc | © 2001–2023 |