Skip to main content
Announcements
Qlik Connect 2024! Seize endless possibilities! LEARN MORE
cancel
Showing results for 
Search instead for 
Did you mean: 
Not applicable

How can I find if a path is empty of files?

I have a script that reads files from a directory, like:

c:\data\charges.*

This folder may or may not have data in it, and I don't know exactly what the file name is, so FileSize won't work -- I mean, I can't do FIleSize("charges.*"), can I?

I can't see a function where I can feed it a file path and it'll come back with the number of files. Has anyone found a solution for this?

Thanks.

1 Solution

Accepted Solutions
hector
Specialist
Specialist

ok, try this script

For each file in dirlist ('c:\data\*');
Files:
Load '$(file)' as File autogenerate(1);
next Let NoOfFiles = NoOfRows('Files'); //This will return he n° of files

or this

Set NoOfFiles = 0;
For each file in dirlist (''c:\data\*');
Let NoOfFiles = NoOfFiles + 1; //This will return he n° of files
next

so, then you can do

if NoOfFiles > 0 then load .... else exit script end if maybe you can try with Set ErrorMode = 0 too

rgds

View solution in original post

6 Replies
hector
Specialist
Specialist

hi, try filename() and filesize() just like here, without parameters, they will return the value for each of your charges* files

rgds

PD. by the way, you can use these functions to create columns, like filename() as file_name

Not applicable
Author

Sorry, I didn't make myself clear before. What I'm trying to do is see if the folder has anything in it, and if not, I want to exit script to avoid an error that arises when there aren't any files in the folder.

So, filename() and filesize() won't work because there aren't any files to be found.

hector
Specialist
Specialist

ok, try this script

For each file in dirlist ('c:\data\*');
Files:
Load '$(file)' as File autogenerate(1);
next Let NoOfFiles = NoOfRows('Files'); //This will return he n° of files

or this

Set NoOfFiles = 0;
For each file in dirlist (''c:\data\*');
Let NoOfFiles = NoOfFiles + 1; //This will return he n° of files
next

so, then you can do

if NoOfFiles > 0 then load .... else exit script end if maybe you can try with Set ErrorMode = 0 too

rgds

Not applicable
Author

Thanks, Héctor. I needed to use filelist instead of dirlist, but this worked just right.

Not applicable
Author

Hi Hector Munoz,

I am trying which is very similar to your post.

But the difference is i need to  check whether any Text Files are there or not ?

If any Text Files are present in that folder then variable VData =1 or

If no Text Files are present in that folder then set variable VData =0.

How i can work on this ?

Thanks in advance.

Regards

Venkat

giuval45
Partner - Contributor III
Partner - Contributor III

Hi Hector,

your second solution, with "filelist" instead of "dirlist" it works perfectly, also without Set ErrorMode = 0.

Many thanks,

Giulio