Personally I would use method 1 if dealing with a single QVD file and method 2 for all other files or when dealing with wild cards in file names. I would avoid method 3; although it is a completely valid method, in my opinion, it belongs within a much more complex custom error handling routine which is outside the scope of this post.
Method 1 - qvdCreatTime()
This method uses an inbuilt function within QV called qvdCreatTime() which returns the date and time the QVD was created. If the QVD doesn't exist, qvdCreatTime() returns NULL and thus we can use it to check if a QVD exists. Obviously this method is limited to QVD files only. It also cannot handle wild cards.IF (NOT isNull(qvdCreateTime('myqvdfile.qvd'))) THEN
LOAD * FROM myqvdfile.qvd (qvd);
END IF
Method 2 - For Each File in Filelist()
This method uses the filelist functions within a for loop. This function is only possible within the loop and has the benefit that wild cards can be used within the file name and path. This allows you to load multiple files that match a pattern by looping through them. Assuming the field names are all identical the files will be concatenated into a single table. Unlike method 1 above, this method can be used on any file type.
FOR each File in filelist ('myqvdfile.qvd')
LOAD * FROM myqvdfile.qvd (qvd);
NEXT File
Method 3 - ErrorMode & ScriptError
This method isn't strictly checking if a file exists but rather changing the way QlikView reacts to errors if it tries to load a file that it can't find. To do this we change the value of the ErrorMode variable to 0 which prevents QV from stopping the script execution if an error occurs. We then try to load the table and afterwards check the variable ScriptError to see if an error has occurred. If ScriptError has a value of 1 then no error occurred. A value of 8 denotes that the error that occurred was that the file does not exist. We can replace the TRACE statements with the relevant actions to take. We then set ErrorMode back to it's default value of 1 and continue the rest of the script.
SET ErrorMode = 0;
LOAD * FROM myqvdfile.qvd (qvd);
IF ScriptError>1 AND ScriptError<>8 THEN
TRACE File not found;
ELSE
TRACE File loaded;
END IF
SET ErrorMode = 1;
Another way to check if a file exists:
ReplyDeleteIF len(FileSize('myfile.txt')) > 0 THEN
....
END IF
- Ralf
Hi Ralf
DeleteYup that would work too. I've also seen it done like this:
IF NOT isNull(fileSize('myfle.txt')) THEN
....
END IF
Matt
Can I user variable with the fucntions qvdCreatTime() and filesize().
DeleteThe parameter fo rteh filenames would be something like sales$(vYear).txt ?
Would this method be applicable on a web table?
ReplyDeleteTo se if a web table exists, then load the data from it?
Hi Richard
DeleteYou'd need to use method 3 in the post above when using web tables.
Regards
Matt
To check for the existence of a folder you can do this:
ReplyDeleteif FileTime('C:\Test') > 0 then
Set vDirExists = 'Yes';
else
Set vDirExists = 'No';
end if
Thanks to Rakesh Mehta!
http://community.qlikview.com/thread/2715