How do we get the files extension? I want to get the files extension and move the file

If statement is inside a FOR EACH loop
file in WithYearFilesFolder
file = object (eg. pdf file )
I want to check if the file extension is a pdf and move it.

Thanks in advance

Hi @samuel.chaane,

You can use Path.Contains(".pdf") inside your if statement. See below for what I use to read doc and pdf texts:

image

2 Likes

Is still it cant identify the extensions. I will try it again

How are you getting your file array? You can set your For Each TypeArgument to string and then check the data with the code I used above

1 Like

@samuel.chaane

  1. Use assign activity :
    getFiles =directory.GetFiles(FolderPath)
    FolderPath= Path of folder where your files are present
  2. Use for each activity
    Inside body of for each use if condition activity
    item.Contains(“.pdf”)
    if yes then use move file

Best Regards,
Vrushali

1 Like

Hi @samuel.chaane,

  1. You can declare the variable filename =path.GetFileName(file.ToString). This will return the filename instead of full file path.

  2. in if condition you can check path.GetExtension(filename)=“.pdf” , here you can check filename (variable declared in step 1) exact extension.

.Contains may not be robust in some scenarios where your file itself contains .pdf(for example, pdf.pdf) as name, but this could be a very rare scenario :slight_smile:

2 Likes

And if you haven’t already done it, remember to convert the string to lower case before comparing it. If not, you might miss pdf files with caps in the extension, e.g. “Example.PDF” or “IMPORTANT.PDF”.

Path.GetExtension(filename).ToLower = ".pdf"

3 Likes