Check and rescue info from file name

Hello everyone, i had already help to determine while a file can be xls or csv, but now i have another issue:

i need to rescue data from the name file itself, and also check if the file name has a valid format

The format contains both and id and data of year+moth, and looks like this: ‘12345678-9_yyyymm’ and except the _ everything is dynamic
After the name check, i need to rescue both the id and the month as separate info to use later as values.

Hi Sylvaranth,

Actually this is quite easy

Option 1

The examples I gave you to split off the extension, will also give you the file part as well. if you need to seperate the part part so you just have the file name you can use either of these first, then split the extension:

  • Split Path
  • @{pathparts}= Split String ${stringpath} ${/)

Once you have the filename part separated from the path and extension a simple split on _ should give you what you need

Option 2

with the full path, just use a regex (Get Regexp Matches) to pull out the values you need

  • First construct and test your regex (Take your time to understand this regex example before moving on, because this approach can be used for so many things not just extracting values from file names)
  • Then put it into a robot file, something like this:
*** Settings ***
Library 	String

*** Variables ***
${FilePath} 	/my/path/to/datafile/12345678-9_202211.csv
${MyIDRegex} 	\\/(\\d*-\\d)_(\\d\\d\\d\\d)(\\d\\d)\\.(.*)

*** Test Cases ***
Sylvaranth87 ID And Date example
	${matches}= 	Get Regexp Matches 	${FilePath} 	${MyIDRegex} 	1	2	3	4
	Log 	${matches}
	${MyFileID}= 	Set Variable 	${matches[0][0]}
	${MyFileYear}= 	Set Variable 	${matches[0][1]}
	${MyFileMonth}= 	Set Variable 	${matches[0][2]}
	${MyFileExt}= 	Set Variable 	${matches[0][3]}


1 Like

fantastic! used the regex method and worked well

1 Like