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 8489575
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T21:51:59+00:00 2026-06-10T21:51:59+00:00

I need your help. I’d like to be able to have a file naming

  • 0

I need your help.

I’d like to be able to have a file naming system that would detect if the filename exists and if it does automatically add a number at the end of it. Starting at 2

ie.

var myString = "2011-1234567";

myString = myString + "-2";

if (2011-1234567-2 already exists) then output new file number as: 2011-1234567-3 

so id like to ideally be able to create a function that would automatically add a number at the end of it if the filename already exists

  • 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-10T21:52:01+00:00Added an answer on June 10, 2026 at 9:52 pm
    var exists = 0
    function file_exists(name) {
       // replace with something suitable for your environment
       exists = 1 - exists 
       return exists
    }
    
    function new_name(suggested) {
       // just return back new name if it available
       if (!file_exists(suggested)) { return suggested }
       // try to split name to "base" and "index" parts
       var have_index = suggested.match(/^(.+)\-(\d+)$/)
       var unused_index
       if (have_index && have_index[2]) {
          base = have_index[1]
          unused_index = ++have_index[2]
       } else {
          // use entire name and start from index 2 if not found
          base = suggested
          unused_index = 2
       }
       // loop until you find next free index
       while (file_exists(base  + "-" + unused_index)) { unused_index++ }
       // ... and return result
       return base  + "-" + unused_index
    }
    

    Running new_name("tommy"), gives “tommy-2”. new_name("tommy-2") – “tommy-3”, etc. Of course you need to define your own vision of “exists” in file_exists function.

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

Sidebar

Related Questions

Need your help with my PHP/MYSQL array. I have a php script that selects
I need your help :/ I have a register that points to one memory
I need your help. I have a C executable called generator.out that is a
i need your help in access db i have a table like this ID
I am a bit confused and need your help. I have a database that
I'm a beginner and I really need your help! I have an XML file
Really need your help. I have a table that can dynamically add and delete
I need your help with a RegEx in PHP I have something like: vacation.jpg
I need your help. I tried to play an audio file stored in Assets
I need your help. Say I have a code inside a for statement similar

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.