Problem with GetFiles() or me?

2010-03-05 Thread Richard Carde
Back to a programming topic...(rare for me).


I've knocked up a small console application to process a folder structure
with hundreds of thousands of small text files, read each file, strip off
some headers and append the resulting data into a single large file.  The
all worked fine until I needed to do it on a different path (mapped drive).


If I specify the path (first argument) as a drive letter (of a mapped drive)
only, and that drive has a current working directory other than the root, it
fails because GetFiles() returns an absolute path which is incorrect - it
prepends the filenames with a \.


eg:


H:\CD Z:\data_to_process

H:\GetFilesTest.exe Z:

Processing file Z:\file1.txt

Processing file Z:\file2.txt

...


This isn't correct. While it correctly enumerates the files within the
folder structure as specified, the path should be Z:file1.txt, etc.  Surely?


Tried with VS2008  .Net 3.5 as well as VS2010 RC  .Net 4 - same behaviour.


A quick Google didn't turn up anything obvious.  So I'm thinking I've missed
something in the documentation (which I must admit, I didn't read until it
didn't work as expected) or am clueless about the vagaries of DOS
working/current directories because this seems so simple.

--8K---8K---8K--
using System;
using System.IO;

namespace GetFilesTest
{
class Program
{
static void Main(string[] args)
{
if (args.Length != 1)
{
Console.WriteLine(Please specify a path);
return;
}

// enumerate all files in the supplied path and subfolders
string[] files = Directory.GetFiles(args[0], cdr*.*,
SearchOption.AllDirectories);

Console.WriteLine(String.Format(Getting files from {0},
args[0]));

// iterate over the files, displaying the full path
foreach (string file in files)
{
Console.WriteLine(String.Format(Processing file {0},
file));
}
}
}
}
--8K---8K---8K--

-- 
Richard Carde


Re: Problem with GetFiles() or me?

2010-03-05 Thread Mark Hurd
Workaround: D:.

Adding the explicit current directory causes the returned strings to
be valid paths.

Do you want to add the bug to Connect?

-- 
Regards,
Mark Hurd, B.Sc.(Ma.)(Hons.)

(BTW Typos fixed below.)

On Sat, Mar 6, 2010 at 1:35 AM, Mark Hurd markeh...@gmail.com wrote:
 On Sat, Mar 6, 2010 at 12:21 AM, Richard Carde rich...@carde.id.au wrote:
 snip

 If I specify the path (first argument) as a drive letter (of a mapped drive)
 only, and that drive has a current working directory other than the root, it
 fails because GetFiles() returns an absolute path which is incorrect - it
 prepends the filenames with a \.

 eg:

 H:\CD Z:\data_to_process

 H:\GetFilesTest.exe Z:

 Processing file Z:\file1.txt

 Processing file Z:\file2.txt

 ...

 This isn't correct. While it correctly enumerates the files within the
 folder structure as specified, the path should be Z:file1.txt, etc.  Surely?

 I agree (and can confirm with DotLisp and .NET 2.0) and I think you've
 found a bug.

 Note that files in the current folder don't have the \ inserted.
 That is your example above is wrong, at least for me:

 T:windows-gcl-saved_acl2.zip.txt
 T:\lu\README.TXT

 The above is a sample of the output from
 (Directory:GetFiles T: *.txt System.IO.SearchOption:AllDirectories)

 Tried with VS2008  .Net 3.5 as well as VS2010 RC  .Net 4 - same behaviour.

 I think I can see the problem using Reflector in
 Directory.InternalGetFileDirectoryNames:

 Where it says:

  If (data.userPath.Length  0) Then
  ch = data.userPath.Chars((data.userPath.Length - 1))
  flag2 = ((ch = Path.DirectorySeparatorChar) OrElse (ch =
 Path.AltDirectorySeparatorChar))
  End If

 I believe it should be
  flag2 = ((ch = Path.DirectorySeparatorChar) OrElse (ch =
 Path.AltDirectorySeparatorChar) OrElse (ch =
 Path.VolumeSeparatorChar))

 But InternalGetFileDirectoryNames is quite complex...

 snip
 --
 Richard Carde

 --
 Regards,
 Mark Hurd, B.Sc.(Ma.)(Hons.)