Skip to main content
Solved

Scripted timestamp parameter


davideagle
Contributor
Forum|alt.badge.img+19

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

Best answer by takashi

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
View original
Did this help you find an answer to your question?

6 replies

takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • Best Answer
  • March 12, 2014
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

david_r
Evangelist
  • March 12, 2014
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

david_r
Evangelist
  • March 12, 2014
Good catch on the colons, Takashi!

davideagle
Contributor
Forum|alt.badge.img+19
  • Author
  • Contributor
  • March 12, 2014
Gentlemen - spectacular! Thank you both VERY much for the rapid service!

 

Cheers, Dave

takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • March 13, 2014
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

 

-----

 


salvaleonrp
Enthusiast
Forum|alt.badge.img+15
  • Enthusiast
  • January 14, 2020

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

 


Cookie policy

We use cookies to enhance and personalize your experience. If you accept you agree to our full cookie policy. Learn more about our cookies.

 
Cookie settings