way to check for directory exists?

1,784 views
Skip to first unread message

TomL

unread,
Jan 20, 2011, 2:51:28 PM1/20/11
to Tasker
Is there a way to check if a directory exists? Or if a file exists?
I have some Delete Dir actions that I don't want to run if the dirs
they try to delete don't exist. If they do run, then they stop the
task execution with an error.

Tom

Rob.G

unread,
Jan 20, 2011, 8:55:18 PM1/20/11
to Tasker
That is on the ToDo list.

fubaya

unread,
Jan 20, 2011, 11:37:39 PM1/20/11
to Tasker
You can do this with the Locale Execute Plugin...

Change "DIRECTORY" to the exact directory. For example, if it's a
directory named "stuff" on the sdcard use /sdcard/stuff. I'm using /
sdcard/test.txt as a temp file. You can change it or you can delete it
at the end of the task.

1- Write File: test.txt Text: WAIT
2- Execute: @ [ -d DIRECTORY ] && echo ok > /sdcard/test.txt || echo
no > /sdcard/test.txt
3- Read Line test.txt to %YESORNO
4- GoTo 3 if %YESORNO matches WAIT

%YESORNO will be "yes" if the directory exists and "no" if it doesn't,
so then you delete the directory if %YESORNO matches yes.

If you need this for several tasks, then make this it's own task,
let's call it "CheckDir", and use this line for the Locale Execute
Plugin:

@ [ -d $(cat /sdcard/dir.txt) ] && echo ok > /sdcard/test.txt || echo
no > /sdcard/test.txt

This will look in the file /sdcard/dir.txt for the name of the
directory you want to check. In your main task, just add these lines:

Write File: dir.txt Text: (the directory you want to check)
Perform Task: CheckDir
delete stuff if %YESORNO matches yes.

If you only need this once, the first method is simpler but the
directory is hard coded. The second method allows the directory to be
variable and you can re-use the task instead of recreating the whole
task for different directories.

Hope this all makes sense.

On Jan 20, 2:51 pm, TomL <tomlo...@gmail.com> wrote:

fubaya

unread,
Jan 21, 2011, 11:42:45 AM1/21/11
to Tasker
Actually, don't listen to that dummy above, even though it was me.
With the Locale Execute Plugin you can simply use this:

rm -rf dir1 dir2 dir3 ... etc

It will throw errors if they dont exist but you'll never see them and
they won't interfere with tasker.

TomL

unread,
Jan 21, 2011, 3:59:55 PM1/21/11
to Tasker
Thank you, that's exactly what I need.

Tom

Bob

unread,
Jun 3, 2012, 7:27:35 AM6/3/12
to tas...@googlegroups.com
This is pretty old post with a problem I am trying to solve.

Wonder is it possible to perform simple check for directory/file existence in Tasker v1.2.1 ?

I am totally new to Tasker and I did not follow it';s development :(

If it's possible to perform such a check please share how. I was trying with FileType action which will gve error if argument(file/directory) is missing

Basically what I try to achieve is:

1. Check for existence of directory.
2. If it does not exist create it.

Matt R

unread,
Jun 3, 2012, 2:21:52 PM6/3/12
to tas...@googlegroups.com
To check for the existence of a directory, use Misc -> Test and set the "Type" to "File Name" (works for files AND directories).  Then put the directory path (e.g. /sdcard/new_dir) you want to test in "Data" and store the results in a local variable (e.g. %dir_exists).  Then, if the variable is unset, the directory doesn't exist.  Thus, you would add a "Create Directory" action and put /sdcard/new_dir in "Dir" and check the "If" box and put %dir_exists variable in the left box, click the tilda (middle gray box) and select "Is Set" as the operator.  Hope this helps.

Matt

Pgr

unread,
May 30, 2013, 11:12:55 AM5/30/13
to tas...@googlegroups.com

Hi CoolRaoul,

I tried your tip but it didn't work. I don't really understand what command you are using because your command starts with a switch... can you please explain?

I did get it to work using this Run/Shell command:

ls %mydir >/dev/null 2>/dev/null && echo 1 || echo 0

%mydir is a Tasker variable with something like /storage/sdcard0/somedir
If the file or directory exists, the result is 1

It looks messy because I divert the output of both stdout and stderr to /dev/null so I don't get results from the ls command, but only from the echoes.

There must be a cleaner way to do this, but at least it works...

Pedro
Message has been deleted

CoolRaoul

unread,
May 30, 2013, 12:03:36 PM5/30/13
to tas...@googlegroups.com
Exemple of a "fexists" (file exists) tasker callable task which returns 0 or 1 if file whose full path  given as parameter exists or not:


CoolRaoul

unread,
May 30, 2013, 12:05:16 PM5/30/13
to tas...@googlegroups.com


Le jeudi 30 mai 2013 17:12:55 UTC+2, Pgr a écrit :

I tried your tip but it didn't work. I don't really understand what command you are using because your command starts with a switch... can you please explain?

What do you call a "switch" ?

My command start with a *square bracket* 

Pgr

unread,
May 30, 2013, 1:59:41 PM5/30/13
to tas...@googlegroups.com
Ok, you're right, it does work correctly the way you said.

It seems I messed up something else when I first tried it (probably the directory name in the variable) and since I wasn't familiar with the square bracket syntax I started trying other commands.

Meanwhile I must have fixed the variable value, so when I went back to your command, it worked just fine. Great!

The Linux command-line never ceases to amaze me, I keep learning new tricks every day! :-)

Thanks a lot.
Message has been deleted

Bruno Silvino

unread,
Jan 13, 2014, 4:27:56 AM1/13/14
to tas...@googlegroups.com
Just an update on your comment, I was trying in this way that you said, but did not work. So I changed.. 
"Type" to "File Type"  (not "File Name")
It returns for directory: dir (stored in a local variable e.g. %dir_exists)
So then you can add an If condition like 
If %dir_exists ~ dir 

Test action
Type: File Type
Data: <directory path>
Store: Result In:  %dir_exists
Continue Task After Error: Checked (thats the trick to proceed to the next step of an If condition, what means in this case your "dir" doesn't exist)

Auguste Pop

unread,
Jan 13, 2014, 11:44:31 AM1/13/14
to tas...@googlegroups.com
run shell script
[ -d /your/directory/normally/starts/with/sdcard/if/on/sdcard ]

check the return value. 0 indicates it exists and is a directory.
or you can simply check the checkbox to stop running the task if it has any error.
Reply all
Reply to author
Forward
0 new messages