Follow @PDSAInc I Love the File/Directory Classes in .NET! - Paul Sheriff's Blog for the Real World

Paul Sheriff's Blog for the Real World

This blog is to share my tips and tricks garnered over 25+ years in the IT industry

Paul's Favorites

I Love the File/Directory Classes in .NET!

I just had a situation where a potential client gave me a zip file of about 200 files. They wanted us to give them an estimate on how long it would take us to recreate each screen in ASP.NET 2.0. I did not relish the thought of typing in all those file names into an Excel spreadsheet so I could assign hours to each one. So I wrote a simple little utility to grab each file in the folder, and add that file name to a StringBuilder object. I separated each file name with a CRLF. Once I had all of the files in the StringBuilder object, I simply copied the text to the Clipboard. Then I opened up Excel and pasted the contents of the Clipboard into an Excel column. Bingo! I now had all those file names into Excel, ready for me to assign hours to!

C# Code
private void GetFiles()
{
  System.Text.StringBuilder sb = new System.Text.StringBuilder(1024);
  System.IO.FileInfo fil;

  foreach (string strFile in System.IO.Directory.GetFiles(@"D:\Customers\ASP"))
  {
    fil = new System.IO.FileInfo(strFile);
    sb.Append(fil.Name.Replace(".asp", "") + Environment.NewLine);
  }

  Clipboard.SetText(sb.ToString());
}

VB.NET Code
Private Sub GetFiles()
  Dim sb As New System.Text.StringBuilder(1024)
  Dim fil As System.IO.FileInfo

  For Each strFile As String In System.IO.Directory.GetFiles("D:\Customer\ASP")
    fil = New System.IO.FileInfo(strFile)
    sb.Append(fil.Name.Replace(".asp", "") & Environment.NewLine)
  Next

  Clipboard.SetText(sb.ToString())
End Sub

I hope this little tip helps you the next time you have to do something like this!

Paul

Posted: Jun 15 2007, 05:53 PM by psheriff | with 4 comment(s)
Filed under:

Comments

Joe said:

for %i in ("*.asp") do echo %~ni >> filelist.csv

# June 16, 2007 4:23 AM

Rob said:

Admittedly it wouldn't have been quite as geeky ;) But, the command prompt could have done 99% of the grunt-work there with it's /D parameter, and then by piping it to a file called filelist.csv you'd haev been pretty much there. But - it is pretty cool just how much you can do with .net without writing reams and reams of code, particularly around file/directory metadata
# June 16, 2007 5:24 AM

Mischa Kroon said:

it's a matter of preference but for me it's:

run command prompt, goto correct dir.

dir /b > mylist.txt

open in excel

replace .asp > nothing

# June 16, 2007 9:42 AM

Visual Basic said:

Thank you so much. This article was great!

# December 19, 2007 2:20 PM