Solved

Coldfusion if directory exists delete and recreate

Posted on 2010-09-23
33
2,084 Views
Last Modified: 2012-05-10
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

0
Comment
Question by:Albee_J
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 17
  • 15
33 Comments
 
LVL 52

Expert Comment

by:_agx_
ID: 33746598
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
 

Author Comment

by:Albee_J
ID: 33746816
Gotta be my directory path then, I will double check
0
 
LVL 16

Expert Comment

by:Gurpreet Singh Randhawa
ID: 33746893
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
Get Actionable Data from Your Monitoring Solution

Your communication platform is only as good as the relevance of the information you send. Ensure your alerts get to the right people every time with actionable responses. Create escalation rules that ensure everyone follows the process and nothing is left to chance.

 

Author Comment

by:Albee_J
ID: 33747024
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
 
LVL 52

Expert Comment

by:_agx_
ID: 33747066
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
 

Author Comment

by:Albee_J
ID: 33747074
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
 
LVL 52

Expert Comment

by:_agx_
ID: 33747082
Albee_J - our posts crossed. That last response was not regarding your code.
0
 

Author Comment

by:Albee_J
ID: 33747090
Not sure what you mean agx?
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33747111
>> 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
 

Author Comment

by:Albee_J
ID: 33747130
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
 
LVL 52

Expert Comment

by:_agx_
ID: 33747136
>> 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
 
LVL 52

Expert Comment

by:_agx_
ID: 33747170
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
 

Author Comment

by:Albee_J
ID: 33747172

>> 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
 

Author Comment

by:Albee_J
ID: 33747196
>>Then using a totally different variable?  

I just fixed that.  Lemme check my code agian

0
 

Author Comment

by:Albee_J
ID: 33747220
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
 

Author Comment

by:Albee_J
ID: 33747269
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
 
LVL 52

Expert Comment

by:_agx_
ID: 33747279
So what's the current code, and what is it doing/not doing?
0
 

Author Comment

by:Albee_J
ID: 33747313
The current code is above your last post.  It is now not pulling all the images up
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33747318
(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
 
LVL 52

Expert Comment

by:_agx_
ID: 33747398
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
 
LVL 52

Expert Comment

by:_agx_
ID: 33747429
>> 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
 

Author Comment

by:Albee_J
ID: 33747447
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
 

Author Comment

by:Albee_J
ID: 33747483
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
 
LVL 52

Expert Comment

by:_agx_
ID: 33747546
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
 
LVL 52

Expert Comment

by:_agx_
ID: 33747597
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
 

Author Comment

by:Albee_J
ID: 33747632
No problem agx, I appreciate your help!
0
 

Author Comment

by:Albee_J
ID: 33749094
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
 
LVL 52

Expert Comment

by:_agx_
ID: 33749120
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
 

Author Comment

by:Albee_J
ID: 33749151
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
 

Author Comment

by:Albee_J
ID: 33749212
It is definitely not liking this with the cfftp it seems to be ignoring it.

<cfif DirectoryExists(theDirecPath)>
0
 
LVL 52

Expert Comment

by:_agx_
ID: 33749240
>> 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
 
LVL 52

Assisted Solution

by:_agx_
_agx_ earned 500 total points
ID: 33749275
>> <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
 

Accepted Solution

by:
Albee_J earned 0 total points
ID: 33756948
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

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article  is about submitting  form through  ColdFusion.Ajax.submitForm to the action page and send a response back in JSON format which later can be decoded using ColdFusion.JSON.decode. By this way you can avoid the usual page refresh for subm…
PROBLEM:  How to open a cfwindow or run a function on double click of a cfgrid row. One of my clients wanted to be able to double click on a row item to get more detailed information about a transaction and to be able to modify the line items i…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…

717 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question