Iterating through folders, then through files

Looking for some sample code that will iterate a list of folders in a path, and within each folder, I want to iterate each file in the folder and run through some calculations and move the file out.

I’ve use the iteration to call a module without problems but what I’m hoping is when I load using the Load files, it wants a direct path and doesn’t seem to like using the Parameters.

Got a quick sample that does a module 1 that just grabs a list of folders then passes each folder path to a new module that then gets the list of files in that folder to load?

Thanks
Flowy

Also have an issue with the new module. Once I can load a file I want to add a new column name based on a mapping from a sql table. I’m having a hard time trying to figure out how to pass this to replace it. I see in my initial load of the file everything is labeled (Column 1, Column 2, etc). Then I load a sql table that has a Column number then to the new Mapped column name.

Best method to use to replace them?

Thanks
Flowy

Also if we load a file by a delimited txt and it has no headers and you get the preset column 1 to column x, trying to use the rename columns with lookup doesn’t work. Any suggestions?

Thanks
Flowy

What do you mean by "doesn't seem to like"?

It would also help if you provide examples because for me it's hard to understand the questions. Drawings, screenshots, diagrams - the more the better.

Awesome. Let me try to create a base sample and see if I can get this to relate back. The main idea is I have a folder with an unknown amount of other sub folders, Each sub folder has files in it. Each file, needs to be remapped with column headers and then outputted back out to a document lets say google docs or to the directory.

The only way I can see this working is by creating modules. Module 1 needing to links to the main folder then iterate and pass a sub folder to Module 2 which then takes the each folder and looks for each file in that folder and sends it to Module 3. Module 3 then takes the file and does a lookup and replaces the column x identifiers with a replacement from sql.

I finally have the replace by lookup working but I'm having a hard time with the looping.

coding wise would be:
For each folder in a directory
For each file in that folder
Rename generic column headers to headers defined by a SQL query and Output file to excel

Thanks
Flowy

If you know that there is only 1 level of subfolders, then you can produce the list of necessary files in one action “List files”. Just specify that you need to scan subfolders. If may reduce the number of necessary modules from 3 to 2: one module to build a list of files and do iteration, and the other module to process one file.

image

If the folder structure is hierarchical with unknown depths, that can be handled too, but that’s a different story.

Yeah I saw this option but in the next module how do you pass the full path with file name to "Import delimited text file". I tried to create a Parameter through "Edit Parameter" using it as a string. Then passed the "File name with full path" as a string to the new created Parameter from Module 1 to Module 2. It doesn't like we to use it this way. Do you have a sample or way to pass the fulle filename with path from module 1 to Module 2 use of the "Import delimited text file" so that I an load it?

Thanks!
Flowy

The error I see is an absolute path with my variable name concatenated. So Module 1 passes “File name with full path” to var “FullPath”. FullPath is created as a string var in Module 2 through edit parameters. Current value I renamed as [FullPath]. But when I try to Load File in “Import delimited text file”, and try Parameter [FullPath] - it errors with “Something is wrong: Calculation failed. 1 error occurred. File C:\Tests\RunTest[FullPath] not found.”

Thanks
Flowy

Please post more screenshots (esp. of errors) and examples. I’m not sure I understand what you are trying to achieve. I understand that English is not your native language, so any visuals would help as they require no translation.

Sure thing but actually I’m for california :slight_smile: so English is native for me. I’ll do my best to taking some picture and seeing if you can help me with some samples on how to get what I’m trying to achieve.

Thanks Dmitry!
Flowy

RenameDirectoryStruture
RenameError
testrename.morph (5.5 KB)
test.xlsx (8.7 KB)

Attaching the morph sample file named “testrename.morph”
Attaching the excel file sample with no header “test.xlsx”

Attaching the Error that I’m receiving at execution of the file load and a picture of how the First module is set to iterate through each file list, and how Module 2 does the execution of that file.

Thanks
Flowy

Apparently, I was wrong :blush:

Haha no worries! I know you work with so many of us in the community! It’s hard to keep track! :slight_smile:

Let me know if you can see how I ca make this work. I even tried to created the parameter as a file name but that still to didn’t seem to work. Attaching the photo of the error.
RenameError2

Flowy

I also tried this with the Import Excel Spreadsheet as well. I tried with using the variable both with square parathesis “[” and with “{” incase I had a syntax issue. Both error.

Attaching the morph source. And the error message I receive at execution. RenameError3.png is using a curly brace “}” or square brace “]” using the parameter as a file name.

If we use a text string using either curly or square brace same error.

testrename.morph (6.3 KB)
RenameError3

Thanks
Flo

The expression for calculating the file path shown in the error message is clearly wrong. The contents of the [FullFilePath] column should’ve been appended instead of the column name. How do you obtain the file path? What’s the expression for it?

image

Parameter assignment doesn’t work like that. Parameters of the iterated module are assigned in the “Iterate” action. See this tutorial guide: EasyMorph | Iterations. Explore the examples in the tutorial and watch the video(s).

Hi Dmitry,

So when I use the pull the file names out in the “Files to Run” box in my sample code in GetFiles Module, I assumed I could iterate through each row of found files. I have pasted the iterate call that shows I tried to use fullfilepath as parameter in execute files with it pointing to the “File name with full path” (RenameError4.png). With the sample picture of the directory structure I created, it should have 4 files listed in the directory/subdirectories. Filename with full path, it has 4 rows. C:\test\a\test.xlsx, C:\test\b\test.xlsx, C:\test\b\test2.xlsx, C:\test\a\test3.xlsx (RenameError5.png). I then call iterate to pass it to Module 2 named ExecuteFiles. In ExecuteFiles, first section calls Create Combined Files box by trying to Load file of what was pulled from Module 1 “Get Files”. But I can’t seem to get row into the Import Excel Spreadsheet. In the Load file, how do I pass that iterate row parameter to Load file? (depicted in RenameError 6.png).

So my hope is you can tell me how I’m suppose to create it in the Edit parameters. Do I use Filename? and if so how do I get it to work in Import Excel spreadsheet in the Load File? Or do I use string text? I’ve tried many different ways but it doesn’t seem to recognize it in the Load file.

Just want a sample code or what to do when you grab rows of filenames, how to pass that filename path to a new module that gets pass to Import Excel Spreadsheet.


RenameError5

Thanks
Flowy

forum.zip (2.3 KB)

Hello @flowy ,

I created a sample project, maybe I didn’t full understand the problem but I hope this could help.
In the first module, main, I scan the folder to retrieve path and file name. The second action simply exclude the .morph file that is in the same dir. The third action iterate the second module, passing the full path as parameter. In the second module I read the content of each file.

1 Like

Thank you so much Andrea! This is exactly what I needed to be able to pass the filenames and paths over to another module in iteration!

Flowy