FileSystemObject : FileExists Method
Description:
Returns True if a specified file exists; False if it does not.
Format :
objectOfFileSystemObject. FolderExists(fileName)
objectOfFileSystemObject : As the names says, it’s a FileSystemObject.
Arguments:
- fileName
- Mandatory
- Type: String
- File , whose existence to be determined.
Function FnIsFileExist(strCompleteFilePath) Set fso = CreateObject("Scripting.FileSystemObject") If fso. FileExists(strCompleteFilePath)Then strStatus = strCompleteFilePath & “ file exists” Else strStatus = strCompleteFilePath & “ file does not exist” End If FnIsFolderExist = strStatus End Function
Msgbox FnIsFileExist (“c:\New Folder\file.txt”)
Having a hard time executing these( just a beginner #embarrassed).
By default, in my code window i had some code written[in a new workbook]. i deleted them and wrote this code:
Function FnIsFileExits(strCompleteFilePath)
Set fso = CreateObject(“Scripting.FileSystemObject”)
If fso.FileExits(strCompleteFilePath) Then
strStatus = strCompleteFilePath & “File there nigga”
Else
strStatus = strCompleteFilePath & “Aint no FIle there man”
FnIsFolderExists = strStatus
End Function
However, i was not sure where to run/call this function from. i clicked on what looked like a play button and it asked me to save my micro. so i did with name: FnIsFileExits.
This took me to another[new code] window where below code was there by default:
Sub isFileExists()
End Sub
i had no idea what to do next. so i wrote this inside[stupid enough i know]:
Sub isFileExists()
MsgBox FnIsFileExits(“D:/hello.xmls”)
End Sub
when i tried to run this i got below error:
—————————
Microsoft Visual Basic for Applications
—————————
Compile error:
Sub or Function not defined
—————————
OK Help
—————————
i would be really helpful if you can show full coding of the program or a how to run thingy.
there were very small mistakes ..
below is the code which worked for me
Sub Vishal()
MsgBox FnIsFileExits(“D:/hello.xmls”)
End Sub
Function FnIsFileExits(strCompleteFilePath)
Set fso = CreateObject(“Scripting.FileSystemObject”)
If fso.FileExists(strCompleteFilePath) Then
strStatus = strCompleteFilePath & “File there nigga”
Else
strStatus = strCompleteFilePath & “Aint no FIle there man”
End If
FnIsFileExits = strStatus
End Function
IF you wanna read more about File System objects, here is the link
http://excel-macro.tutorialhorizon.com/filesystemobject-in-excel-macro/
Feel free to ask anythg 🙂
Sumit