Skip to main content
Question

Can you sort files into folders using specific text in their file name?


Hi

I have a large number of files that are formatted like so:

AA0001

AA0002

AA0003

AB0001

AB0002

AB0003

AC0001

AC0002

AC0003

Is there a way to take all of these files and sort them into their respective folders using the text prefix, for example all the AA files go into a folder call AA, the AB's in AB and AC's into AC?

I have a TestFilter that will do using a @Value(fme_basename) CONTAINS Test Condition for each text prefix, the output for each of these going to it's own writer, and this does work however it seems messy. Is there a better way to do this?

Thanks.

11 replies

Forum|alt.badge.img

You can use substring extractor to pull out x number of letters from fme_basename (after exposing it). This creates an attribute called _substring which you can then fanout on


erik_jan
Contributor
Forum|alt.badge.img+17
  • Contributor
  • June 23, 2017

You can use the "Directory and File Path" reader to read all file names in the folder.

Then feed these (based on the required sub string) to a "File Copy" writer to copy (or move) the files to different folders.

This way you do not need to read the contents of the files (which you do by using the fme_basename).


erik_jan wrote:

You can use the "Directory and File Path" reader to read all file names in the folder.

Then feed these (based on the required sub string) to a "File Copy" writer to copy (or move) the files to different folders.

This way you do not need to read the contents of the files (which you do by using the fme_basename).

So I've done this but it's just making the folders, not copying any of the files across. What am I missing?

 

 


mark2atsafe
Safer
Forum|alt.badge.img+43
jamesclarke wrote:
So I've done this but it's just making the folders, not copying any of the files across. What am I missing?

 

 

Can you post a workspace or at least a screenshot? It's going to be hard to diagnose without. Any messages in the log window give a clue?

 

 


mygis
Contributor
Forum|alt.badge.img+12
  • Contributor
  • June 23, 2017
Question: do all the folders exist?

 

 


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • June 23, 2017
jamesclarke wrote:
So I've done this but it's just making the folders, not copying any of the files across. What am I missing?

 

 

It's difficult to diagnose without information about how you've designed the workspace, but I suppose most likely you have not connected source file path attribute between the source side and the writer feature type.

 


takashi wrote:
It's difficult to diagnose without information about how you've designed the workspace, but I suppose most likely you have not connected source file path attribute between the source side and the writer feature type.

 

This is what I have so far. It creates the folders that I want but doesn't actually copy any files over. I have managed to get it to copy all the files into each of the folders but that's not much use since it just duplicates them, it doesn't actually sort them.

 

 


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • June 26, 2017
jamesclarke wrote:
This is what I have so far. It creates the folders that I want but doesn't actually copy any files over. I have managed to get it to copy all the files into each of the folders but that's not much use since it just duplicates them, it doesn't actually sort them.

 

 

You have to inform the File Copy writer the path of source files to be copied. Try connecting between "path_unix" (or "path_windows") of the source side and "filecopy_source_dataset" of the File Copy writer feature type.

 


ebygomm
Influencer
Forum|alt.badge.img+31
  • Influencer
  • June 26, 2017

You need to create the attributes for the filecopy writer, something like the above will copy the relevant files to subfolders in the same parent folder. If you want to copy them somewhere else you will need to do a bit more manipulation to create the correct destination


takashi
Contributor
Forum|alt.badge.img+19
  • Contributor
  • June 26, 2017
jamesclarke wrote:
This is what I have so far. It creates the folders that I want but doesn't actually copy any files over. I have managed to get it to copy all the files into each of the folders but that's not much use since it just duplicates them, it doesn't actually sort them.

 

 

This image illustrates my intention. There could be other possible configurations.

 

 


jamesclarke wrote:
This is what I have so far. It creates the folders that I want but doesn't actually copy any files over. I have managed to get it to copy all the files into each of the folders but that's not much use since it just duplicates them, it doesn't actually sort them.

 

 

Thank you Takashi, that's what I was looking for, it copied everything to the correct folders. I knew it was something simple I was just overlooking.

 

 


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