Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I am looking for code to open a text file , then read the text file line by line , if the line in the text file (each line will store approx 5 values) does not contain a certain value e.g "hart" then I wand to remove that line. I am using c# and vs2012 , could anyone show me how to do this ? the file being read from is a csv file. I have no code example here as my current code does not work and I feel givin example will only cause more confusion than asking for someone to show me a clean fresh approach to doing this.
I have added the code I currently have which adds all of the data to a text file however the code I need to figure out is to take these results and filter them
foreach (DataRow dr in this.CalcDataSet.Items)
{
foreach (object field in dr.ItemArray)
{
str.Append(field.ToString() + ",");
}
str.Replace(",", "\n", str.Length - 1, 1);
}
try
{
System.IO.File.WriteAllText(Filepath, str.ToString());
}
catch (Exception ex)
{
MessageBox.Show("Write Error :" + ex.Message);
}
var lines = System.IO.File.ReadAllLines(Filepath).ToList();
var acceptedLines = new List<string>();
foreach (var line in lines)
if (Matches(line))
acceptedLines.Add(line);
System.IO.File.WriteAllLines(Filepath, acceptedLines);
}
private bool Matches(string s)
{
if (s == cmbClientList.SelectedText.ToString())
{
return true;
}
else return false;
}
Use the TextFieldParser class to open and read the file, and split the values into an array. You can then examine each item on each line to see if it contains the value you want.
If the line contains the value, then write the line to a new file. If it doesn't contain the value, then do not write to the new file.
When you're done, close the input and output files. Then delete the original input file and rename the output file.
You can't easily read and modify a text file in-place.
Another option would be to read using TextFieldParser and write to an in-memory stream. At the end, write from the memory stream back to the original file. This will work if the file is small enough to fit in memory.
This will basically do what you want:
var lines = System.IO.File.ReadAllLines("somefile.csv");
var acceptedLines = new List<string>();
foreach (var line in lines)
if (Matches(line))
acceptedLines.Add(line);
System.IO.File.WriteAllLines("output.csv", acceptedLines);
private bool Matches(string s) {
// Whatever you want, return true to include the line, false to exclude)
}
you can do this
string[] lines = File.ReadAllLines("yourfile.csv");
List<string> linesToWrite = new List<string>();
int currentCount = 0;
foreach(string s in lines)
{
if(s.Contains("YourKeyValue"))
linesToWrite.Add(s);
}
File.WriteAllLines("yourfile.csv", linesToWrite );
Related
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 3 years ago.
Improve this question
I have a For Each Loop container where I process files that arrive nightly. I have three user variables defined: FolderPath ("I:\Data Analytics\Referral"), FileName (blank) -- read only variables, FileExistsFlg -- ReadWriteVariables. The first two are strings and the last one is an int. I have added "using System.IO;" to my Namespaces.The value for the FileExistsFlg is 0 -- the default value when I added the variable.
Here is my C# code:
public void Main()
{
// TODO: Add your code here
String FilePath = Dts.Variables["User::FolderPath"].Value.ToString()
+ Dts.Variables["User::FileName"].Value.ToString();
if ( File.Exists(FilePath))
{
Dts.Variables["User::FileExistsFlg"].Value = 1;
}
MessageBox.Show(FilePath);
MessageBox.Show(Dts.Variables["User::FileExistsFlg"].Value.ToString());
Dts.TaskResult = (int)ScriptResults.Success;
}
The task executes, but it always returns a value of 0 when in fact there are files. I'm using MessageBox.Show(. . . .) to test the script.
The code works correctly if I provide a specific file name in the variable, but the file names will change every day. If I leave the variable blank, then it returns 0.
This doesn't strike me as rocket science and I'm mystified why it keeps returning a false value.
Thanks.
I am assuming you want to get all the files from a folder. You can use System.IO.Directory.GetFiles to do this:
string mypath = "C:/myfolder/";
string[] files = Directory.GetFiles(mypath);
You can then run through all the files like this:
foreach(string item in files){
if(item.Contains("my file name without the extension")){
//Do Something
}
}
-----------EDIT #1
string mypath = "C:/myfolder/";
string[] files = Directory.GetFiles(mypath);
if(!files.Length == 0){
foreach(string item in files){
//Run rest of code using the item
}
}else{}
the foreach statement's string variable contains the file path of the file.
-----------EDIT #2
If an error occurs at the first if statement saying that the "!" operator cannot be applied to integers, then instead put the foreach code into the else statement.
string mypath = "C:/myfolder/";
string[] files = Directory.GetFiles(mypath);
if(files.Length == 0){
//do nothing. No files are in the folder.
}else{
foreach(string item in files){
//Do something. Files are in the folder.
}
}
From my understanding of the question, inside the foreach statement would be the code to process and archive the file. The string called "item" should contain the path to the file.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 5 years ago.
Improve this question
Suppose I have a file with multiple line:
En;15;
Vu;
US;32;
I need to check if the string VU is contained so I did:
string text = #"En;15;
Vu;
US;32;"
var exist = text.Contains("Vu");
this will return true but I need to check also if the line of Vu contains only Vu or other contents as the other lines. How can I do this? Thanks.
UPDATE
if the line contains also other element should return false
Split your string into an array and that you will be able to separate all the lines into a separated indexes in the string array using the Split() method
with the separator '\n' that represents a new line:
static void Main(string[] args)
{
string stringfromfile = #"En;
15;
Vu;
US;
32;";
string[] ar = stringfromfile.Split('\n');
// remove ";" character and fix white space for safety
for (int i = 0; i < ar.Length; i++)
{
ar[i] = ar[i].Replace(";","").Trim();
}
if (ar.Contains("Vu"))
{
Console.WriteLine("TRUE");
}
else
{
Console.WriteLine("FALSE");
}
foreach (var itm in ar)
{
Console.WriteLine(itm);
}
Console.ReadLine();
}
Assuming that in your input a "newline" is "\n", and assuming that things in a line are separated by ";", then:
1) break the text into separate lines
2) for each line, break the line into pieces separated by ";"
3) for each broken-line, check each piece and see if Vu is there
4) ..and if it is, hey you found it
5) ..and if that broken-line had just 1 piece, hey, it was single Vu
Bits of code:
1)
#"text
that
has
lines".Split("\n") ==> array of 4 lines
2)
"linethat;has;pieces".Split(";") ===> array of 3 pieces
3) "for each" is a foreach loop. You will need one for lines, and one for pieces. One inside the other.
4) Split removes the separator, so ";" wont show up in a piece, so if(piece == "Vu")
5) "pieces" is array-of-strings, so if(pieces.Length==1) means that the line had a single piece
Now you have all bits, just use them properly.
You could try searching very simply for "\nVu\n" so it would look something little like:
var exist = text.Contains("\nVu\n");
This would find all cases except for the special cases of first and last line, and is more efficient than splitting the string into an array.
And as #quetzalcoatl said
..and first and last line can be covered by .StartsWith("Vu\n") and .EndsWith("\nVu")..
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a text file that the user upload to the application through OpenFileDialog then when the file open the program is reading it line by line using:
string[] data = File.ReadAllLines(file);
how do i make a dynamic editing for each line so that after the Nth number or letter put a space then display the result to the user without writing over the file,
for example:
if my file contain:
0000000000BADBAD
2323040007BADAAD
4234420087BADBAC
the Display should be
0000000000 BADBAD
2323040007 BADAAD
4234420087 BADBAC
Once you have read all the lines into string array, data is isolated from your file i.e. any modification in data won't be reflected into your file. You can manipulate it like any other object.
One of the way manipulating data and achieving your result would be:
foreach (string value in data)
{
Console.WriteLine(value.Insert(10, " "));
}
You can loop throught your lines and extract the data with Regex, example:
string str = "0000000000BADBAD";
var match = Regex.Match(str, #"(?<Number>\d+)(?<Text>.*)");
var number = match.Groups["Number"].Value;
var text = match.Groups["Text"].Value;
string result = String.Format("{0} {1}", number, text);
And write result on your output.
Another alternative is using Linq
string str = "0000000000BADBAD";
int n;
string number = String.Join("", str.TakeWhile(c => int.TryParse(c.ToString(), out n)));
string text = String.Join("", str.SkipWhile(c => int.TryParse(c.ToString(), out n)));
string result = String.Format("{0} {1}", number, text);
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I want to parse a large textfile and if the line contains a certain substring then append that line to my new text file. I need the solution with the lowest memory usage, This is what I have so far, the comments is what I need help adding:
.
.
.
if (File.ReadLines(filepath).Any(line => line.Contains(myXML.searchSTRING)))
{
// code to grab that line and append it to the a new text file
// if new text file doesn't exist then create it.
// All text files im parsing have the same header, I want to grab
// the third line and use it as my new text file header.
// Only write the header once, I do not want it written every time a new
// text file is opened for parsing
}
Try :
var count = 1;
File.WriteAllLines(newFilePath,
File.ReadLines(filepath)
.Where(count++ == 3 || l => l.Contains(myXML.searchSTRING))
);
Both WriteAllLines() and ReadLines() use enumerators, so should have relatively low memory usage.
I'm not sure how you would know to write the header only once, it depends on how you have your list of files to open available. Are they in an array? If so wrap the File.WriteAllLines call in a foreach loop around that array.
Something like this should do it (edited to reflect #JimMischel's comments):
private static void WriteFile(string mySearchString, string fileToWrite, params string[] filesToRead)
{
using (var sw = new StreamWriter(fileToWrite, true))
{
var count = 1;
foreach (var file in filesToRead)
{
using (var sr = new StreamReader(file))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (count == 3)
{
sw.WriteLine(line);
}
if (count > 3 && line.Contains(mySearchString))
{
sw.WriteLine(line);
}
count++;
}
}
}
}
}
You would call it like this:
WriteFile("Foobar", "fileToWrite.txt", "input1.txt", "input2.txt", "input3.txt");
You can use a StreamWriter for that :
using (var fs = new FileStream(outpuFilePath, FileMode.Append, FileAccess.Write))
{
using (var sw = new StreamWriter(fs))
{
foreach (var line in File.ReadLines(filepath).Where(line => line.Contains(myXML.searchSTRING)))
{
sw.WriteLine(line);
}
}
}
I think the most important thing is to use "Where" instead of "Any" Any returns a true/false, if a collection matches, whereas you want to filter the collection. Below should get you started in combination with the answers above (I would use Linq for clarity though).
StreamWriter outFile = new StreamWriter("output.txt");
string filepath = "infile.txt";
var header=File.ReadLines(filepath).Skip(2).First();
outFile.WriteLine(header);
var searchString = "temp";
File.ReadLines(filepath).Where(x => x.Contains(searchString))
.Select(x =>outFile.WriteLine(x));
Please read article for MemoryMappedFile
http://www.dotnetperls.com/memorymappedfile-benchmark
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
Is it possible to either see what the next line of a file is or write to the previous line of a file?
I am reading through a roughly 13,000 line file and if a line matches one of my regular expressions I then change the line, if not, it stays the same. These lines are getting written to a new file. It looks like this, roughly of course.
//create Streamreader sr
//create Streamwriter sw
//loop through file by line
//if line matches REGEX, change it. Else, don't change it
//write line to new file
//if end of file, close sr and sw
I need to either look to the next for ENDREC so I can write a new line before it
OR if the current line is ENDREC I need to write to the line before it. Any ideas?
If loading the whole file into memory isn't a problem, try something like this:
public void Test()
{
string fileName = "oldFileName";
string newFileName = "newFileName";
string[] allLines = File.ReadAllLines(fileName);
string changedLine = "Changed";
var changedLines = allLines.Select(p => ((Regex.IsMatch(p, "test")) ? changedLine : p));
File.WriteAllLines(newFileName, changedLines);
}
How about something like this?
var regex = new Regex(#"[a-zA-Z0-9_]+", RegexOptions.Compiled);
using (var reader = File.OpenText("in.txt"))
using (var writer = File.CreateText("out.txt"))
{
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var match = regex.Match(line);
if (match.Success)
{
// Alter the line however you wish here.
}
writer.WriteLine(line);
}
writer.Flush();
}