Search specific file in last 15 days folder

Hi Devs,
I want to search any specific file in last 15 days folder until file not found.
Below is the scenario:
29—file not found
28—file not found
27—file not found
26—file not found


14—file not found
13–file found–exit from loop—read that file
12
11

Please help me to automate this scenario.

Thanks in advance!!!

Hi @Riya1

Check the below LINQ expression:

filePath = (From day In Enumerable.Range(0, 15)
                          Let folderDate = DateTime.Now.AddDays(-day).ToString("dd-MM")
                          Let folderPath = Path.Combine(baseFolderPath, folderDate)
                          Where Directory.Exists(folderPath)
                          Let files = Directory.GetFiles(folderPath, fileName)
                          Where files.Length > 0
                          Select files.FirstOrDefault()).FirstOrDefault()

Please create the necessary variables.

Regards

@Riya1

  1. For each folder in folder and give the mail folder where all your folders are presnet
  2. For each file in folder and give filter as the filename you need if founf exit loop using break

Cheers

Hi @Riya1 ,
Please use the below approach

  1. Assign: rootFolderPath = “C:\RootDirectory”
  2. Assign: targetFileName = “specificFile.txt”
  3. Assign: folderList = Directory.GetDirectories(rootFolderPath)
  4. For Each: item in folderList
    4.1 If: Directory.GetLastWriteTime(item) >= DateTime.Now.AddDays(-15)
    4.1.1 Assign: fileList = Directory.GetFiles(item)
    4.1.2 For Each: file in fileList
    4.1.2.1 If: Path.GetFileName(file) = targetFileName
    4.1.2.1.1 Log Message: file

Regards
Sandy

Hi @Riya1

Follow the below approach:

  1. Use For Each Folder in Folder activity to iterate through the folders.
  2. Use IF activity with below condition expression:
    Not Directory.GetLastWriteTime(CurrentFolder.FullName) >= DateTime.Now.AddDays(-15)
    If True: Use Continue activity to move to check next folder ELSE: Move further to check files inside the folder.
  3. Use For Each File in Folder activity to iterate through the files from the current folder.
  4. Use IF activity to check the current file name in the folder is matching with your file name or not (I’m using Variable named as FileName in sequence)
  5. If File Name matched then you can use Read File activity according to the File Type.


Hope it helps!!
Happy Automation :uipath: