I have a number of files in a folder including PDF ,excel and zip files. But i need to keep only PDF files and delete the excel and zip and other format files

I have a number of files in a folder including PDF ,excel and zip files. But i need to keep only PDF files and delete the excel and zip and other format files

@sathish_moorthy

filesToDelete = Directory.GetFiles(“your_folder_path_here”)

For Each activity (item in filesToDelete)
If Path.GetExtension(item.ToString).ToLower =“.pdf”
Do nothing

else

Delete File

Hi @sathish_moorthy

Try this

Path.GetFileName(CurrentFile.ToString).Contains(".pdf")

Cheers!!

Hi @sathish_moorthy

Try this in Invoke Code:

Dim filesToDelete As List(Of String) = Directory.GetFiles("C:\YourFolderPath") _
    .Where(Function(file) Path.GetExtension(file).ToLower() <> ".pdf") _
    .ToList()

For Each fileToDelete As String In filesToDelete
    File.Delete(fileToDelete)
Next

Change your required folder path.

Regards

Hi @sathish_moorthy

Input:

Code in Invoke Code:

Dim filesToDelete As List(Of String) = Directory.GetFiles("C:\Users\"+Environment.UserName+"\Documents\UiPath\BlankProcess28\Input") _
    .Where(Function(file) Path.GetExtension(file).ToLower() <> ".pdf") _
    .ToList()

For Each fileToDelete As String In filesToDelete
    File.Delete(fileToDelete)
Next

Change the folder path according to yours

Output:

Regards

So this expression will get you all the Excel and Zip files:

New System.IO.DirectoryInfo("C:\temp").GetFiles("*").Where(Function(s) ({".XLSX",".XLS",".ZIP"}.Contains(s.Extension.ToUpper)))

You can just loop through that and delete:

image

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