Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7840721
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T15:53:23+00:00 2026-06-02T15:53:23+00:00

I’m still trying to get my grips on Coldfusion… I need to create a

  • 0

I’m still trying to get my grips on Coldfusion…

I need to create a directy of files (say there are 10 files) and output 5 random files. Getting and outputting files is ok, but I’m not sure where to fit in the randrange. Here is my code:

 <cfdirectory action="list" directory="#expandpath("img/")#" filter="some*.*" name="dir">
     <!--- imgID --->
     <CFSET imgID= #RandRange(1, #dir.allRecords#)#>
     <!--- this only grabs the first 5 files --->
     <cfoutput query="dir" maxrows="5">
        <cfif FileExists("#expandpath("img/#name#")#")>
        <cfimage source="#expandpath("img/#name#")#" name="myImage">                                                   <cfif IsImage(myImage) is true>
          <cfset ImageSetAntialiasing(myImage,"on")>
              <cfset ImageScaleToFit(myImage,"highestQuality")>
              <!--- append to a list --->
          <li><cfimage source="#myImage#" action="writeToBrowser"></li>
           </cfif>               
        </cfif>
      </cfoutput>   

This works ok in displaying the first 5 images. However, I would like to have 5 random images.

Thanks for some insights!

EDIT:
This is how I ended up doing it – ONE QUESTION UNSOLVED –

<!-- get the directy, listinfo="name" because I only need filenames --->
<cfdirectory action="list" LISTINFO="name" directory="#expandpath(" logos/")#" filter="marke*.*" name="dir">

 <cfset images=[ ]>
 <!-- since dir is not indexable, like dir[pos], I need another array!-->
 <cfset dirArr=[ ]>
 <cfset blocker="false">
 <cfset maxLogos=5>
 <!-- fill new dirArr(ay) -->               
 <cfoutput query="dir">
    <cfset #ArrayAppend(dirArr, #expandpath( "logos/#name#")#)#>
 </cfoutput>
 <!-- loop -->
 <cfloop condition="blocker eq false">
    <-- random position -->
    <cfset pos=R andRange(1, #dir.recordcount#)>
    <cfif #dir.recordcount# eq 0 OR #ArrayLen(images)# gte #maxLogos#>
        <-- STOP loop -->
        <cfset blocker="true">
    </cfif>
    <cfset ArrayAppend(images, #dirArr[pos]#)>
    <!-- BROKEN unknown ARRAYDELETE --> 
    <!--- <cfset ArrayDelete(dirArr, #dirArr[pos]#)> --->
    <!-- IMG -->
    <cfimage source="#dirArr[pos]#" name="myImage">
    <cfif IsImage(myImage) is true>
        <cfoutput>
        <li data-icon="false">
           <cfimage source="#myImage#" action="writeToBrowser">
        </li>
        </cfoutput>
    </cfif>
 </cfloop>

The problem is the ArrayDelete does not work variable ARRAYDELETE is undefined, Coldfusion(8) tells me. Any idea what I’m doing wrong?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T15:53:26+00:00Added an answer on June 2, 2026 at 3:53 pm

    I’m not sure if your code will actually work as there appears to be several syntactical errors in it. Also you’re doing a directory list on img but then pulling images from logos and you’ve not made it clear what the relationship is between these directories

    those issues aside, here is how i would handle this.

    <cfscript>
    // this code is untested, but should get you going
    // get list of image file names as an array
    dir = directoryList(expandPath("imgs"), false, "name", "*.jpg");
    images = [];
    while(true) {
      // if out directory list is now empty or we have 5 results, we're done
      if(!arrayLen(dir) or arrayLen(images) gte 5) break;
      // get an image from a random point in the list
      pos = randrange(1, arrayLen(dir));
      // append it to our images array
      arrayAppend(images, dir[pos]);
      // delete form the source array, this avoids duplicates in further iterations
      arrayDeleteAt(dir, pos);
    }
    </cfscript>
    

    This gives you an array of images, with between 0 and 5 elements, which you can then output as a list.

    As a side note, its not advisable to use <cfimage> and related functions repeatedly. If you need to resize or manipulate an image you should then cache it back to disk rather than repeating the manipulation every request.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have thousands of HTML files to process using Groovy/Java and I need to
I'm trying to create an if statement in PHP that prevents a single post
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to render a haml file in a javascript response like so:
In my XML file chapters tag has more chapter tag.i need to display chapters
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to select an H1 element which is the second-child in its group

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.