Looping through files

James Reece
June 14, 2016 09:38 ANSWERED
I want a simple workflow to open a pre-defined folder location and to iterate through all pdf files within that have a similar filename (the date changes), e.g. “Weekly Macro Report_yyyy_MMM_dd”, and perform some screen scraping.

How can I express "Weekly Macro Report * " as a variable to the Read PDF activity. A wildcard, basically.

I cannot find documentation that details the parameters/variables of the activities.

4 Likes

Hi James,

You could get the filelist with Directory.GetFiles(string path, string searchPattern) - it supports * and ? wildcards:

string() pdfList = Directory.GetFIles(“D:\myFolder”, “Weekly Macro Report*.pdf”)

and then iterate with For/Foreach loop where you will have the ReadPDF activity.

Regards.

7 Likes

James Reece June 14, 2016 10:29
Thanks, Andrzej.

I was just reading the dir.GetFiles docs on msdn.

So my vb expression for the For Each loop will literally be the whole syntax, above??

Sorry for the newby Q - this is my first workflow after a day or two of watching tutorials and it’s still a bit of a mystery.

Andrzej.Kniola June 14, 2016 10:53
Everybody started one day :slight_smile:

Pretty much, yes, depending on what you want to do with it later.

A stub for this could look like this:

5 Likes

James Reece June 14, 2016 10:55
Thanks, Andrzej.

I was just reading the dir.GetFiles docs on msdn.

So I have set TypeArgument to System.String.

My directory to an existing path on my server and within the For Each body, simply put Message Box with input.text set as item.ToString.

Sadly, when it runs it tells me the directory does not exist. I used a full unc path and it appears to have resolved it correctly - the error message has, at least.

James Reece June 14, 2016 11:33
Works a treat, thank you.

Though I got myself in unnecessary trouble when I assumed the method GetFiles() would return array of FileInfo.

I was trying to loop thru FileInfo objects but threw cast exceptions all over the place.

Treating all as string or string did the trick. Why is that?

Andrzej.Kniola June 15, 2016 12:04
Calling GetFiles on a System.IO.DirectoryInfo will return FileInfo, but static Directory.GetFiles only returns String with full paths as the elements.

Here on SO there is a good short explanation on how to use both.

For this use case, at least as far as I understand your description, filepaths should be sufficient.

1 Like

Can someone please explain, what is Directory in Directory.GetFiles()? For me this function is not working, when I assign Directory a string value of my path or give it a String array to store in the files on the directory, I keep getting errors that ‘GetFiles’ is not a member of String and String.Array. So what is it a member of?

Directory is a class not a variable which you can assign values.

Hi! How can I check for 2 types of files?

I’ve tried: Directory.GetFiles(DayFolder.ToString,".TAB,.TXT") but it isn’t working. I want to find files that end in either .txt or .tab and screen scrape! thanks!!!

@lailas

To my knowledge, this isn’t straight forward and there is not a way to do this with the default object.

This might help:

Hi @UiRobot

Any chance you could upload the workflow for this one?

Thanks

Hi all, I am working on opening pdf’s and scraping data. for the for each activity, I am getting the following error: For each: Unable to cast object of type ‘System.Char’ to type ‘System.String’.

can someone help.
thanks
Terri

I’d make 2 separate searches and either merge them, or run both lists through whatever you want to do with the files.

It could be that the “type” in the “for each”-loop is set to System.Char. Try changing it to System.String.

If it’s somewhere else, adding a “.ToString” at the end of the char should work.

Hi –thank you so much. that worked. Enjoy your weekend.

Terri