5 Replies - 1681 Views - Last Post: 07 August 2009 - 02:58 PM Rate Topic: -----

#1 sjlsam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 08

cffreturn not working, cant figure out why

Post icon  Posted 29 October 2008 - 01:20 PM

the function checks a folder that has dinner1.jpg, dinner11.jpg and dinner12.jpg in it, and sending the function dinner1.jpg means it checks to see if that file exists, and tries to increment it until a filename is available for use

the function works as evident by the <cfoutput>'s remarked with DEBUG

my script always errors out saying that "Variable NEWFILENAME is undefined."
its as if the cfreturn is not returning anything

anyone have an idea as to what's wrong?

<!--- folder contains dinner1.jpg, dinner11.jpg, and dinner12.jpg --->
<cfparam name="filepath" default="D:\uploadtest\">

<cffunction name="AutoIncrement" access="public" returntype="string">

	<cfargument name="filename" type="string" required="yes" default=0>
	<cfargument name="path" type="string" required="yes" default=0>
	<cfargument name="step" type="numeric" required="no" default=1>

	<!--- Split filename into name and extension --->
	<cfset extension = listGetAt(filename, 2, '.')>
	<cfset fileroot = listGetAt(filename, 1, '.')>

	<!--- Conduct FileExists for fileroot + step, else increment by 1 and try again --->
	<cfif FileExists("#path#/#fileroot##step#.#extension#")>
		
		<!--- DEBUG ---> <cfoutput>#fileroot##step#.#extension# exists</cfoutput><br />
		
		<cfset AutoIncrement(filename=filename,path=path,step=step+1)>
		
	<cfelse>
		
		<!--- DEBUG ---> <cfoutput>#fileroot##step#.#extension# does not exist</cfoutput>
		
		<!--- filename is valid, return to the user --->
		<cfset filenametest = fileroot & step & "." & extension>
		<cfreturn filenametest>
		
	</cfif>

</cffunction>

<cfset newFileName = AutoIncrement(filename="dinner1.jpg",path=filepath)>
<br /><cfoutput>#newFileName#</cfoutput>

This post has been edited by sjlsam: 29 October 2008 - 01:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: cffreturn not working, cant figure out why

#2 sjlsam  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 10
  • Joined: 23-January 08

Re: cffreturn not working, cant figure out why

Posted 31 October 2008 - 01:01 PM

someone suggested i do the same functionality with a cfloop and it worked fine that way

View Postsjlsam, on 29 Oct, 2008 - 01:20 PM, said:

the function checks a folder that has dinner1.jpg, dinner11.jpg and dinner12.jpg in it, and sending the function dinner1.jpg means it checks to see if that file exists, and tries to increment it until a filename is available for use

the function works as evident by the <cfoutput>'s remarked with DEBUG

my script always errors out saying that "Variable NEWFILENAME is undefined."
its as if the cfreturn is not returning anything

anyone have an idea as to what's wrong?

<!--- folder contains dinner1.jpg, dinner11.jpg, and dinner12.jpg --->
<cfparam name="filepath" default="D:\uploadtest\">

<cffunction name="AutoIncrement" access="public" returntype="string">

	<cfargument name="filename" type="string" required="yes" default=0>
	<cfargument name="path" type="string" required="yes" default=0>
	<cfargument name="step" type="numeric" required="no" default=1>

	<!--- Split filename into name and extension --->
	<cfset extension = listGetAt(filename, 2, '.')>
	<cfset fileroot = listGetAt(filename, 1, '.')>

	<!--- Conduct FileExists for fileroot + step, else increment by 1 and try again --->
	<cfif FileExists("#path#/#fileroot##step#.#extension#")>
		
		<!--- DEBUG ---> <cfoutput>#fileroot##step#.#extension# exists</cfoutput><br />
		
		<cfset AutoIncrement(filename=filename,path=path,step=step+1)>
		
	<cfelse>
		
		<!--- DEBUG ---> <cfoutput>#fileroot##step#.#extension# does not exist</cfoutput>
		
		<!--- filename is valid, return to the user --->
		<cfset filenametest = fileroot & step & "." & extension>
		<cfreturn filenametest>
		
	</cfif>

</cffunction>

<cfset newFileName = AutoIncrement(filename="dinner1.jpg",path=filepath)>
<br /><cfoutput>#newFileName#</cfoutput>

Was This Post Helpful? 0
  • +
  • -

#3 garyrgilbert  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 15
  • Joined: 22-July 08

Re: cffreturn not working, cant figure out why

Posted 05 December 2008 - 09:27 AM

I know you probably already figured this out but the problem is that you have the cfreturn inside your cfif statement

<cfif checkSomethign eq true>
do something
<cflese>
do somethign else

<cfreturn>
</cfif>

what happens if it never gets to the cfelse?

It won't return and you will get an error.

At the top of your function set the variable

...function...

<cfset var filenametest =''>

... your if statement code here...

<cfreturn filnametest/>
</cffunction>



Was This Post Helpful? 0
  • +
  • -

#4 ComboAlex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-July 09

Re: cffreturn not working, cant figure out why

Posted 06 August 2009 - 12:59 PM

View Postgaryrgilbert, on 5 Dec, 2008 - 08:27 AM, said:

what happens if it never gets to the cfelse?

It won't return and you will get an error.

At the top of your function set the variable

[code]


It wasn't working because the <cfreturn> tag was inside the <cfif> statement?! I had the same problem and I suspected that it was because the <cfreturn> was placed in the <cfif> statement. However this is very strange if you are perhaps a C or Java or... programmer, because you can place the return statement anywhere in the code... well if the compiler gets to the return statement (for instance if you put an IF statement etc).

Here is my topic. Read also the comments in the code I posted.
Was This Post Helpful? 0
  • +
  • -

#5 ComboAlex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-July 09

Re: cffreturn not working, cant figure out why

Posted 06 August 2009 - 01:31 PM

Oh, and BTW... in my code (check the link to my other topic) I tested if I get to the inside of the <cfif> statement which I (the compiler) was. So I didn't see any reason that a <cfreturn> wouldn't work in there (in the <cfif> statement). This is why I am very surprised that I have to put the <cfreturn> statement at the end of the function. Odd, very odd.
Was This Post Helpful? 0
  • +
  • -

#6 ComboAlex  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 34
  • Joined: 31-July 09

Re: cffreturn not working, cant figure out why

Posted 07 August 2009 - 02:58 PM

Check out this topic here. Maybe you will find your answer there.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1