Solved

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

Posted on 2013-05-15
5
355 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
In this fourth video of the Xpdf series, we discuss and demonstrate the PDFinfo utility, which retrieves the contents of a PDF's Info Dictionary, as well as some other information, including the page count. We show how to isolate the page count in a…

930 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now