How to convert filename to string, change it, then make it filename again?

edited August 2016 in Library Questions

I have video files in folder, they are named with numbers 1001.mov, 1002.mov, 1003.mov etc How can I extract the number part, then change it and make that new value filename again?

I was reading about arrays, and some examples with path manipulation, but I can't work out anything. :/

Answers

  • I am sorry, but I don't understand your hint. Could you explain it in short to me?

  • Problem is I can't figure out exactly what you're asking. What needs to be renamed: the file itself or just its string name in the sketch? :-??

  • I have video files in folder (let's say 100 videos), and I have to play them in random order, but I must know in every moment which exactly file is playing because if command happens, I have to jump to specific file for that one that is playing (eg 1001.mov is playing, command A happens and I have to jump to 3001.mov).

    My professor told me that I should take that number from filename and make it string, so that I can randomize numbers and in that way I can make files play in random order.

    I've found some some other "play random video" ideas here, but none of them is with this filename to string - to number - to string- to filename.

    I am confused.

  • (eg 1001.mov is playing, command A happens and I have to jump to 3001.mov).

    By "command A" you mean hit key A? Well, I don't think there are enough keys to press for 100 files! :-\"

  • There are 4 commands that can happen, and for every command there is a file from the group... when command A happens, you jump from currently playing file 1xyz.mov , to file 2xyz.mov, when command B happens, ... to 3xyz.mov, and C ..... 4xyz.mov, D ... 5xyz.mov So for A command file will always start with 2. eg 1056.mov is playing, A happens - jump to 2056.mov

    So just 4 command keys, not 100 :P

  • edited August 2016

    So you wanna group movie files according to their String name's 1st number char.

    We can get any char from a String via method charAt():
    https://processing.org/reference/String_charAt_.html

    But 1st off, you're gonna need to collect all ".mov" files from the "data/" subfolder before grouping them. For that initial task, I recommend FilenameFilter:

    import java.io.FilenameFilter;
    
    final FilenameFilter MOV_FILTER = new FilenameFilter() {
      @ Override boolean accept(final File dir, String name) {
        return name.toLowerCase().endsWith(".mov");
      }
    };
    

    Then use File's listFiles() method passing the FilenameFilter object as its argument:

    File folder = dataFile("");
    println(folder);
    
    File[] movs = folder.listFiles(MOV_FILTER);
    println("# of files found at folder above:", movs.length, ENTER);
    printArray(movs);
    println();
    
  • I couldn't specify the path from where files will be taken from, so I need that filename filter? Ok, but what about those three digits that have to be randomized?

    And what should I do with that string problem? Should I trim extension from filename, take that string and use Integer.valueOf(number) to make it integer?

  • edited August 2016

    ... so I need that filename filter?

    No, it's merely a recommendation.

    ... I couldn't specify the path from where files will be taken from,

    Recommended place fro any resource files in within sketch's subfolder "data/".
    Of course, if you wanna grab from something else, you're gonna need the full path of it.

    And what should I do with that string problem?

    1st things 1st! Write the code to grab all the needed files wherever they are.
    Once you've got that covered, you can think about grouping them split by 4.

Sign In or Register to comment.