Skip to main content
Question

copy a file with a simple script


Forum|alt.badge.img

Hi,

I search for a simple way to do this:

- copy a file named "test.xlsx" as "test2.xlsx" in the same directory and

- copy the file "test2.xlsx" obtained as "test2 - 20180725.xlsx" in the same directory

with a simple script at the end of a workbench

thank you

11 replies

pratap
Contributor
Forum|alt.badge.img+11
  • Contributor
  • July 25, 2018

ebygomm
Influencer
Forum|alt.badge.img+38
  • Influencer
  • July 25, 2018

Does it need to be a script?

You can use the file copy writer to copy and rename files at the end of your workspace


Forum|alt.badge.img
  • Author
  • July 25, 2018
ebygomm wrote:

Does it need to be a script?

You can use the file copy writer to copy and rename files at the end of your workspace

I don't understand how to use this writer to do the job. At the end of my workbench i use a FeatureWriter to write my "test.xlsx" file in my directory and after this, how can i connect the filecopy writer after the featurewriter

 

 


ebygomm
Influencer
Forum|alt.badge.img+38
  • Influencer
  • July 25, 2018
philippe33 wrote:
I don't understand how to use this writer to do the job. At the end of my workbench i use a FeatureWriter to write my "test.xlsx" file in my directory and after this, how can i connect the filecopy writer after the featurewriter

 

 

Are you using a 2018 version of FME?

 

 


pratap
Contributor
Forum|alt.badge.img+11
  • Contributor
  • July 25, 2018

Hi,

Use another feature writer with FileCopy

Place necessary attributes with attribute creator in between

untitled.jpg


Forum|alt.badge.img
  • Author
  • July 25, 2018
ebygomm wrote:
Are you using a 2018 version of FME?

 

 

No. I use a 2016 version

 

 


ebygomm
Influencer
Forum|alt.badge.img+38
  • Influencer
  • July 25, 2018
philippe33 wrote:
No. I use a 2016 version

 

 

A system caller after the featurewriter might be more straightforward in that case

 

 

e.g.

 

 

 


paalped
Contributor
Forum|alt.badge.img+5
  • Contributor
  • July 25, 2018

If you really want it in a script you could write something like this:

import os
import shutil
from datetime import date


today = date.today()
filename = FME_MacroValues['DestDataset_XLSXW']
path, fname = os.path.split(filename)
basename, extension = fname.split('.')
newfname = f'{basename} - {today.strftime("%y%m%d")}.{extension}'
newfilename = os.path.join(path,newfname)
shutil.copy(filename, newfilename)

its in python 3.6 btw.

for python 2.7 

newfname = '{} - {}.{}'.format(basename,today.strftime("%y%m%d"),extension)


Forum|alt.badge.img
  • Author
  • July 25, 2018
ebygomm wrote:
A system caller after the featurewriter might be more straightforward in that case

 

 

e.g.

 

 

 

Hi,

 

The systemCaller give me that answer in the log :

 

 

SystemCaller: Executing `copy /Y C:\\Livraison AIXM 51 V2.0\\01 - Ecriture_mid_uuid/test.xlsx C:\\Livraison AIXM 51 V2.0\\01 - Ecriture_mid_uuid/tst2.xlsx'

 

2018-07-25 14:16:58| 17.1| 0.0|WARN |SystemCaller: Failed to Execute `copy /Y C:\\Livraison AIXM 51 V2.0\\01 - Ecriture_mid_uuid/test.xlsx C:\\Livraison AIXM 51 V2.0\\01 - Ecriture_mid_uuid/tst2.xlsx'

 

 

probably it doesn't succeed to write because it's a "/" and it needs a "\\" before "tst2.xlsx"

 


Forum|alt.badge.img
  • Author
  • July 25, 2018
Please have a look to my picture to understand where is the issue:

 

 

ima1.png

 

 

I suceed by the "attributecreator" way to copy "test.xlsx" into "tst2.xlsx" using a private attribute "dir" which is obtained with a python script

 

 

ima2.png

 

 

and here is the log for this way

 

 

ima3.png

 

 

Why is it wrong with the systemcaller ???

 

 

This way allows to change the initial place of my global work. If i put the initial folder "Livraison AIXM 51 V2.0" containing all the subfolders in another folder (ie c:\\ or c:\\users\\desktop) it works

 

 

Why not with systemcaller ?

 


paalped
Contributor
Forum|alt.badge.img+5
  • Contributor
  • July 26, 2018
Try: copy "$(dir)\\test.xlsx" "$(dir)\\tst2.xlsx"

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