LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Renaming files with moveFile, adding date & time to filename

    Employee
    Posted 06-28-2017 07:10

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: linnoinen

    Hi everyone,

    I'm puzzled by Lavastorm's (and BRAINscript's) behaviour when it comes to modifying filenames and renaming them.

    What I'm trying to achieve is pretty simple: write a file to a folder, rename it, and add the current date and time to the filename, converting it to .DAT file format.
    Looking through the previous posts on moveFile() here, I found a BRAINscript snippet which lets me do almost this -- except when I add the time to the filename, the BRAINscript error says it can't find my file.

    My BRAINscript for the Filter node looks like this:
    oldFile = "C:\\tmp\\test.csv"
    
    extList = oldFile.split(".")
    n = extList.len()
    ext = "." + extList[n-1]
    
    dirAndFile = oldFile.replace(ext,"")
    
    newFile = dirAndFile + "_" + date().str() + ".dat"
    
    moveFile(oldFile,newFile)
    
    consume(1,TRUE)
    
    emit newFile
    The above code takes the input file, splits off the "csv" and adds the date() as a string, then adds .dat as the extension. Works like a charm.

    What alludes me is why doesn't adding and extra "time().str()" to the same line work? For example:

    newFile = dirAndFile + "_" + date().str() + "_" + time().str() + ".dat"
    If you change the code and run it, the error will be:

    Node execution terminated while processing data by error:
    error moving file C:\tmp\test.csv: The filename, directory name, or volume label syntax is incorrect.
    Also, even trying something like creating separate variables from date and time, and adding them to the filename does not work either.

    Super frustrating!

    Thanks!
    Karri


  • 2.  RE: Renaming files with moveFile, adding date & time to filename

    Employee
    Posted 06-28-2017 07:20

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: stonysmith

    If you use time.str(), you produce a string that contains colons such as 12:34:56
    Windows won't allow you to have a file name that contains colons.

    c:\temp_2017-06-28_12:34:56.csv
    is an invalid filename.

    use time.str().replace(":","") to remove the colons.


  • 3.  RE: Renaming files with moveFile, adding date & time to filename

    Employee
    Posted 06-28-2017 23:17

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: linnoinen

    Oh, of course!
    Thanks a million!

    One additional thing I've been tackling regarding filenames is running numbers.
    If I wanted to add the date and a running number to the filename, i.e 28-06-2017_001.csv, and then make that number grow by 1 after every run, is there a node / function that let's me do that?
    The problem I guess is that the previous runs need to be stored somewhere so that Lavastorm knows what the previous number was.

    /Karri