Solved

Open CSV file, copy first cell of A1 to clipboard, delete it, save file

Posted on 2013-05-15
5
357 Views
Last Modified: 2013-07-10
Hi everyone,

I am using a Macro Recorder software that allows C# scripts to be used as part of the macro.

The script I need to adapt is this:

public class Program
{
	public static void Main()
	{
		System.Windows.Forms.MessageBox.Show("DOSOMETHING");
	}
}

Open in new window


What I need the C# to do is this:
1) Open a .csv file on my PC
2) Copy the first cell, e.g. A1 to clipboard
3) Delete A1
4) Save file

Can anyone help? Would make my life easier!

Thanks,

Ben
0
Comment
Question by:intangiblemedia
  • 3
  • 2
5 Comments
 
LVL 42

Accepted Solution

by:
sedgwick earned 500 total points
ID: 39167799
var lines = File.ReadLines("input.csv");
var line = lines.FirstOrDefault();
var cell = line.Split(',').FirstOrDefault();
Clipboard.SetText(cell);
using (StreamWriter sw = new StreamWriter("input.csv"))
{
	sw.WriteLine(string.Join(",",line.Split(',').Skip(1).ToArray()));
	
	foreach(var l in lines.Skip(1))
	{
		sw.WriteLine(l);
	}
}

Open in new window

0
 

Author Comment

by:intangiblemedia
ID: 39167830
Thanks - does this also delete the row after its been copied to clipboard and save file?
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39168147
It copies and deletes the 1st cell in the 1st line
0
 

Author Comment

by:intangiblemedia
ID: 39168848
Thank you for your post - but I have tried including it in the function above and it wont work.

I get error messages like:
"The type or namespace name "var" could not be found"
0
 
LVL 42

Expert Comment

by:sedgwick
ID: 39169008
use string instead of var.
Except In the first line use string[] instead of var.
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Although it can be difficult to imagine, someday your child will have a career of his or her own. He or she will likely start a family, buy a home and start having their own children. So, while being a kid is still extremely important, it’s also …
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

803 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question