Link to home
Start Free TrialLog in
Avatar of Albee_J
Albee_J

asked on

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

Avatar of _agx_
_agx_
Flag of United States of America image

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>
Avatar of Albee_J
Albee_J

ASKER

Gotta be my directory path then, I will double check
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
Avatar of Albee_J

ASKER

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>
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.
Avatar of Albee_J

ASKER

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.
Albee_J - our posts crossed. That last response was not regarding your code.
Avatar of Albee_J

ASKER

Not sure what you mean agx?
>> 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...
Avatar of Albee_J

ASKER

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
>> 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.
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#">

Avatar of Albee_J

ASKER


>> 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
Avatar of Albee_J

ASKER

>>Then using a totally different variable?  

I just fixed that.  Lemme check my code agian

Avatar of Albee_J

ASKER

I'm getting rid of this line, I don't need it
<!--- <cfdirectory action="create" directory="E:\Coldfusion8\wwwroot\FMAF-Administrator\Camp-Slideshows\#SlideShowFolder#">--->
Avatar of Albee_J

ASKER

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>
So what's the current code, and what is it doing/not doing?
Avatar of Albee_J

ASKER

The current code is above your last post.  It is now not pulling all the images up
(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#/"
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>


>> 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.
Avatar of Albee_J

ASKER

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>
Avatar of Albee_J

ASKER

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>
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)?
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.
Avatar of Albee_J

ASKER

No problem agx, I appreciate your help!
Avatar of Albee_J

ASKER

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">
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)?


Avatar of Albee_J

ASKER

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>
Avatar of Albee_J

ASKER

It is definitely not liking this with the cfftp it seems to be ignoring it.

<cfif DirectoryExists(theDirecPath)>
>> 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

SOLUTION
Avatar of _agx_
_agx_
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial