Search code examples
nim-lang

Nim Lang - Finding char index


I appreciate this is a very newbie question, I just came across Nim 2 days ago and thought I'd give it some investigation. but I'm not finding the documentation to be very intuitive.

I have used the following code to get a list of files

const dataPath:string = r"/Users/xyz/Dev/Nim"

proc listFilesRecursively(dirPath: string) =
  for entry in walkDirRec(dirPath):
    echo("Filename: ",entry.splitPath.tail, ", Path: ",entry.normalizedPath)
    #echo rfind(entry,'/',start=30)

listFilesRecursively(dataPath)

I have managed to find the last occurance of '/' in by using reverse find rfind(). But I'm trying to find index of the next occurance of '/' so I can extract the substring between the last and 2nd to last occurances of '/'.

I've tried settings different values to the start parameter, and even used rfind(entry,'/',start=rfind(entry,'/')) to try and make it find the next index after the first. But I cannot get it to work.

I might have the following structure in the directory

  • /Users/xyz/Dev/folder1/PEOPLE/file1.csv
  • /Users/xyz/Dev/folder1/PEOPLE/file2.csv
  • /Users/xyz/Dev/folder1/SALES/file1.csv
  • /Users/xyz/Dev/folder1/SALES/file2.csv
  • /Users/xyz/Dev/folder2/PEOPLE/file1.csv
  • /Users/xyz/Dev/folder2/PEOPLE/file2.csv
  • /Users/xyz/Dev/folder2/SALES/file1.csv
  • /Users/xyz/Dev/folder2/SALES/file2.csv

I'm looking to get the substring of people and sales, so I can group all files from all 'sales' folders into one key/pair value, and group all files from 'people' foldes into another key pair value (dictionary in python, in nim?). I will then loop through the files in each group, import the CSV files and upload them to a database (SQL server 2019, if it is possible?)

Many thanks for your help.

I also tried chatGPT but it doesn't seem to understand much about the nim syntax.


Solution

  • I think entry.parentDir.lastPathPart from the os module should give you the correct path component.

    For key/value pairs have a look at Table from tables.