Alteryx Designer Desktop Discussions

Find answers, ask questions, and share expertise about Alteryx Designer Desktop and Intelligence Suite.

How to check if a specific folder exists or not?

jpscoralick
8 - Asteroid

Hello everyone!

 

Every month I must read a bunch of files from a directory which has subfolders named as month's numbers (01 to 12). In order to read different months, I use a numeric constant.


The main issue here is that each folder is created during its month, for example: on February, the directory already has the folder of January (01), and the folder of February (02) will be eventually created during the month (and, for now, the remaining months don't have its folders).

 

Therefore, I'd like to check inside the directory if a folder exists or not. Is it possible?

I mean, I know that Directory Tool would generate an error if the folder doesn't exists, but I'm trying to catch the error (maybe with Message Tool) and handle it in a way that does not interrupt the workflow execution (like the error from Directory Tool does).

 

Can someone help me?

Thanks in advance.

11 REPLIES 11
dsandmann
8 - Asteroid

The best way to do this would likely incorporate the use of run command using dir like seen here - https://stackoverflow.com/questions/15214486/command-to-list-all-files-in-a-folder-as-well-as-sub-fo...

 

You can parse the output from there, as long as the main folder exists I believe you won't get an error.

binuacs
21 - Polaris

@jpscoralick The directory tool will not show any folder name if it is empty, If your folder will not be empty then you can use the directory tool else you should use the python code to check whether the folder exists or not.

jpscoralick
8 - Asteroid

Hey, @binuacs

Thanks for your quick answer.

 

The main folder that I have to read in the Directory Tool is not empty. In January it'd have the "01 folder". In February, it'd have "01" and during the month, the "02" folder.

So, I want to run my workflow in February and check if the "02" folder already exists or not.

 

And regarding the second suggestion of yours, even if I use a python code to check whether the folder exists or not, the Directory Tool would generate an error. So, the workflow execution would be affected. I want to catch this error and handle it in order the workflow keep its execution. Do you have any idea how to solve this?

 

Thanks a lot again!

 

 

binuacs
21 - Polaris

@jpscoralick Are these folders (February 01, 02) will be having any files or just folders without any files? If there are no files present in a folder then the directory tool will not pick that folder. Do you think you can check the existence of the folder in that way? I mean if the directory tool did not return any folder which you are looking for means the folder might not exist/be empty.

jpscoralick
8 - Asteroid

 

@binuacs  the folders always have at least one file.

The main issue is that I have to input in the Directory Tool a path that I'm not sure whether exists or not, once folders (01, 02, 03 etc.) may not have been created yet. Also I cannot create a pre-check once Directory Tool doest not accept any other tool as input.

 

 

jpscoralick
8 - Asteroid

Hey, @dsandmann.

Thanks a lot for answering me!

Imma take a look at, although I don't know how to use command line within Alteryx.

 

dsandmann
8 - Asteroid

@jpscoralick - if the subfolder will always have a file in it, why not just just use the parent folder in the directory tool? from there you can parse the subfolder location and compare it to expectations based on date timeframes

 

To use command line in Alteryx you would use the Run Command tool in the Developer tool area, which may need to be unhid.

jpscoralick
8 - Asteroid

Hey @dsandmann ! Thanks for helpful answer.

Please, could you show an example regarding how can I check if a subfolder exists from reading the parent folder with Directory Tool?

FrederikE
13 - Pulsar

Hey @jpscoralick,

 

Use the settings like in the following picture:

FrederikE_0-1678453309619.png

 

This outputs all files and filepaths that are on my desktop including folder on there and folders in these folders, etc. 

You just need to shift the location to your desired folder. Then you get all the files there and can extract the foldernames from the paths. 

Labels
Top Solution Authors