Coldfusion if directory exists delete and recreate

I am having a hard time with the syntax.  I am trying to take my existing code and check if a directory already exists by that name.  If it does I want to delete it and recreate it.

Here is the existing code:

I see tutorials on this page http://livedocs.adobe.com/coldfusion/8/htmldocs/help.html?content=Tags_d-e_03.html  but I am messing up the syntax :/
<!---Slide Show--->
<cfif findnocase('\\fmafs1\fmafiles\X-Change\All Users\',Arguments.SlideShow) gt 0>

 
   <cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
     
  <cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">
 
 
<cfftp action="open" 
 username="****"
password="*****"
server="****"
connection="images" 
passive="Yes" 
stopOnError = "Yes" 
timeout = "1200" 
retrycount="1">

	<cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
	<cfftp action="createdir" directory="#SlideShowFolder#" connection="images">
	<cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/" connection="images">
	<cfloop query="getImages">
		<cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#Arguments.SlideShow#\#getImages.name#" remotefile="#getImages.name#">
	</cfloop>


<cfftp  action = "close"
 connection = "images">    

                 <cfset Image = "">
      <cfelse>
                    <cfset Image = "">
      </cfif>

Open in new window

Albee_JAsked:
Who is Participating?
 
Albee_JConnect With a Mentor Author Commented:
agx:
I will use your suggestions to store the local and remote paths.  For now here is the working code to delete a remote directory if it exists and recreate it.


<!--- Check whether the directory exists. --->
<cfset theDirecPath = "/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#">

<cfftp connection="images" action="existsDir" directory="#theDirecPath#" result="dirResults">
<cfif dirResults.returnvalue>

    <!--- delete files in the directory before deleting directory. --->
<cfftp Connection="Images" Action="ListDir" Directory="#theDirecPath#" Name="Listing">
<cfloop index="x" From="1" to="#Listing.RecordCount#">
<cfftp Connection="Images" Action="Remove" Item="#Listing.Path[x]#">
</cfloop>

    <!---delete the directory. --->
    <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
   <cfftp action="RemoveDir" directory="#SlideShowFolder#" connection="images">
     

<!--- Then Create folder on remote server and upload all images--->
      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
      <cfftp action="createdir" directory="#SlideShowFolder#" connection="images">
      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/" connection="images">
      <cfloop query="getImages">
            <cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#Arguments.SlideShow#\#getImages.name#" remotefile="#getImages.name#">
      </cfloop>

<cfftp  action = "close"
 connection = "images">    
<cfset Image = "">

<cfelse>

<!--- Just create folder on remote server and upload all images--->
      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
      <cfftp action="createdir" directory="#SlideShowFolder#" connection="images">
      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/" connection="images">
      <cfloop query="getImages">
            <cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#Arguments.SlideShow#\#getImages.name#" remotefile="#getImages.name#">
      </cfloop>

</cfif>
</cfif>

  <!---END Slide Show Code --->
0
 
_agx_Commented:
What part are you having problems with?  As long as you've got a valid directory path, it's just:

<cfset theDirecPath = "c:\something\orOther\">
<cfif DirectoryExists(theDirecPath)>
     <!--- add RECURSE to delete all subfolders as well ....--->
     <cfdirectory action="delete" directory="#theDirecPath#">
     <cfif NOT DirectoryExists(theDirecPath)>
         <cfdirectory action="create" directory="#theDirecPath#">
     </cfif>
</cfif>
0
 
Albee_JAuthor Commented:
Gotta be my directory path then, I will double check
0
Cloud Class® Course: Microsoft Windows 7 Basic

This introductory course to Windows 7 environment will teach you about working with the Windows operating system. You will learn about basic functions including start menu; the desktop; managing files, folders, and libraries.

 
Gurpreet Singh RandhawaWeb DeveloperCommented:
yes! pretty Straight like agx said


<cfset theDirecPath = "c:\something\orOther\">
<cfif DirectoryExists(theDirecPath)>
     <!--- add RECURSE to delete all subfolders as well ....--->
     <cfdirectory action="delete" directory="#theDirecPath#" recurse="yes">
</cfif>
<cfif NOT DirectoryExists(theDirecPath)>
         <cfdirectory action="create" directory="#theDirecPath#">
</cfif>

it will also work
0
 
Albee_JAuthor Commented:
I found the problem, I needed to delete and recreate in two areas, my local and live.  Keeping a copy on my local


 <!---Slide Show--->
<cfif findnocase('\\fmafs1\fmafiles\X-Change\All Users\',Arguments.SlideShow) gt 0>

   <cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 <cfset homeDirecPath = "E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">
 <cfset siteDirecPath = "/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/">
<cfif DirectoryExists(homeDirecPath) AND DirectoryExists(siteDirecPath)>
     <!--- add RECURSE to delete all subfolders as well ....--->
     <cfdirectory action="delete" directory="#homeDirecPath#" recurse="yes">
        <cfdirectory action="delete" directory="#siteDirecPath#" recurse="yes">
     
             <cfif NOT DirectoryExists(theDirecPath)>
      <cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
      <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">
 
 
<cfftp action="open"
 username="***"
password="***"
server="***"
connection="images"
passive="Yes"
stopOnError = "Yes"
timeout = "1200"
retrycount="1">

      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
      <cfftp action="createdir" directory="#SlideShowFolder#" connection="images">
      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/" connection="images">
      <cfloop query="getImages">
            <cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#Arguments.SlideShow#\#getImages.name#" remotefile="#getImages.name#">
      </cfloop>


<cfftp  action = "close"
 connection = "images">    

                 <cfset Image = "">
      <cfelse>
                    <cfset Image = "">
            </cfif>
            </cfif>
      </cfif>
0
 
_agx_Commented:
I don't see that adaptation adding any benefit.  Since the goal is to perform 2 actions, then both be grouped together within the same CFIF block.  If nothing else, it's more logical and intuitive.
0
 
Albee_JAuthor Commented:
Not giving me an error but it doesn't look like it is deleting the existing files and pulling the new.  I added about 4 more images to the folder and tested.  No error, but it didn't recreate the folder right.  If it did the 4 new images would have shown up.
0
 
_agx_Commented:
Albee_J - our posts crossed. That last response was not regarding your code.
0
 
Albee_JAuthor Commented:
Not sure what you mean agx?
0
 
_agx_Commented:
>> I needed to delete and recreate in two areas,

Are you sure you're doing _both_ steps for both folders?  I'm not seeing to "create" actions...
0
 
Albee_JAuthor Commented:
What it does not that I like it, is create just the folder on my local, then creates on live with files.
 Not sure how to fix
0
 
_agx_Commented:
>> Not sure what you mean agx?

I just meant our posts keep crossing.  So our responses are out-of-synch and may not make sense.
0
 
_agx_Commented:
I'm not sure the folders and variables are right. It seems like something is missing.

You seem to be deleting these 2 folders
     <cfdirectory action="delete" directory="#homeDirecPath#" recurse="yes">
     <cfdirectory action="delete" directory="#siteDirecPath#" recurse="yes">

Then using a totally different variable?      
             <cfif NOT DirectoryExists(theDirecPath)>

Then re-creating only 1 of the folders...? I would have expected 2 create statements
      <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">

0
 
Albee_JAuthor Commented:

>> I just meant our posts keep crossing.  So our responses are out-of-synch and may not make sense.
:) I understand agx

The code as it stands doesn't really recreate the whole folder. It just doesn't error out.  Not sure how to fix that
0
 
Albee_JAuthor Commented:
>>Then using a totally different variable?  

I just fixed that.  Lemme check my code agian

0
 
Albee_JAuthor Commented:
I'm getting rid of this line, I don't need it
<!--- <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">--->
0
 
Albee_JAuthor Commented:
Ok here is the revised code.  But it is still not truly deleting and recreating the directory.  I added 4 more images to my folder and ran the code.  They did not appear on the live.

<!---Slide Show--->
<cfif findnocase('\\fmafs1\fmafiles\X-Change\All Users\',Arguments.SlideShow) gt 0>

   <cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 <cfset theDirecPath = "/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/">
<cfif DirectoryExists(theDirecPath)>
     <!--- add RECURSE to delete all subfolders as well ....--->
     <cfdirectory action="delete" directory="#theDirecPath#" recurse="yes">
     
             <cfif NOT DirectoryExists(theDirecPath)>
       <cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
     

<!--- <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">--->
 
 
<cfftp action="open"
 username=" "
password=" "
server=""
connection="images"
passive="Yes"
stopOnError = "Yes"
timeout = "1200"
retrycount="1">

      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
      <cfftp action="createdir" directory="#SlideShowFolder#" connection="images">
      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/" connection="images">
      <cfloop query="getImages">
            <cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#Arguments.SlideShow#\#getImages.name#" remotefile="#getImages.name#">
      </cfloop>


<cfftp  action = "close"
 connection = "images">    

                 <cfset Image = "">
      <cfelse>
                    <cfset Image = "">
            </cfif>
            </cfif>
      </cfif>
0
 
_agx_Commented:
So what's the current code, and what is it doing/not doing?
0
 
Albee_JAuthor Commented:
The current code is above your last post.  It is now not pulling all the images up
0
 
_agx_Commented:
(Here we go again with the cross posts ... ;-)

I think there's too many different path variables and hard coded paths. That might be part of the problem.  Do you really need all of the different permutations?  

For example, what's this value
Localfile="#Arguments.SlideShow#\#getImages.name#

... and how does it relate to all of the others

 <cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 <cfset theDirecPath = "/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/">
 .... directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
.... #SlideShowFolder#" connection="images">
....directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/"
0
 
_agx_Commented:
Let's try this again.  

I think your condition is backward.

 <cfif DirectoryExists(theDirecPath)>
     <cfdirectory action="delete" directory="#theDirecPath#" recurse="yes">

      this will never execute because you just deleted the directory .....

     <cfif NOT DirectoryExists(theDirecPath)>
         .... do ftp stuff .....

        <cfset Image = "">
     <cfelse>
          <cfset Image = "">
     </cfif>

</cfif>


0
 
_agx_Commented:
>> this will never execute because you just deleted the directory .....

No, that's incorrect.  

With all the nested cfif's, I'd add some debugging code so you can see which steps are executing and when.  Not to mention, check the cfftp status to make sure it's actually doing something.
0
 
Albee_JAuthor Commented:
Yes.

 <cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
is giving me just the folder name from the url that the files are coming from.  That is being stored in the database and used to point to the right folder to build the slideshow with.

 <cfset theDirecPath = "/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/">

 <cfset theDirecPath = "/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/">
Is looking for the folder on the live server

      <cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
Is getting all of the images that are stored in the folder located on the users computer (our xchange server)

This creates the directory and loops through all the images to pull them up

<cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
      <cfftp action="createdir" directory="#SlideShowFolder#" connection="images">
      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/" connection="images">

      <cfloop query="getImages">
            <cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#Arguments.SlideShow#\#getImages.name#" remotefile="#getImages.name#">
      </cfloop>
0
 
Albee_JAuthor Commented:
The code worked perfectly until I added the code to try and delete and recreate the existing directory

<!---Slide Show--->
<cfif findnocase('\\fmafs1\fmafiles\X-Change\All Users\',Arguments.SlideShow) gt 0>

 
   <cfdirectory action="list" directory="#Arguments.SlideShow#" filter="*.gif|*.png|*.jpg" name="getImages">
     
  <cfset SlideShowFolder = listgetat(SlideShow,listlen(Arguments.SlideShow,'\'),'\')>
 
<cfftp action="open"
 username=" "
password=" "
server=" "
connection="images"
passive="Yes"
stopOnError = "Yes"
timeout = "1200"
retrycount="1">

      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
      <cfftp action="createdir" directory="#SlideShowFolder#" connection="images">
      <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#/" connection="images">
      <cfloop query="getImages">
            <cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#Arguments.SlideShow#\#getImages.name#" remotefile="#getImages.name#">
      </cfloop>


<cfftp  action = "close"
 connection = "images">    

                 <cfset Image = "">
      <cfelse>
                    <cfset Image = "">
      </cfif>
0
 
_agx_Commented:
Given the nested cfif's it sounds like the logic may have been added in the wrong place.
What does the code look like without the delete (ie working code)?
0
 
_agx_Commented:
Never mind, I see that's the new code.

Well it's either 1 of 2 things.  Either one of the variables you created in the new code are wrong OR the CFIF structure is wrong.  That's why I suggested adding debugging.

Unfortunately, I'm late for a call right now. So I have to step off for an hour or so.
0
 
Albee_JAuthor Commented:
No problem agx, I appreciate your help!
0
 
Albee_JAuthor Commented:
Still working on the issue but I know I can't use    <cfdirectory> because I am trying to remove files from a remote server.  I can delete using

  <cfftp action="RemoveDir" directory="#SlideShowFolder#" connection="images">
0
 
_agx_Commented:
Just back from my meeting, and I was going to ask you about that.  Though you said delete a directory, looking at the code I realized you were probably trying to delete a folder on the remote ftp server instead.

So you always want to remove the remove the remote ftp folder (assuming it exists)?


0
 
Albee_JAuthor Commented:
I have the code in place to remove the directory and its files. I just need to put in the cfif logic right now

<!--- Check whether the directory exists. --->
<cfset theDirecPath = "/www.nutsandboltsfoundation.org/Manufacturing-Camps/#SlideShowFolder#">
<!---<cfif DirectoryExists(theDirecPath)>--->


    <!--- If yes, check whether there are files in the directory before deleting. --->
<CFFTP Connection="Images" Action="ListDir" Directory="#theDirecPath#" Name="Listing">

<CFloop index="x" From="1" to="#Listing.RecordCount#">

  <CFFTP Connection="Images" Action="Remove" Item="#Listing.Path[x]#">

</CFLOOP>

    <!--- Directory is empty - just delete the directory. --->
    <cfftp action="changedir" directory="/www.nutsandboltsfoundation.org/Manufacturing-Camps/" connection="images">
   <cfftp action="RemoveDir" directory="#SlideShowFolder#" connection="images">
        <cfoutput>
        <p>The directory existed and has been deleted.</P>
        </cfoutput>
       
 
       
<!--- </cfif>--->
 <cfabort>
0
 
Albee_JAuthor Commented:
It is definitely not liking this with the cfftp it seems to be ignoring it.

<cfif DirectoryExists(theDirecPath)>
0
 
_agx_Commented:
>> I just need to put in the cfif logic right now

Using something like cfftp action="existsDir"? Okay.

BTW: In case you were interested, this was what I had in mind when I mentioned condensing the variables and removing all the extra/hard-coded paths.  There's only 2 lines containing hard coded paths.  So I think it's easier to read and maintain.

*Obviously not tested, and based on the working version (without the delete folder code).  

....
<!--- Set REMOTE paths --->
<cfset RemotePath = "/www.nutsandboltsfoundation.org/Manufacturing-Camps/">
<!--- Set LOCAL paths (ie xchange server) --->
<cfset LocalPath = "\\fmafs1\fmafiles\X-Change\All Users\">
<cfif findnocase(LocalPath, Arguments.SlideShow) gt 0>

    <!--- Get list of local images --->
    <cfset LocalFolderName  = listLast(Arguments.SlideShow,'\')>
    <cfset RemoteFolderName = LocalFolderName >
    <cfdirectory action="list" directory="#LocalPath#" filter="*.gif|*.png|*.jpg" name="getImages">


    <!--- Connect to REMOTE server --->
    <cfftp action="open" username=" " password=" " server=" " connection="images" passive="Yes" stopOnError = "Yes" timeout = "1200" retrycount="1">
    <!--- Create directory on REMOTE server --->
    <cfftp action="changedir" directory="#RemotePath#" connection="images">
    <cfftp action="createdir" directory="#RemoteFolderName#" connection="images">
    <cfftp action="changedir" directory="#RemotePath#/#RemoteFolderName#/" connection="images">
    <!--- Copy images from LOCAL to REMOTE server --->
    <cfloop query="getImages">
        <cfftp action="PUTFILE" connection="images" passive="Yes" stopOnError="Yes" Localfile="#LocalPath#\#getImages.name#" remotefile="#getImages.name#">
    </cfloop>

</cfif>

Open in new window

0
 
_agx_Connect With a Mentor Commented:
>> <cfif DirectoryExists(theDirecPath)>

DirectoryExists only works on local or UNC paths.  To delete a remote ftp folder, you have to use CFFTP.  I haven't tested it myself, but you could try using the "existsDir" command:

<cfftp connection="images" action="existsDir" directory="#RemoteFolderPath#" result="dirResults">
<cfif dirResults.returnvalue>
     exists.... code to delete it here...
<cfelse>
    DEBUG: doesn't exist. do something else here...
</cfif>
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.