Change filename and file extension for csv files.

I have a file called Sample_data.csv.1 which I want to convert to Sample_data1.csv

I am pulling these files from an API and I was wondering how can remove .#number extension and append it to the file name. As the data dump gets larger the value of .#number gets bigger, so manually thing is simply not feasible.

The file cannot be read when the file extension is csv.1 and we simply cannot remove the .1 as we will have duplicate file names.

I want Knime to change the filename and extension in the desired directory.

Welcome to KNIME forum.
If you use File Reader node, it will read the file with any extension. The way it numbered now could be simply used in the loop.

1 Like

Hi @shubhamss,

I built a workflow that I think solves your problem. KNIME_Change Filename.knwf (11.1 KB)

For testing purposes I created 3 files based on your ’ Sample_data.csv.1’ filename. I placed 3 of them on my desktop and ran the workflow. It renamed all 3 on my desktop to the name format you are wanting.

2 Likes

If I use regex it gives me, “WARN List Files 0:258 Node created an empty data table.”

If I use none it gives me, “ERROR Copy/Move Files 0:261 Execute failed: Could not rename C:\cygwin64\home\Data\data-hourly-01012015-01312015.csv.11 to C:\cygwin64\home\Data\data-hourly-01012015-01312015.csv.11”

Hi, to get this to work we have to change the Regex to mirror the filename format. I based my Regex off of your example which was string data and smaller. I will update and send to you.

Here ya go:

([A-Za-z].)-([A-Za-z].)-([0-9].)-([0-9].).([a-z]{3}).([0-9].*)

1 Like

Oh thanks, now the regex works but the second error message still shows. How exactlly are we handling the file duplication if all csv**.#** is converted into csv.

Is the final node configured in this way?

image

Yes in this exact way. When we have filename.csv.1 the file is corrupted so want it to go back to filename1.csv

We can have duplicate filenames so the intent is to take the 1 and place it before the dot csv.

Ok, I think I understand what’s going on. We have to adjust the Regex replace sequence as well. I need to do a quick test and I’ll post the sequence.

I modified the regex to this ([A-Za-z].)-([A-Za-z].)-([0-9].)-([0-9].).([a-z]{3}).([0-9].*) as my file name is to be exact: “gen-hourly-07012018-07312018.csv.13” and I can have multiple “gen-hourly-07012018-07312018”

Hey @shubhamss

Here is an updated version of the workflowKNIME_Change Filename.knwf (11.1 KB)

4 Likes

Thank you so much for your assistance, works like a charm.

3 Likes

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.