Jump to content
Mzigaib

Check if directory exists with hscript

Recommended Posts

Is there a way to check if a directory exists with hscript? I try this:

set txt=''
foreach dir(`run('uls -p /mnt/non_existente/directory')`)
    if ($dir != "")
        set txt = 'nothing'
    else
        set txt = 'all'
end
message $txt

But if the directory doesn't exist it return me nothing, any tips?

 

Thanks.

Share this post


Link to post
Share on other sites

I found a way:

# Check if directory exists
set check=''
foreach dir(`system(" if [ -d 'your_directory_path_or_not' ];then echo 'yes';else echo 'not';fi")`)
    if (`strmatch($dir,"not")`==1)
        set check = 'Not Found'
    else 
        set check = 'Found'
    endif
end
message $check

But if anyone have a better and more elegant way let me know.

Thanks.

Edited by Mzigaib

Share this post


Link to post
Share on other sites

Is there a reason this needs to be HScript? In Python:

import os

msg = "Not Found"

if os.path.exists(path):

    msg = "Found"

return msg

 

  • Haha 1

Share this post


Link to post
Share on other sites
4 hours ago, toadstorm said:

Is there a reason this needs to be HScript? In Python:


import os

msg = "Not Found"

if os.path.exists(path):

    msg = "Found"

return msg

 

Yes I don't use phyton, but thanks anyway.

Share this post


Link to post
Share on other sites

Extended it a bit:

import os

node = hou.pwd()
dir = node.parm('directory').eval()

msg = 'Directory {} does not exist'.format(dir)

if os.path.exists(dir):
    msg = 'Directory {} exists.'.format(dir)
elif not dir:
    msg = 'Choose a directory.'

print msg

 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×