1

I have a folder structure which stores images by the thousand, take for example the following folder names;

00001-01000
01001-02000
02001-03000
03001-04000
04001-05000
05001-06000
06001-07000

I am then dynamically rendering a page to display all sorts of images. In the page i am trying to build a link to the image, but what i need is to figure out what folder an image belongs to. For instance, i am using the following to get the image id from the database

<%= rs.Fields("imageid") %>

If that were to return '04232' the folder name that would belong to would be '04001-05000'.

Is there any way that i could figure out what the folder name would be, by only having the imageid in classic asp?

  • Yes it would be a trivial expression. As a hint the start would be something like `Left(CStr(imageid),2) & "001-"` – Nick.Mc Jun 23 '18 at 14:12

1 Answers1

0

For this you need two small methods. First, padding a string:

Function PadLeft(str, padChar, desiredLength)
    Dim result
    result = CStr(str)
    Do Until Len(result)>=desiredLength
        result = padChar & result
    Loop
    PadLeft = result
End Function

And the method to find the folder name:

Function ExtractFolderName(imageFileName)
    Dim numericValue, rangeStart, rangeEnd
    numericValue = CInt(imageFileName)
    rangeStart = (Fix((numericValue / 1000)) * 1000) + 1
    rangeEnd = (Fix((numericValue / 1000)) + 1) * 1000
    ExtractFolderName = PadLeft(rangeStart, "0", 5) & "-" & PadLeft(rangeEnd, "0", 5)
End Function

To use it:

folderName = ExtractFolderName(rs("imageid"))

Basically, the method performs some math on the name to find the desired range. Note this will throw error in case of a non numeric value in the database.

Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208