How to check if file exists (QTP, VBScript)
Parent page: Service Functions – System (QTP, VBScript)
Use FileSystemObject.FileExists method.
Argument should be a full path string.
When to use it? Always before trying to access the file (delete, rename, or read from).
Does file format matter? No. It could be text file, excel spreadsheet, bitmap, binary file, – anything. Just make sure you pass into both filename and extension.
Note. Always release the object you created when you no longer need it. If you don’t do that then in the long run it’ll cause you memory leakage problem.
Set FSO = CreateObject("Scripting.FileSystemObject") boolRC = FSO.FileExists(sFileName) Set FSO = Nothing 'release an object If Not boolRC Then 'do what you need to do if 'file does not exist End If
2 responses to "How to check if file exists (QTP, VBScript)"
Hai,
I am Suguna, thanks for your examples..
I am new to QTP i dont know all functions. Can you help me?? I need to check number of text files in the folder but i dont know names i cant use FSO.FileExists?
Thanks in advance!!!
[ Albert’s reply: sample scripts here. ]
Hi Albert,
How to check if path exists? No files.
Thanks to you.
[ Albert’s Reply.
Hi Ashish,
I’m not really sure about your question. Did you mean to check if a path string is valid or a folder defined by the path exists? Use FolderExists method of FileSystemObject to check ]