Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Incrementing file extensions

ITAITA Posts: 463Member
I'm stuck (again). Here is my problem: I need to store files which a user uploads to the server in a specified directory and with a 'sensible' name, ie one that I create. What I want to do is create a script which does all the naming for me. Basically all the files will have the same prefix, it will only be the ending which will be different. For example:

[code]
Directory Listing:
TXT1.txt
TXT2.txt
TXT3.txt
TXT4.txt
[/code]

Notice the increment (1, 2, 3, 4). How would I do this? I have made an attempt at this but I'm unsure as to how I would continue. Here are some ideas which I have so far:

[code]
1. Change to the correct directory for files.
2. Read all of the files in the directory and see what
the increment is currently at eg 4 for TXT4.
3. Rename the uploaded file to the filename which is one more than
the last eg TXT5.
4. End

2.1 Split the names of the files into separate parts, the prefix (TXT)
and the suffix (increment number). Find the highest suffix and
increment it.

3.1 Create the filename by concatenating TXT and the increment number
found, together.
[/code]

I know that Perl is good for this sort of thing, so this message is cross-posted to the Perl forum.

Many thanks for all help and suggestions.

ITA

Comments

  • ManningManning Posts: 1,621Member
    : I'm stuck (again). Here is my problem: I need to store files which a user uploads to the server in a specified directory and with a 'sensible' name, ie one that I create. What I want to do is create a script which does all the naming for me. Basically all the files will have the same prefix, it will only be the ending which will be different. For example:
    :
    : [code]
    : Directory Listing:
    : TXT1.txt
    : TXT2.txt
    : TXT3.txt
    : TXT4.txt
    : [/code]
    :
    : Notice the increment (1, 2, 3, 4). How would I do this? I have made an attempt at this but I'm unsure as to how I would continue. Here are some ideas which I have so far:
    :
    : [code]
    : 1. Change to the correct directory for files.
    : 2. Read all of the files in the directory and see what
    : the increment is currently at eg 4 for TXT4.
    : 3. Rename the uploaded file to the filename which is one more than
    : the last eg TXT5.
    : 4. End
    :
    : 2.1 Split the names of the files into separate parts, the prefix (TXT)
    : and the suffix (increment number). Find the highest suffix and
    : increment it.
    :
    : 3.1 Create the filename by concatenating TXT and the increment number
    : found, together.
    : [/code]
    :
    : I know that Perl is good for this sort of thing, so this message is cross-posted to the Perl forum.
    :
    : Many thanks for all help and suggestions.


    You could keep another file called COUNTER.TXT whose sole purpose is to tell you what the next increment is. So the process would be:

    Open COUNTER.TXT
    Read the number
    Close COUNTER.TXT
    Open COUNTER.TXT
    Write the number + 1
    Close COUNTER.TXT
    Rename uploaded file to TXT.TXT

    I'm not sure if it's more efficient than trying to determine the next increment by looking at the existing filenames, but it's definitely simple to code.
Sign In or Register to comment.