PDA

View Full Version : تشخیص وجود فایل



vahid_d_0101
جمعه 26 تیر 1388, 19:17 عصر
من مخیوام بفهمم که یه فایل در آدرس مورد نظر وجو داره یانه قبل این را داخل سایت دیده بودم هرچی سرچ میکنم پیدا نمیکنم
در ضمن نمیخوام اول فایل را باز کنم و ارور داد این بار بفهمم که فایل وجو ندارد
یعنی نمیخوام از دستور on eeror استفاده کنم

HjSoft
جمعه 26 تیر 1388, 19:23 عصر
Dim X$

X$ = Dir$("C:\windows\calc.exe")
If X$ = "" Then
MsgBox "It does Not exist!", vbExclamation, "Doesn't Exist"
Else
MsgBox "It does exist!", vbExclamation, "Does Exist"
End If

'================================================= ==
'Function: FileExists
'Description: Checks to see if a file exists
'Return Type: Boolean
'Where to place code: Module
'Notes: Set strPath to the path of the file you want to check
' Returns True if it exists, False if it doesn't
' Don't use this function to check for directories.
' In the case of directories, will return false if the
' directory doesn't have a "\" at the end of the path.
'
'Author: Oli
'Small bug fix by Matt Kirchoff
'http://www.littleguru.com
'================================================= =

Public Function FileExists(strPath As String) As Boolean
strPath = Trim(strPath)
If strPath = "" Then
FileExists = False
Exit Function
End If
FileExists = Len(Dir(strPath)) <> 0
End Function

xxxxx_xxxxx
جمعه 26 تیر 1388, 20:56 عصر
Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "PathFileExistsA" (ByVal pszPath As String) As Long

Private Sub Form_Load()
MsgBox PathFileExists("C:\text.txt")
End Sub

اگر مقدار صفر برگشت داده شد يعني فايل وجود نداره. و اگر مقدار 1 بود يعني وجود داره.
اين تابع همين كاربرد رو هم براي فولدر داره.
موفق باشيد/

Pr0grammer
جمعه 26 تیر 1388, 23:59 عصر
توضیح دوستان کامله؛
اما از File System Object هم می تونید استفاده کنید.

موفق باشید

123qwe
شنبه 27 تیر 1388, 08:14 صبح
سلام دوست عزیز
پروژه زیر را دانلود کن همه چیز توش مشخصه.
فقط اول باید Microsoft Scripting Runtime را از آدرس Project > References فعال کنی.
موفق باشی.