Question

Moving file from one location to another

  • 25 March 2019
  • 4 replies
  • 26 views

Badge

Hi All,

I wish to move a file from one folder to another. I have tried using FileCopy Transformer initially but that isn't working properly. It works fine on a single run whenever installed fresh but isn't working fine on consecutive runs. It is only copying the data from one folder to another.

I now resort to create a python script which can be called via a python caller or used as a shutdown script.. Can anyone recommend me a python script for moving a file from one location to another or instead a delete from the location?

Thanks.


4 replies

Badge

Hi @asabhinavgreats,

I would give a shutdown-script a try. I wanted to copy the log-file from one location to another.

Therefore, I created two private parameters [logdatei]:

import time

# time.localtime() returns the current time as a Python object

 

# time.strftime gives a textual representation of the date

 

# %Y = Year, %m = month, %d = day, etc.

 

# FME_MacroValues['FME_MF_NAME'] ; siehe links die system parameters

 

#aus dem Workbenchnamen, der auch die Extension "fmw" enthält, wird nur der erste Teil (Element 0) weiterverwendet

 

workbenchname = FME_MacroValues['FME_MF_NAME'].split('.')[0]

 

logname = workbenchname+'_'+ time.strftime('%Y-%m-%d_%H-%M-%S', time.localtime())+'.log'

 

return logname

and

[ziel], here I created a new destination folder ../YYYYMMDD:

# import fme and other modules

 

import fme,time,os

 

from os import path

# Filepath hardcoded to O:\\00_PYTHON

 

#startpfad = 'O:\\\\00_PYTHON'

 

startpfad = FME_MacroValues['_startpfad']

# time.localtime() returns the current time as a Python object

 

# time.strftime gives a textual representation of the date

 

# %Y = Year, %m = month, %d = day, etc.

 

textdate = time.strftime('%Y%m%d', time.localtime())

 

newdir = startpfad + '\\\\' + textdate

 

return newdir

The shutdownscript is pretty simple:

# import fme and other modules

 

import fme

 

import shutil

 

import os

file2move=FME_MacroValues['logdatei']

 

destination=FME_MacroValues['ziel']

 

shutil.move(file2move,destination)

Hope that helps!

 

 

 

 

 

Userlevel 2
Badge +16

Another option would be using the SystemCaller and use DOS commands to move or remove files.

Badge +2

Hi @asabhinavgreats,

What are you parameter settings in the File Copy Writer, in particular, is the File Operation=Move ?

This writer should be satisfactory to perform what you want to achieve without using a python script. If the Writer parameters are set up with the 'move' function rather than the default 'copy' but this still appears to only be copying over the files then please consider reaching out to our support team so we can investigate this issue further for you: https://www.safe.com/support/

Badge

I have used a Directory and File Pathnames Reader along with a FileCopy Writer with filename value as nul as a test. That worked for me. Thanks guys for your input.

Reply