Looping through files


#1

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.


Return all files in a folder
Read all PDF files from folder
#2

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.


#3

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.


#4

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

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.


#6

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?


#7

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.


#8

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?


#9

Directory is a class not a variable which you can assign values.
https://msdn.microsoft.com/en-us/library/system.io.directory(v=vs.110).aspx
https://msdn.microsoft.com/en-us/library/07wt70x2(v=vs.110).aspx


PDF upload