string.Contains() not working

Hi, I’m having issue with Contains() where it unable to detect the variable that String Contains.
The Contains unable to detect the string.csv name. It goes not found directly. Could you advise on this issue

Hi @ainaasyhda

Have a try on this

YourVariable.Contains(Filename.ToString.Trim)

Regards
Gokul

Hi @ainaasyhda ,

You could try adding other operations like the ones shown below to detect the string →

obj.ToString.ToLower.Trim.Contains(iteminlowercase)
obj.ToString.ToUpper.Trim.Contains(ITEMINUPPERCASE)

If that doesn’t work, then would you be so kind as to include an example or two of the strings which do not return the desired output?

An alternative would be to use the IsMatch() from the RegularExpressions class.
We can look into that once you provide the examples.

Kind Regards,
Ashwin A.K

1 Like

@ainaasyhda Can you show what the condition you wrote. And, provide sample file name that you are comparing to


image

I want to get the .csv file name that contains(AINAA) however at the if else statement it didnt working for the condition that i wrote

Hi @ainaasyhda

Try like this expression

NewFileInfo(report).Length <> 0 And report.ToString.Trim.Contains("AINAA")

Regards
Gokul

Hi @ainaasyhda ,

The screenshot you have share doesn’t seem to contain any csv files.
I am referring to the Housekeep.AINAA.CompChk, but if you have a csv file which contains what you are looking for in that directory then its fine.

Also, I would recommend utilizing the Immediate Window on the left hand side to see what the inSystemName contains.

You could also log the items before performing the check, so that if there are any extra characters, you will be able to pinpoint the errors from the logs.

Its always good to cultivate that habit, as it helps you peer into your code, and how it works.

Kind Regards,
Ashwin A.K

1 Like

Hi,

Can you try the following condition?

New FileInfo(report).Length <>0 AndAlso report.Contains(inSystemName) AndAlso System.IO.Path.GetExtension(report).ToLower=".csv"

Regards,

1 Like

Put the back slash before extension like \ .csv

It’s hard to spot the exact flaw with the information given, but the trick to resolve this is also a matter of doing your debugging properly, so you can deduce what is the exact error instead of guessing and trying stuff untill it works. Usually it is something tiny, since I spot no major flaws in the logic.

It helps to break down any potential point of failure, make it visible and isolate individual functionalities.

Example: your DirectoryGetFiles, does it contain the right information? Add a writeline to your loop outside and display the value(s) of Report.
next: Your if - then contains 2 conditions. Which one fails? Check them separately first and see which of the expressions is wrong.
Etc etc.

Also, you can take a shortcut as wel with your directory.getfiles:

Directory.GetFiles(yourFolder, “*” + inSystemName + “*.csv”

This will already filter the list of files on your .csv files containing inSystemName, saving you one validation already and shortening your loop.

Hi! Thank you for your advised, i will try out it! The process will get the csv file from one folder for example - C:\FOLDER\Reports\AINAA.csv
However, in this folder contains a few others different .csv file.
im writing the code to get the directory as - Directory.getfiles(“C:\S4ID\Reports”, “*.csv”)

Yes, but then you validate it versus the input filenameinSystemName right? So if you include that into the getFiles expression you are guaranteed that it will only list that file, since at this point in the code, inSystemName has 1 value, so it can be directly passed into your file filter.
And if there are multiple files that meet the condition, such as AINAA.csv, AINAA2.csv etc, you still list all of them if you include the * into the expression.

==> AINAA122345.csv matches AINAA*.csv