skip to Main Content

Why is only the last step in my script not working? The rest is working ok.
Error: file number 30 should have name C, but it gets name A from this statement below.
Why is this loop not renaming file 30 with C? (as is the rest renamed C correct)

Details:
I am using a script to rename files. I have 3 names in a sequence A, B and C.

The first 10 files are renamed with A
The next 10 with B
The next 10 with C
Then the next 10 again with A
Then the next 10 with B
etc.

Script/ statement


var X = 3; //sequence steps
var Y = 10; //files per sequence step

if  (counter) {

    index = (counter)%(X*Y);
    if (index <= Y)         newName += '_A_';
    else if (index <= Y*2)     newName += '_B_';
    else if (index <= Y*3)     newName += '_C_';

}

I have isolated this problem to be only in the last number of the last sequence step (C in this example). The rest is working ok.

2

Answers


  1. try:

    var X = 3; // sequence steps
    var Y = 10; // files per sequence step
    
    if (counter) {
        index = (counter - 1) % (X * Y);
    
        if (index < Y) {
            newName += '_A_';
        } else if (index < Y * 2) {
            newName += '_B_';
        } else if (index <= Y * 3) {
            newName += '_C_';
        }
    }
    
    Login or Signup to reply.
  2. You did not post your code for what counter is. You should update your question to include this as it is pretty important for what went wrong. I will update my answer if you do this.

    I will say that you should update your code in a few ways to be clearer, cleaner, and more modern. First, use const or let instead of var. Second, name your variables something useful, not just single letters or abbreviations. This will make your code much easier to read. Next, when doing anything other than perhaps a single if statement, always use brackets in your if/else statements.

    const sequenceSteps = 3;
    const maxFilesPerSequence = 10;
    
    if (counter) {
        const index = counter % (sequenceSteps * maxFilesPerSequence);
      
        if (index <= maxFilesPerSequence) {
            newName += '_A_';
        } else if (index <= 2 * maxFilesPerSequence) {
            newName += '_B_';
        } else if (index <= 3 * maxFilesPerSequence) {
            newName += '_C_';
        }
    }
    

    Doing it this way will look much cleaner and help you and others to spot problems. Please update your question to include where you defined the count variable with the loop code and then I will be able to update this answer with a more clear solution.

    You may also consider adding console.log() statements inside the loop to log the value of index and counter in order to see what those values are when the problem is occurring.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search