Solved

Scripted timestamp parameter

  • 12 March 2014
  • 6 replies
  • 23 views

Badge +14

I've used a method in the past with a WorkspaceRunner to pass a timestamp value into the log file name parameter of a second workspace to ensure that I always get a unique logfile for each rn of the same workspace. But, that's only any good when I can trigger the process in this way. Right now, what I want to be able to do is to use a scripted parameter ideally to generate a unique log file name that is say a concatenation of the workspace name and the start time so like: FMEBatchRun_2014-03-12T12:01:52.log.

 

 

Is a scripted parameter the best option for this and if so, can anyone help me with the syntax as I'm not much good (any good really) at writing python...

 

 

Thanks, Dave
icon

Best answer by takashi 12 March 2014, 11:41

View original

6 replies

Userlevel 2
Badge +17
Hi Dave,

 

 

This is an example. Colons (:) cannot be used in a file name.

 

-----

 

# Scripted (Python) Parameter Example

 

import time

 

d = FME_MacroValues['FME_MF_DIR'] # directory path

 

w = FME_MacroValues['FME_MF_NAME'] # workspace name

 

t = time.strftime('%Y%m%dT%H%M%S') # timestamp

 

return '%s%s_%s.log' % (d, w, t)

 

-----

 

 

Hope this helps.

 

 

Takashi
Userlevel 4
Hi Dave,

 

 

I'd say a scripted parameter is perfect for this. Here's a Python script that should do the trick:

 

 

---

 

import fmeobjects

 

from datetime import datetime

 

 

now = datetime.now().strftime('%Y-%m-%dT%H:%M:%S')

 

workspace = FME_MacroValues['FME_MF_NAME']

 

 

return workspace + "_" + now + ".log"

 

---

 

 

David
Userlevel 4
Good catch on the colons, Takashi!
Badge +14
Gentlemen - spectacular! Thank you both VERY much for the rapid service!

 

Cheers, Dave
Userlevel 2
Badge +17
P.S. The workspace name (FME_MF_NAME) generally ends with the extension ".fmw", it may be better to remove it. And the created log file path may conflict with an existing file even if it contains time stamp, when the previous job was completed in less than 1 second. I tried improving the script in consideration of them. For your information.

 

-----

 

# Scripted (Python) Parameter Example

 

# Create log file path with time stamp.

 

import time, os

 

d = FME_MacroValues['FME_MF_DIR'] # directory path

 

w = FME_MacroValues['FME_MF_NAME'] # workspace name

 

t = time.strftime('%Y%m%dT%H%M%S') # time stamp

 

 

# If the workspace name ends with ".fmw", remove it.

 

if w.endswith('.fmw'):

 

    w = w[:-4]

 

 

# If the expected path conflicts with an existing file,

 

# append sequential number to avoid overwriting it.

 

base = '%s%s_%s' % (d, w, t)

 

path = '%s.log' % base

 

n = 1

 

while os.path.exists(path):

 

    path = '%s_%d.log' % (base, n)

 

    n += 1

 

return path

 

-----

 

Badge +10

Instead of using the workspace filename, can I use the ProcessID instead?

 

Reply