3
Context:
I am monitoring file deletion and inclusion actions in a folder through Class FileSystemWatcher
with the intention of registering a Log of the actions performed in it.
Problem:
This folder contains several files and subfolders that I am not interested to monitor, so before recording the Log, I need to check if the action performed, was performed in a relevant file to be saved.
Premises of the expression
The file must be in 2 level.
Ex.: Pasture1 Pasture2.qlqCoise FileThe file must belong to the folder
Rem
(be a direct child)
Ex.: Pasture1 Rem Archive.qlqCoise
Examples of valid and invalid expressions:
"Cliente1\\Rem\\COB0111111.REM.txt" //Nome Valido
"Cliente1\\Rem\\23123123.REM.txt" //Nome Valido
"Cliente1\\OK\\COB02222222.REM.txt" //Invalido
"Cliente1\\Ret\\COB0613062019.REM.txt" //Invalido
"COB0613062019.REM.txt" //Invalido
"Cliente2\\COB0613062019.REM.txt" //Invalido
"Cliente2\\Rem\\COB0633333.REM.txt" //Nome Valido
"Cliente2\\Rem\\pasta2\\COB02123123.REM.txt" //Invalido
"Cliente2\\Bla\\Rem\\COB0613062019.REM.txt" //Invalido
"Cliente1\\COB0613062019.REM.txt" //Invalido
"Rem\\COB0613062019.REM.txt" //Invalido
"Rem" //Invalido
"Cliente3" //Invalido
I performed this check with .split
and conditional, but I would like to perform it.
MCVE (Example of solution without regular expression):
static void Main(string[] args)
{
List<String> nomes = new List<string>();
List<String> nomesValidos = new List<string>();
nomes.Add("Cliente1\\Rem\\COB0111111.REM.txt"); //Nome Valido
nomes.Add("Cliente1\\Rem\\23123123.REM.txt"); //Nome Valido
nomes.Add("Cliente1\\OK\\COB02222222.REM.txt"); //Invalido
nomes.Add("Cliente1\\Ret\\COB0613062019.REM.txt"); //Invalido
nomes.Add("COB0613062019.REM.txt"); //Invalido
nomes.Add("Cliente2\\COB0613062019.REM.txt"); //Invalido
nomes.Add("Cliente2\\Rem\\COB0633333.REM.txt"); //Nome Valido
nomes.Add("Cliente2\\Rem\\pasta2\\COB02123123.REM.txt"); //Invalido
nomes.Add("Cliente2\\Bla\\Rem\\COB0613062019.REM.txt"); //Invalido
nomes.Add("Cliente1\\COB0613062019.REM.txt"); //Invalido
nomes.Add("Rem\\COB0613062019.REM.txt"); //Invalido
nomes.Add("Rem"); //Invalido
nomes.Add("Cliente3"); //Invalido
foreach (string nome in nomes) {
var teste = nome.Split("\\");
if (teste.Length == 3) { //Garanto que estará no 2 nível
if (teste[1].ToUpper() == "REM") { //Garanto que o pai direto do arquivo é REM
nomesValidos.Add(nome);
}
}
}
foreach (string nome in nomesValidos) {
Console.WriteLine(nome);
}
Console.ReadLine();
}
I tried to make some expressions on Regex101, but I didn’t get very close to what I’d like.
The MCVE was better than with Regex.
– Bacco
@Bacco except the giving part
.Add()
for each element to create the initial list, and theToUpper()
and the way to write the backslash on the string.– Maniero
What is the doubt?
– Maniero
have you considered using the native filter? (2nd parameter) https://docs.microsoft.com/en-us/dotnet/api/system.io.filesystemwatcher.-ctor?view=netframework-4.8#System_IO_FileSystemWatcher__ctor_System_String_System_String_
– Bacco
@Maniero I put the example string exactly as file Watcher returns in
EventArgs
, My question was how to build a Regex that would do the same as this MCVE. @Bacco did not know that the filter parameter accepted Pattern, I will read the link you sent.– Caique Romero
I think you did good and now looking to get worse. Regex is always worse in everything you can analyze.
– Maniero
@Maniero you say in the sense of performance?
– Caique Romero
I said all of them. I did not answer because the question clearly asks to do something worse, but I would at least make this code simpler and more correct:https://dotnetfiddle.net/qLM6Mg.
– Maniero
I was not aware that Regex is always worse, that it should be used only in last case, otherwise I would have kept the initial solution.
– Caique Romero
Especially now that I’ve given you the one that doesn’t make unnecessary allocations which is what slowed you down.
– Maniero