How to check the content of a folder for specific

Hello all,

My question today is regarding searching specific file inside a folder, I have 21 files that I need to find inside a folder which contains subfolders, my idea was to search for them then add those files to a list or array or something of sort so that i would be able to later move those files to another location, my issue here is how should I group those 21 file names to work as a filter and then how to create the list with the files so that I can move them to the other directory

all help apreciated

Hi @goncalo.rocha ,

Could you let us know the criteria of selecting/filtering those specific 21 file names ?

Hi @goncalo.rocha
If you know all the 21filename that you need to find out ,you can do it in this way
place all the file name you want to find out in excel

Read the excel
loop through it
use PAth exist check the file you are looking is found or not
IF found then use move file activity to move the file to the desired location
if not found you can use log the message as file not found.

Hope this helps.

@goncalo.rocha

create a list of string name as list1 and initialize New List(Of String)

  1. Use array to maintain the filenames you need and then loop through array
  2. Then inside the loop use list1 = List1.concat(Directory.GetFiles("Folder","FileName*",SearchOption.AllDirectories))

after loop list 1 will contain all the filepaths you need

or other way

List1 = System.IO.Directory.GetFiles("Folder","FileName*",SearchOption.AllDirectories).Where(function(x) RequiredFilesList.Any(function(y) y.Equals(New FileInfo(x).Name))).ToList

RequiredFilesList is the name of files you need

cheers

1 Like

my issue here is that the files might not always be on the same subfolder there fore the file path might change, I only have the name of the file itself, the full path cannot always be considered the same, i was thinking in something like a:

for each file in folder (including subfolders)
if file contains (filename) add to list

or something like that, would it be doable or would it take too much time?

@goncalo.rocha

SearchOption.AllDirectories will search for all the sub folders in the main folder

include sub folders in for each loop also will be same

cheers

so for filename should I use the array of string with the filenames that i need to look for?

@goncalo.rocha

Yes exactly

cheers

@Anil_G one last question, regarding loop, there is no loop activity, should i use a for each

for each item in File array
list1 = List1.concat(Directory.GetFiles(“Folder”,“Currentitem”,SearchOption.AllDirectories))

thank you

@goncalo.rocha

Yes use for each and pass the array of filenames as input

second way does not need loop also

cheers

when placing that into an assign inside the loop i get that directory and searchoption do not exist in the current context

I’m working with C# and i don’t know if that makes the issue

@goncalo.rocha

please try with fullnames space


List1.concat(System.IO.Directory.GetFiles("Folder","FileName*",SearchOption.AllDirectories))

cheers

Sequence
|-- Assign: allFiles = Directory.GetFiles("C:\Path\To\Your\Directory", “.”, SearchOption.AllDirectories)
|-- Assign: filesToSearch = {“file1.txt”, “file2.txt”, “file3.txt”, …}
|-- Assign: filteredFiles = allFiles.Where(Function(file) filesToSearch.Contains(Path.GetFileName(file))).ToArray()
|-- For Each: filePath In filteredFiles
| |-- Move File: Source = filePath, Destination = "C:\Path\To\Your\Destination"