Solved

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

Posted on 2013-05-15
5
353 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
Comment Utility
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
Comment Utility
Thanks - does this also delete the row after its been copied to clipboard and save file?
0
 
LVL 42

Expert Comment

by:sedgwick
Comment Utility
It copies and deletes the 1st cell in the 1st line
0
 

Author Comment

by:intangiblemedia
Comment Utility
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
Comment Utility
use string instead of var.
Except In the first line use string[] instead of var.
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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…
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…

762 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

9 Experts available now in Live!

Get 1:1 Help Now