How to save keyboard input as multiple .txt files using timestamp in naming convention

edited December 2017 in Questions about Code

I'm at the start of an ambitious (for me) project in Processing 2 for my MA in Creative Media. I want to create a sort of chain novel where users add to a long continuous narrative, but each user's input is saved as an individual text file and printed on separate index cards.

My issue (first of many presumably) is when saving the individual .txt files which are a record of each user's input, I want the filename to contain the timestamp.

I've used textFile = textFile = createWriter(timestamp()+".txt"); as a placeholder for the moment, but this obviously isn't a 'thing'...

This is what I have so far (Code adapted from examples by Amnon available here: https://amnonp5.wordpress.com/2012/01/28/25-life-saving-tips-for-processing/ and Daniel Shiffman available here: http://www.learningprocessing.com/examples/chapter-18/example-18-1/:):

    String myText = "Give me your story.";
    String yourText = ""; // Variable to store text currently being typed
    String savedText = ""; // Variable to store saved text when control is hit
    PrintWriter textFile;

    void setup() {
      size(500, 500);
      textAlign(CENTER, CENTER);
      textSize(30);
      fill(0);
      // Create a new file in the sketch directory
      textFile = createWriter(timestamp()+".txt");
    }

    void draw() {
      background(255);
      text(myText, 0, 0, width, height);
      text(yourText, 0, 0, width, height);
      text(savedText, 0, 0, width, height);
    {
      textFile.println(savedText);
      textFile.flush();
      textFile.close();
     } 
      }

    void keyPressed() {
      if (keyCode == ENTER) {
        myText="";}
      if (keyCode == BACKSPACE) {
        if (yourText.length() > 0) {
          yourText = yourText.substring(0, yourText.length()-1);
      }
      } 
      else if (keyCode == DELETE) {
        yourText = "";
      } 
      else if (keyCode != SHIFT && keyCode != CONTROL && keyCode != ALT) {
        yourText = yourText + key;
      }
        // If the Control key is pressed, save the String and clear it
      if (key == CODED) 
      {
        if (keyCode == CONTROL) {
        savedText = yourText;
        // Text is cleared
        yourText = ""; 
      } else {
        // Otherwise, concatenate the String
        // Each character typed by the user is added to the end of the String variable.
        yourText = yourText + key; 
      }
    }
    }

As you can tell I am brand-new to code and what's above is not an elegant piece of code (and probably making your eyes bleed). If you have any suggestions for making it better, or a better alternative than timestamp for naming the multiple .txt files, please let me know. Thanks in advance.

Answers

  • Answer ✓

    In the Reference page, search for second(). You have also hour(), minute(), etc. You can format them however you want (nf() can help there), eg. to make a date like 2015-03-24--06-57-11 which is good for sorting, too.

  • Thanks PhiLho! First of many. :)

  • edited December 2017 Answer ✓

    Also take a look at the example I've posted here: :P
    http://forum.processing.org/two/discussion/10067/how-to-prevent-file-overwriting

    // Forum.Processing.org/two/discussion/10067/how-to-prevent-file-overwriting#Item_3
    // Docs.Oracle.com/javase/8/docs/api/java/text/SimpleDateFormat.html
    
    static final String PATTERN = "yyyy-MMM-dd.HH_mm_ss", EXT = ".log";
    String timeStamp, logSavePath;
    
    void setup() {
      timeStamp = getTimeStamp(PATTERN);
      logSavePath = dataPath(timeStamp) + EXT;
    
      println(timeStamp);
      println(logSavePath);
    
      exit();
    }
    
    static final String getTimeStamp(String pattern) {
      return new java.text.SimpleDateFormat(pattern).format(new java.util.Date());
    }
    
  • @GoToLoop Thanks so much for this. Really appreciate it. Will give it a whirl. :)

Sign In or Register to comment.