Solved

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

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

A short article about problems I had with the new location API and permissions in Marshmallow
This is about my first experience with programming Arduino.
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …

830 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