M
Matthijs de Z
Hi,
After googling and reading some pages (among
http://msdn.microsoft.com/en-us/library/bb882639.aspx), i was trying
to get some code running, but I have some problem with the regular
expression.
I would like to look for files that match a name build like this:
defaulName20100223.zip
Therefore I made a regular expression string:
string regExpression = "("+myDefaultName+@"[0-9]{8}\.[zZ][iI][pP])";
where myDefaultName is just a string (needs to be dynamic).
the fileList I will query, will look something like this:
c:\\myDir1\\mydir2\\defaulName20100223.zip
c:\\myDir1\\mydir2\\extra backup defaulName20100223.zip
c:\\myDir1\\mydir2\\defaulName20100224.zip
c:\\myDir1\\mydir2\\defaulName20100225.zip
c:\\myDir1\\mydir2\\copy defaulName20100223.zip
c:\\myDir1\\mydir2\\defaulName20100226.zip
c:\\myDir1\\mydir2\\defaulName20100226-copy.zip
the only files I want in the result set are:
c:\\myDir1\\mydir2\\defaulName20100223.zip
c:\\myDir1\\mydir2\\defaulName20100224.zip
c:\\myDir1\\mydir2\\defaulName20100225.zip
c:\\myDir1\\mydir2\\defaulName20100226.zip
I tried to add Path.DirectorySeparatorChar.ToString() to the front of
the regular expression string, I get it twice.
string myDefaultName = @Path.DirectorySeparatorChar + "defaultName";
string regExpression = "("+myDefaultName+@"[0-9]{8}\.[zZ][iI][pP])";
Gives me "(\\defaultName[0-9]{8}\\.[zZ][iI][pP])" as a regular
expression, while DirectorySeparatorChar is actually just one \
(although I can see it twice again in the result set)
How can I make the regExpr so, that it does what it should do?
I pasted the rest of the code under the message.
Kinds regards and hope you can help me out,
Matthijs
-----------------------------------
private void worker()
{
string startFolder = @"c:\myDir1\mydir2\";
IEnumerable<System.IO.FileInfo> fileList =
GetFiles(startFolder);
string myDefaultName = @Path.DirectorySeparatorChar +
"defaultName";
string regExpression = "("+myDefaultName+@"[0-9]{8}\.[zZ]
[iI][pP])";
System.Text.RegularExpressions.Regex searchTerm =
new System.Text.RegularExpressions.Regex(regExpression);
var queryMatchingFiles =
from file in fileList
where file.Extension == ".zip"
let matches = searchTerm.Matches(file.FullName)
where searchTerm.Matches(file.FullName).Count > 0
select new
{
name = file.FullName,
matches = from
System.Text.RegularExpressions.Match match in matches
select match.Value
};
queryMatchingFiles = queryMatchingFiles; //just for debug
mode, so I can hover over it and check the content
}
static IEnumerable<System.IO.FileInfo> GetFiles(string path)
{
if (!System.IO.Directory.Exists(path))
throw new System.IO.DirectoryNotFoundException();
string[] fileNames = null;
List<System.IO.FileInfo> files = new
List<System.IO.FileInfo>();
fileNames = System.IO.Directory.GetFiles(path, "*.*");
foreach (string name in fileNames)
{
string onlyTheName =
name.Substring(name.LastIndexOf(Path.DirectorySeparatorChar) + 1);
files.Add(new System.IO.FileInfo(name));
}
return files;
}
After googling and reading some pages (among
http://msdn.microsoft.com/en-us/library/bb882639.aspx), i was trying
to get some code running, but I have some problem with the regular
expression.
I would like to look for files that match a name build like this:
defaulName20100223.zip
Therefore I made a regular expression string:
string regExpression = "("+myDefaultName+@"[0-9]{8}\.[zZ][iI][pP])";
where myDefaultName is just a string (needs to be dynamic).
the fileList I will query, will look something like this:
c:\\myDir1\\mydir2\\defaulName20100223.zip
c:\\myDir1\\mydir2\\extra backup defaulName20100223.zip
c:\\myDir1\\mydir2\\defaulName20100224.zip
c:\\myDir1\\mydir2\\defaulName20100225.zip
c:\\myDir1\\mydir2\\copy defaulName20100223.zip
c:\\myDir1\\mydir2\\defaulName20100226.zip
c:\\myDir1\\mydir2\\defaulName20100226-copy.zip
the only files I want in the result set are:
c:\\myDir1\\mydir2\\defaulName20100223.zip
c:\\myDir1\\mydir2\\defaulName20100224.zip
c:\\myDir1\\mydir2\\defaulName20100225.zip
c:\\myDir1\\mydir2\\defaulName20100226.zip
I tried to add Path.DirectorySeparatorChar.ToString() to the front of
the regular expression string, I get it twice.
string myDefaultName = @Path.DirectorySeparatorChar + "defaultName";
string regExpression = "("+myDefaultName+@"[0-9]{8}\.[zZ][iI][pP])";
Gives me "(\\defaultName[0-9]{8}\\.[zZ][iI][pP])" as a regular
expression, while DirectorySeparatorChar is actually just one \
(although I can see it twice again in the result set)
How can I make the regExpr so, that it does what it should do?
I pasted the rest of the code under the message.
Kinds regards and hope you can help me out,
Matthijs
-----------------------------------
private void worker()
{
string startFolder = @"c:\myDir1\mydir2\";
IEnumerable<System.IO.FileInfo> fileList =
GetFiles(startFolder);
string myDefaultName = @Path.DirectorySeparatorChar +
"defaultName";
string regExpression = "("+myDefaultName+@"[0-9]{8}\.[zZ]
[iI][pP])";
System.Text.RegularExpressions.Regex searchTerm =
new System.Text.RegularExpressions.Regex(regExpression);
var queryMatchingFiles =
from file in fileList
where file.Extension == ".zip"
let matches = searchTerm.Matches(file.FullName)
where searchTerm.Matches(file.FullName).Count > 0
select new
{
name = file.FullName,
matches = from
System.Text.RegularExpressions.Match match in matches
select match.Value
};
queryMatchingFiles = queryMatchingFiles; //just for debug
mode, so I can hover over it and check the content
}
static IEnumerable<System.IO.FileInfo> GetFiles(string path)
{
if (!System.IO.Directory.Exists(path))
throw new System.IO.DirectoryNotFoundException();
string[] fileNames = null;
List<System.IO.FileInfo> files = new
List<System.IO.FileInfo>();
fileNames = System.IO.Directory.GetFiles(path, "*.*");
foreach (string name in fileNames)
{
string onlyTheName =
name.Substring(name.LastIndexOf(Path.DirectorySeparatorChar) + 1);
files.Add(new System.IO.FileInfo(name));
}
return files;
}