Solved

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

Posted on 2013-05-15
5
364 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Transaction Monitoring Vs. Real User Monitoring

Synthetic Transaction Monitoring Vs. Real User Monitoring: When To Use Each Approach? In this article, we will discuss two major monitoring approaches: Synthetic Transaction and Real User Monitoring.

Question has a verified solution.

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

Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
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
Six Sigma Control Plans
Progress

696 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