Looks like no one’s replied in a while. To start the conversation again, simply ask a new question.

Using an "if not exists" command with a folder on an SMB share

I'm trying to use an "if not exists" command on a folder which is stored on a mounted smb share..


This part of my script is supposed to check if a folder exists and;


If it does - Skip to the next part of the script


If it doesn't - Create the folder.


At the moment it either errors saying that the folder doesn't exist or doesn't create the folder.


I can get this script to work fine using local folder paths but I want to do it on an SMB share..


Here's my script.



Many thanks in advance.


Martin


-----------


set username to (do shell script "whoami")

set Media to " Media Files"

mount volume "smb://172.22.8.23/macmedia$"

tell application "Finder"


if not (exists ("smb://172.22.8.23/macmedia$/" & username as text) & Media as text) then

make new folder at "macmedia$" with properties {name:(username as text) & Media as text}

end if


end tell

tell application "Finder"


set mnt to ("smb://172.22.8.23/macmedia$/" & username as text) & Media as text


try
mount volume mnt

end try


eject "macmedia$"


end tell


------------------


Posted on Dec 2, 2011 9:12 AM

Reply
Question marked as Best reply

Posted on Dec 2, 2011 9:25 AM

You're not doing what you think you are.


Consider your script:


tell application "Finder"


if not (exists ("smb://172.22.8.23/macmedia$/" & username as text) & Media as text) then


You're asking the Finder if the string "smb://172.22.8.23" exists. Of course it does. You can see it right there in the script.


If you want the Finder to check if a volume is mounted, or if a folder exists, you need to be more specific:


if not (exists disk "macmedia") then

or


if not (exists folder username of disk "macmedia") then

In other words, you need to be explicit about what it is you're looking for.

3 replies
Question marked as Best reply

Dec 2, 2011 9:25 AM in response to martin.hannah

You're not doing what you think you are.


Consider your script:


tell application "Finder"


if not (exists ("smb://172.22.8.23/macmedia$/" & username as text) & Media as text) then


You're asking the Finder if the string "smb://172.22.8.23" exists. Of course it does. You can see it right there in the script.


If you want the Finder to check if a volume is mounted, or if a folder exists, you need to be more specific:


if not (exists disk "macmedia") then

or


if not (exists folder username of disk "macmedia") then

In other words, you need to be explicit about what it is you're looking for.

Dec 6, 2011 1:02 AM in response to Camelot

Hi Camelot,


Thanks a lot for that the script's working as it should now..


I'm still having intermittent problems on some clients where it will only map the root volume "macmedia$" and freeze there.. This is a problem as they can then see and access everyone elses folders from there.


Do you know of a better/cleaner way to do what I'm trying to acheive? All i'm after is mapping an additional drive for student's so they can save larger files to our media server within their own folders.


Is there a way of checking if a folder exisits within an smb share without mounting the volume? Just trying to think outside of the box a bit..


Here's my new script;


----------------------------------


set username to (do shell script "whoami") & " Media" as text


mount volume "smb://172.22.8.23/macmedia$"


tell application "Finder"


if not (exists folder username of disk "macmedia$") then



makenewfolderatdisk "macmedia$" with properties {name:username as text}


end if


end tell



tell application "Finder"


set mnt to ("smb://172.22.8.23/macmedia$/" & username as text)


try


mount volumemnt


end try


eject "macmedia$"


end tell


----------------------------------------


Many thanks,


Martin

Using an "if not exists" command with a folder on an SMB share

Welcome to Apple Support Community
A forum where Apple customers help each other with their products. Get started with your Apple ID.