Files not deleting for previous month

Hi All,

There is one issue, the logic is written as such bot should delete the file except last month and last to last month, i.e if this august then it should delete may june file if it exist.

Path.GetFileName(file).Contains(Now.ToString(“MMMM_”)+Now.Year.ToString) or Path.GetFileName(file).Contains(Now.AddMonths(-1).ToString(“MMMM_”)+Now.AddMonths(-1).Year.ToString)

Hi @Rakesh_Tiwari

For Each file In Directory.GetFiles(folderPath)
    fileName As String = Path.GetFileName(file)
    
    currentMonthYear As String = Now.ToString("MMMM_yyyy")
    lastMonthYear As String = Now.AddMonths(-1).ToString("MMMM_yyyy")
    lastToLastMonthYear As String = Now.AddMonths(-2).ToString("MMMM_yyyy")
    
    If Not (fileName.Contains(currentMonthYear) OrElse 
            fileName.Contains(lastMonthYear) OrElse 
            fileName.Contains(lastToLastMonthYear)) Then
        File.Delete(file)
    End If
Next

Hope it helps!!

Hi @Rakesh_Tiwari

=> Assign → currentDate = DateTime.Now
=> Assign → lastMonth = currentDate.AddMonths(-1)
=> Assign → lastToLastMonth = currentDate.AddMonths(-2)
=> For Each file in folder activity to iterate the files in the folder.
=> Inside for each insert the If condition and give the below condition

- Condition -> CurrentFile.CreationTime < lastToLastMonth OR CurrentFile.CreationTime > lastMonth

=> If condition satisfied in then block give the Delete file activity to delete the CurrentFile.

Check the below workflow for better understanding.

Hope it helps!!

this statement:
grafik

can be simplified to:
grafik

With this statement we can create a list:
grafik

And can quickly check


new Int32(){0,1}.Select(Function (x) now.AddMonths(x).toString("MMMM_yyyy")).Any(Function (x) Path.GetFileName(yourFileNameVar).Contains(x))

@Rakesh_Tiwari

currentMonthYear = DateTime.Now.ToString(“MMMM_”) + DateTime.Now.Year.ToString()
lastMonthYear = DateTime.Now.AddMonths(-1).ToString(“MMMM_”) + DateTime.Now.AddMonths(-1).Year.ToString()
lastToLastMonthYear = DateTime.Now.AddMonths(-2).ToString(“MMMM_”) + DateTime.Now.AddMonths(-2).Year.ToString()
For each File in FolderPath
fileName = Path.GetFileName(file)
if (!fileName.Contains(currentMonthYear) && !fileName.Contains(lastMonthYear) && !fileName.Contains(lastToLastMonthYear))
Use Delete File


i just changed file name with files but showing error as end expression.

Not (fileName.Contains(currentMonthYear) Or fileName.Contains(lastMonthYear) Or fileName.Contains(lastToLastMonthYear))

1 Like


error: value can not be null

but i can see in that folder, files are available

@Rakesh_Tiwari

ReadingSpecificColumns.zip (161.9 KB)

In the else use delete file

1 Like

@Rakesh_Tiwari

You can try the below process which is simpler
you can follow another simple process.
=> Use for each file in folder activity to iterate the files in the folder.
=> Inside for each insert the If activity and give the below condition

- Condition -> CurrentFile.CreationTime < DateTime.Now.AddMonths(-2) OR CurrentFile.CreationTime > DateTime.Now.AddMonths(-1)

=> In then block give the Delete file activity, In the file name field give CurrentFile.toString

Check the below workflow for better understanding.

Or you can find the solution which is posted at above replies. The both are working for me.

Hope it helps!!

1 Like

Hi All,

Thanks all for your support issue is resolved not, it was issue with folder location.

Hence closing this thread.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.