How to count number of Excel files from a folder using c#? - c#

I need to count the number of excel files,pdf files from a directory.
I have Counted the total number of files from a directory using
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(#"D:\");
int count = dir.GetFiles().Length;
Any Suggestion?

Here's a LINQ solution.
var extensions = new HashSet<string>(StringComparer.OrdinalIgnoreCase)
{
".xls",
".xlsx",
".pdf",
};
var baseDir = #"D:\";
var count = Directory.EnumerateFiles(baseDir)
.Count(filename =>
extensions.Contains(Path.GetExtension(filename)));

Use SearchPattern in GetFiles method.
dir.GetFiles("*.XLS");

int count = 0;
foreach (string file in Directory.GetFiles(#"D:\"))
{
if (file.EndsWith(".pdf") || file.EndsWith(".xls"))
{
count++;
}
}

String[] excelFiles=Directory.GetFiles("C:\\", "*.xls");

int count = Directory.GetFiles(path).Count(f =>(f.EndsWith(".xls") || f.EndsWith(".xlsx")));

simple
int count = dir.GetFiles("*.txt").Length + dir.GetFiles("*.pdf").Length

var count = System.IO.Directory.GetFiles(#"D:\")
.Count(p => Path.GetExtension(p) == ".xls");

Related

Count how many files starts with the same first characters c#

I want to make function that will count how many files in selected folder starts with the same 10 characters.
For example in folder will be files named File1, File2, File3 and int count will give 1 because all 3 files starts with the same characters "File", if in folder will be
File1,File2,File3,Docs1,Docs2,pdfs1,pdfs2,pdfs3,pdfs4
will give 3, because there are 3 unique values for fileName.Substring(0, 4).
I've tried something like this, but it gives overall number of files in folder.
int count = 0;
foreach (string file in Directory.GetFiles(folderLocation))
{
string fileName = Path.GetFileName(file);
if (fileName.Substring(0, 10) == fileName.Substring(0, 10))
{
count++;
}
}
Any idea how to count this?
You can try querying directory with a help of Linq:
using System.IO;
using System.Linq;
...
int n = 10;
int count = Directory
.EnumerateFiles(folderLocation, "*.*")
.Select(file => Path.GetFileNameWithoutExtension(file))
.Select(file => file.Length > n ? file.Substring(0, n) : file)
.GroupBy(name => name, StringComparer.OrdinalIgnoreCase)
.OrderByDescending(group => group.Count())
.FirstOrDefault()
?.Count() ?? 0;
You could instantiate a list of strings of files with a unique name, and check if each file is in that list or not:
int count = 0;
int length = 0;
List<string> list = new List<string>();
foreach (string file in Directory.GetFiles(folderLocation))
{
boolean inKnown = false;
string fileName = Path.GetFileName(file);
for (string s in list)
{
if (s.Length() < length)
{
// Add to known list just so that we don't check for this string later
inKnown = true;
count--;
break;
}
if (s.Substring(0, length) == fileName.Substring(0, length))
{
inKnown = true;
break;
}
}
if (!inKnown)
{
count++;
list.Add(s);
}
}
The limitation here is that you are asking if the first ten characters are the same, but your examples given showed the first 4, so just adjust the length variable according to how many characters you would like to check for.
#acornTime give me idea, his solution didn't work but this worked. Thanks for help!
List<string> list = new List<string>();
foreach (string file in Directory.GetFiles(folderLocation))
{
string fileName = Path.GetFileName(file);
list.Add(fileName.Substring(0, 10));
}
list = list.Distinct().ToList();
//count how many items are in list
int count = list.Count;

How do i read a text files lines and remove the first line?

string[] files = File.ReadAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt");
foreach (string file in files)
{
}
I want to to remove from the file UploadedVideoFiles.txt the first line.
Using LINQ is the best approach in this case:
foreach (string file in files.Skip(1))
var lines = File.ReadAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt");
File.WriteAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt", lines.Skip(1));
Use Enumerable.Skip Linq extension method:
string[] files = File.ReadAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt").Skip(1).ToArray();
File.WriteAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt", files);
Old school (without LINQ) is to start iterating at index 1.
for (int i = 1; i < files.Length; ++i)
{
// do something with files[i], which is a line in the file.
}

Split text file every 120,000 Lines?

So I have a textfile I need to split every 120,000, when it's split at the 120,000th line I need the rest to into another text file. Any ideas on this guys?
You can use Batch from MoreLINQ to group your lines into batches of 120,000 lines, which can then each be handles separately.
foreach(var batch in File.ReadLines(inputFile).Batch(120000))
WriteToFile(batch);
var lines = new List<string>();
int counter = 0,i = 1;
string line;
using (var reader = new StreamReader("filePath"))
{
while ((line = reader.ReadLine()) != null)
{
lines.Add(line);
counter++;
if (counter == 120000)
{
string fileName = String.Format("file{0}.txt",i);
File.WriteAllLines(fileName,lines);
lines.Clear();
counter = 0;
i++;
}
}
}
if(lines.Count > 0) File.WriteAllLines("path", lines);
Note: You should use different file names when using the File.WriteAllLines, otherwise you will just overwrite a single file's content.For example you can use another counter for it and increment it for every file, "file1, file2 etc..".
Just another way using Enumerable.GroupBy and "integer division groups":
int batchSize = 120000;
var fileGroups = File.ReadLines(path)
.Select((line, index) => new { line, index })
.GroupBy(x => x.index / batchSize)
.Select((group, index) => new {
Path = Path.Combine(dir, string.Format("FileName_{0}.txt", index + 1)),
Lines = group.Select(x => x.line)
});
foreach (var file in fileGroups)
File.WriteAllLines(file.Path, file.Lines);

Code to trim part of a text file in C#

I have a situation where I am given a text file with text formatted as follows:
C:\Users\Admin\Documents\report2011.docx: My Report 2011
C:\Users\Admin\Documents\newposter.docx: Dinner Party Poster 08
How would it be possible to trim the text file, so to trim the ":" and all characters after it.
E.g. so the output would be like:
C:\Users\Admin\Documents\report2011.docx
C:\Users\Admin\Documents\newposter.docx
Current Code:
private void button1_Click(object sender, EventArgs e)
{
using (StreamWriter sw = File.AppendText(#"c:\output.txt"))
{
StreamReader sr = new StreamReader(#"c:\filename.txt");
Regex reg = new Regex(#"\w\:(.(?!\:))+");
List<string> parsedStrings = new List<string>();
while (sr.EndOfStream)
{
sw.WriteLine(reg.Match(sr.ReadLine()).Value);
}
}
}
Not working :(
int index = myString.LastIndexOf(":");
if (index > 0)
myString= myString.Substring(0, index);
Edit - Added answer based on modified question. It can be condensed slightly, but left expanded for clarity of what's going on.
using (StreamWriter sw = File.AppendText(#"c:\output.txt"))
{
using(StreamReader sr = new StreamReader(#"input.txt"))
{
string myString = "";
while (!sr.EndOfStream)
{
myString = sr.ReadLine();
int index = myString.LastIndexOf(":");
if (index > 0)
myString = myString.Substring(0, index);
sw.WriteLine(myString);
}
}
}
Edited
StreamReader sr = new StreamReader("yourfile.txt");
Regex reg = new Regex(#"\w\:(.(?!\:))+");
List<string> parsedStrings = new List<string>();
while (!sr.EndOfStream)
{
parsedStrings.Add(reg.Match(sr.ReadLine()).Value);
}
sure. while reading each line, do a
Console.WriteLine(line.Substring(0,line.IndexOf(": "));
i'd use the answer given here Code to trim part of a text file in C# and find the 2nd occurrence and then use it in substring.
var s = #"C:\Users\Admin\Documents\report2011.docx: My Report 2011";
var i = GetNthIndex(s,':',2);
var result = s.Substring(i+1);
public int GetNthIndex(string s, char t, int n)
{
int count = 0;
for (int i = 0; i < s.Length; i++)
{
if (s[i] == t)
{
count++;
if (count == n)
{
return i;
}
}
}
return -1;
}
Assuming this is being done where the files are supposed to exist, you could handle this taking into account any colons in the (what I assume is) description by checking for the existence of the files after you get the index of the colon.
List<string> files = new List<string>();
files.Add(#"C:\Users\Admin\Documents\report2011.docx: My Report 2011");
files.Add(#"C:\Users\Admin\Documents\newposter.docx: Dinner Party Poster 08");
files.Add(#"C:\Users\Admin\Documents\newposter.docx: Dinner Party: 08");
int lastColon;
string filename;
foreach (string s in files)
{
bool isFilePath = false;
filename = s;
while (!isFilePath)
{
lastColon = filename.LastIndexOf(":");
if (lastColon > 0)
{
filename = filename.Substring(0, lastColon);
if (File.Exists(filename))
{
Console.WriteLine(filename);
isFilePath = true;
}
}
else
{
throw new FileNotFoundException("File not found", s);
}
}
}
Try this:
more faster:
string s = #"C:\Users\Admin\Documents\report2011.docx: My Report 2011";
string path = Path.GetDirectoryName(s) + s.Split(new char[] { ':' }) [1];
Console.WriteLine(path); //C:\Users\Admin\Documents\report2011.docx
you need import System.IO
you could use split:
string[] splitted= myString.Split(':');
Then you get an array where you take the first one.
var mySplittedString = splitted[0]
Have a look here if you need more information on this.
EDIT: In your case you get an array with the size of at least 3 so you need to get splitted[0] and splitted[ 1]

C#: what is the simplest way to sort the directory names and pick the most recent one?

I have a list of directories in a parent directory. These directories will be created in a format like 00001, 00002, 00003... so that the one with the bigger trailing number is the recent one. in the above instance, it is 00003. I want to get this programmatically.
thanks for any help..
Try this:
var first = Directory.GetDirectories(#"C:\")
.OrderByDescending(x => x).FirstOrDefault();
Obviously replace C:\ with the path of the parent directory.
.NET 2:
private void button1_Click(object sender, EventArgs e) {
DirectoryInfo di = new DirectoryInfo(#"C:\Windows");
DirectoryInfo[] dirs = di.GetDirectories("*",
SearchOption.TopDirectoryOnly);
Array.Sort<DirectoryInfo>(dirs,
new Comparison<DirectoryInfo>(CompareDirs);
}
int CompareDirs(DirectoryInfo a, DirectoryInfo b) {
return a.CreationTime.CompareTo(b.CreationTime);
}
Why not do something like this:
string[] directories = Directory.GetDirectories(#"C:\temp");
string lastDirectory = string.Empty;
if (directories.Length > 0)
{
Array.Sort(directories);
lastDirectory = directories[directories.Length - 1];
}
What about:
var greaterDirectory =
Directory.GetDirectories(#"C:\")
.Select(path => Path.GetFileName(path)) // keeps only directory name
.Max();
or
var simplest = Directory.GetDirectories(#"C:\").Max();
Just throw the output of GetDirectories or GetFiles at a bubble sort function.
private void SortFiles(FileSystemInfo[] oFiles)
{
FileSystemInfo oFileInfo = null;
int i = 0;
while (i + 1 < oFiles.Length)
{
if (string.Compare(oFiles[i].Name, oFiles[i + 1].Name) > 0)
{
oFileInfo = oFiles[i];
oFiles[i] = oFiles[i + 1];
oFiles[i + 1] = oFileInfo;
if (i > 0)
{
i--;
}
}
else
{
i++;
}
}
}

Categories