# Code to pass file names into metadata from data reader

**URL:** <https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263>\
**Category:** CloverDX Platform\
**Created:** [August 27, 2013, 12:00am UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263 "2013-08-27T00:00:00Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![nhanson](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nhanson](https://forum.cloverdx.com/u/nhanson)\
**Post date:** [August 27, 2013, 12:00am UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/1 "2013-08-27T00:00:00Z")

</div>

Hi

I would like to read the filename(s) of input file(s) used in my data reader component and pass the filename through as an additional field using a subsequent reformat component - could anyone provide me with a simple example of how this can be done? Example of the filename structure would be FILENAME\_20130823.txt

Thanks in advance

Nikki

---

<div class="post-metadata">

**Author:** ![slechtaj](https://avatars.discourse-cdn.com/v4/letter/s/59ef9b/32.png) [@slechtaj](https://forum.cloverdx.com/u/slechtaj)\
**Post date:** [August 27, 2013, 2:06pm UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/2 "2013-08-27T14:06:14Z")

</div>

Hi Nikki,

This can be easily done using the metadata connected to reader’s output port. All you need to do is to edit the metadata, add new field and set its _Autofilling_ property to “source\_name” (please see attached screenshot). This will extract the source name (the File URL from your reader) and place it to a particular record. I have created a short sample for you, so please find it attached below.

- [readingFilenames.zip](https://cdn.cloverdx.com/forum/16494%5F1d9dab59f59422abe5fef1f4d2c84d89/readingFilenames.zip)
- [autofilling\_source\_name.png](https://cdn.cloverdx.com/forum/16494%5F05cabaf6822a1a8a3bb8caa58efef762/autofilling%5Fsource%5Fname.png)

---

<div class="post-metadata">

**Author:** ![nhanson](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nhanson](https://forum.cloverdx.com/u/nhanson)\
**Post date:** [September 11, 2013, 8:45am UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/3 "2013-09-11T08:45:10Z")

</div>

Hi Jan,

Many thanks for this.

Is there a way to reformat the file\_name value to remove the file directory details from the field so that you are left with only the file name and not the whole route path plus the file name?

Thanks again  
Nikki

---

<div class="post-metadata">

**Author:** ![imriskal](https://avatars.discourse-cdn.com/v4/letter/i/b38774/32.png) [@imriskal](https://forum.cloverdx.com/u/imriskal)\
**Post date:** [September 12, 2013, 12:19pm UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/4 "2013-09-12T12:19:58Z")

</div>

Hi Nikki,

Unfortunately, we do not provide this option directly. You can either split the value by “/” character (or “\” in case of OS Windows) and take only the value after the last slash or (if you have CloverETL Server and jobflows) you can use ListFiles component which provides information about both filename and filename + path.

Kind regards,

---

<div class="post-metadata">

**Author:** ![nhanson](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nhanson](https://forum.cloverdx.com/u/nhanson)\
**Post date:** [September 12, 2013, 4:24pm UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/5 "2013-09-12T16:24:02Z")

</div>

Hi Lubos,

I’m sorry but my clover skills aren’t that good so I don’t know how to strip out the file path using the “/” or “\” values?

If an example path and filename were “C:\NH\KPI2\Requests\Reactive\New\Filename123.csv” could you provide an example of what the function would be to strip out the “\” values and get to the last part?

Thanks  
Nikki

---

<div class="post-metadata">

**Author:** ![David\_Hlidek](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@David\_Hlidek](https://forum.cloverdx.com/u/David_Hlidek)\
**Post date:** [September 13, 2013, 5:46am UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/6 "2013-09-13T05:46:38Z")

</div>

Dear Nikki,

you may extract filename by this CTL2 code:

```auto

$out.0.field1 = find('C:\NH\KPI2\Requests\Reactive\New\Filename123.csv', '([^/\\]+)$')[0];

```

It should be platform independent. If you need this functionality in more places you can define function ([http://doc.cloveretl.com/documentation/ … -ctl2.html](http://doc.cloveretl.com/documentation/UserGuide/index.jsp?topic=/com.cloveretl.gui.docs/docs/functions-ctl2.html)) and import ([http://doc.cloveretl.com/documentation/ … -ctl2.html](http://doc.cloveretl.com/documentation/UserGuide/index.jsp?topic=/com.cloveretl.gui.docs/docs/import-statement-ctl2.html)) into your transformations.

Function would look like:

```auto

//#CTL2

function string extractFileName(string path) {
	string[] res = find(path, '([^/\\]+)$');
	
	if (res.length()<1) {
		raiseError("Cannot find filename in path: "+path);
	}
	
	return res[0];
}

function integer generate() {

	$out.0.field1 = extractFileName('C:\NH\KPI2\Requests\Reactive\New\Filename123.csv');

	return ALL;
}

```

I hope this helps.

---

<div class="post-metadata">

**Author:** ![nhanson](https://avatars.discourse-cdn.com/v4/letter/n/d9b06d/32.png) [@nhanson](https://forum.cloverdx.com/u/nhanson)\
**Post date:** [September 13, 2013, 9:53am UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/7 "2013-09-13T09:53:22Z")

</div>

That’s perfect!

Thank you for your help!

Nikki

---

<div class="post-metadata">

**Author:** ![vachah](https://avatars.discourse-cdn.com/v4/letter/v/a8b319/32.png) [@vachah](https://forum.cloverdx.com/u/vachah)\
**Post date:** [November 15, 2013, 4:42pm UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/8 "2013-11-15T16:42:17Z")

</div>

Hello, I tried to use the example and it worked really well. I would like to ask you about some explanation of usage of the last part - ‘([\\]+)$’)[0]  
I understand the first part but I’m not able to reuse this type of regular expression and I was not able to find deeper detail in manual. Thanks s lot.

---

<div class="post-metadata">

**Author:** ![David\_Hlidek](https://avatars.discourse-cdn.com/v4/letter/d/58f4c7/32.png) [@David\_Hlidek](https://forum.cloverdx.com/u/David_Hlidek)\
**Post date:** [November 19, 2013, 7:11am UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/9 "2013-11-19T07:11:31Z")

</div>

Hi,

the regex was actually:

```auto
([^/\\]+)$

```

And its semantic is: take one or more non-slash and non-backslash chars from end of string. So from string “/my/path/to/file.txt” or “c:\my\other\path\file.txt” is extracted “file.txt”.

[^/\\] = all chars except / or \ (so the ^ inverts meaning)

Missing ^ is probably issue in your regex. Its semantics actually is: Take one or more \ from end of string and that is not what you are looking for, I guess. Try to rewrite it as

```auto
'([^\\]+)$')[0]

```

CTL uses Java regexes format, so besides of our manual ([http://doc.cloveretl.com/documentation/ … sions.html](http://doc.cloveretl.com/documentation/UserGuide/index.jsp?topic=/com.cloveretl.gui.docs/docs/regular-expressions.html)) and links there you can find plenty of examples online, e.g. [http://www.vogella.com/articles/JavaReg … ticle.html](http://www.vogella.com/articles/JavaRegularExpressions/article.html)

I hope this helps.

---

<div class="post-metadata">

**Author:** ![golomeen](https://avatars.discourse-cdn.com/v4/letter/g/3bc359/32.png) [@golomeen](https://forum.cloverdx.com/u/golomeen)\
**Post date:** [January 28, 2014, 6:56am UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/10 "2014-01-28T06:56:27Z")

</div>

Please help me  
I used to always use source\_name and sheet\_name in metadata for excel files, but past source codes are unreacheable, it was 2 years ago.  
Now I’ve changed direct file name setting from ${data\_in}\1.xls to port:$0.Field1:source and source\_name autofilling stopped working, while sheet\_name continues doing it.

As I remember, source\_name always worked regardless of filename setting way. Am I wrong and what should I do?

And on other hand, getting file names through port fails on one of three XLSreaders in my graph with very scanty message

```auto
ERROR [WatchDog] - Graph execution finished with error
ERROR [WatchDog] - Node XLS_READER2 finished with status: Component has finished and input port 0 still contains some unread records.
ERROR [WatchDog] - Node XLS_READER2 error details:
INFO [exNode_0_1390551613757_LEVEL1_LINES] - Number of commited records: 0

```

while direct filename setting is also OK

Version: 3.3.0.021P commercial

---

<div class="post-metadata">

**Author:** ![novakj](https://avatars.discourse-cdn.com/v4/letter/n/47e85d/32.png) [@novakj](https://forum.cloverdx.com/u/novakj)\
**Post date:** [January 28, 2014, 2:44pm UTC](https://forum.cloverdx.com/t/code-to-pass-file-names-into-metadata-from-data-reader/1263/11 "2014-01-28T14:44:51Z")

</div>

Hello,

Unfortunately, there is an issue in your version of CloverETL related to this functionality, please see the issue report here: [https://bug.javlin.eu/browse/CL-2626](https://bug.javlin.eu/browse/CL-2626)

It is fixed since CloverETL version 3.3.2, so if you upgrade your CloverETL instalation, it should work as you expect.
