If condition always comes as false

Hello everyone,
I am trying to make check if files exist in a folder.If yes continue with the task else display a message.For some reason it goes straight to the Else statement and prints the message.There are files in filePath.Any ideas?

Hello @DimiG ,
Check the path and file name with a proper extension like File.xlsx

Can you print the file path and share them here @DimiG ?

HI @Sudharsan_Ka ,
you mean like this?
path

Yes I dont see a file name here @DimiG

If you want to check for folder change this to folder
image

Or Use this expression

Directory.Exists(FilePath)

@Sudharsan_Ka I changed the from file to folder and it woks but if i remove the files the else statement doesn’t print the message

@DimiG Whether the folder you showed in screenshot is available in your machine?

@Sudharsan_Ka yeah the folder is on my desktop.Now if the folder has files the process goes on normally.If I remove them no message is displayed.

Okay so you have changed the option right files-> Folder @DimiG

That means you are checking whether the folder is available in your system or not , so removing the file will not change the condition becase you are checking if the folder is available or not

So in this case Folder is available all the time so it will go to then all the time so the message box in else is not displayed

Regards
Sudharsan

Let me understand clearly you need to check if file is available or Folder is available

If folder you can use the expression it told

if files

System.IO.File.Exists(Path.Combine(filePath,"Filename.Extension"))

Regards
Sudharsan

@Sudharsan_Ka I want to check if the folder contains any pdf files.
does System.IO.File.Exists(Path.Combine(filePath,“*.pdf”)) seem right?

Your file name? @DimiG

If the name is dynamic and the file count will be one means you can try this

System.IO.File.Exists(Directory.GetFiles(filePath,"*pdf")(0).ToString)

Regards
Sudharsan

@Sudharsan_Ka The folder can contain any number of pdf files.
I just wanna check if it contains any.
I tried System.IO.File.Exists(Directory.GetFiles(filePath,“*pdf”))
and System.IO.File.Exists(Directory.GetFiles(filePath,“*pdf”).ToString) and didn’t work.I am getting the message dispay even though the folder contains files.

You need to combine two conditions. First check if the folder path exists and then check if the folder contains any files.

Directory.Exists(filePath) AndAlso Directory.GetFiles(filePath, "*.pdf").Count > 0
1 Like

@ptrobot
That seems to work just fine.Thank you so much.I am also deleting the Path Exists activity since i won;t be using the output for validation anymore.

@Sudharsan_Ka @Gokul_Jayakumar
Thank you guys for helping me and for your time.

You missed out of zero(0) here @DimiG

System.IO.File.Exists(Directory.GetFiles(filePath,"*pdf")(0).ToString)

Alternative condition you can use is @DimiG

Directory.GetFiles(filePath,"*.pdf").Any
1 Like

@Sudharsan_Ka
This suggestion also works like a charm.Thank you so much.

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