Re: GetFiles()

K

Kevin Spencer

According to the documentation, "wildcards are permitted." The only wildcard
mentioned was the asterix. In other words, you can use the asterix. Period.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
http://www.takempis.com
Complex things are made up of
lots of simple things.
 
Y

Yan-Hong Huang[MSFT]

Hello Mark,

Kevin is right. .NET has a great way to deal with custom sorting.

You need to create a class that implements IComparer that takes two objects and returns a number that determines whether
Object1 is greater than or less than Object2.

You can cast both Object's to FileInfo compare their LastWrittenTime properties and return which one should be higher and
lower.

When you call Array.Sort, you can pass in an instance of your IComparer to be used in the comparison.

For an example:

// ----- cut here -----

import System.IO
import System
import System.Collections

var files : FileInfo[] = new DirectoryInfo("c:\\temp").GetFiles()
System.Array.Sort(files, new FileSizeSorter)
print(files)

class FileSizeSorter implements IComparer
{
function Compare(a : Object, b : Object) : int
{
var f1 = FileInfo(a)
var f2 = FileInfo(b)

return f1.LastWriteTime - f2.LastWriteTime // you need to change this line of code :)
}
}

// ----- cut here -----


Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Content-Class: urn:content-classes:message
!From: "Mark Fox" <[email protected]>
!Sender: "Mark Fox" <[email protected]>
!References: <[email protected]> <#[email protected]>
!Subject: Re: GetFiles()
!Date: Tue, 5 Aug 2003 10:49:59 -0700
!Lines: 75
!Message-ID: <[email protected]>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: 7bit
!X-Newsreader: Microsoft CDO for Windows 2000
!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Thread-Index: AcNbef0AzDXlQBV4TDCTVDxqCKyPqg==
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:165073
!NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!Then how do people get an alphabetized list of files in a
!directory for an "Open File" dialog box in their .NET
!WinForms projects? There must be some way to get a
!sorted list of files.
!
!>-----Original Message-----
!>According to the documentation, "wildcards are
!permitted." The only wildcard
!>mentioned was the asterix. In other words, you can use
!the asterix. Period.
!>
!>--
!>HTH,
!>
!>Kevin Spencer
!>Microsoft MVP
!>..Net Developer
!>http://www.takempis.com
!>Complex things are made up of
!>lots of simple things.
!>
!>!>> Hello,
!>>
!>> I have a very basic question. I am using the
!>> System.IO.DirectoryInfo.GetFiles() to get array of the
!>> files in a folder. But I am looking to get a list of
!>> files that is sorted by the date written. Based on the
!>> documentation for the DirectoryInfo.GetFiles Method
!>> (String) overload, which says that you may pass "*.txt"
!>> or "s*" as an argument, I figured that I could pass the
!>> standard arguments for the "dir" command line tool.
!But
!>> when I use
!>>
!>> FileInfo[] fiArr = di.GetFiles("/o:-d /t:w");
!>>
!>> I get an exception:
!>>
!>> Message:
!>> Second path fragment must not be a drive or UNC name.
!>>
!>> Parameter name: path2
!>>
!>> Source (The application or object that caused the
!error):
!>> mscorlib
!>>
!>> The Method That Threw This Exception (Target Site):
!>> InternalCombine
!>>
!>> Stack Trace:
!>> at System.IO.Path.InternalCombine(String path1,
!String
!>> path2)
!>> at System.IO.DirectoryInfo.GetFiles(String
!>> searchPattern)
!>>
!>> I get the same error with
!>>
!>> FileInfo[] fiArr = di.GetFiles("/o:-d");
!>>
!>> Why doesn't this work? How do I get the GetFiles()
!>> method to return a sorted array? If you know how to
!get
!>> the sorted list of files _and_ restrict the search to
!>> JPEG or GIF files (either *.jpeg or *.jpg or *.gif),
!that
!>> would be even more awesome. Either way, I appreciate
!>> your help!
!>
!>
!>.
!>
!
 
Y

Yan-Hong Huang[MSFT]

Hello Mark,

Thanks very much for your positive feedback.

We are working on process to make sure that as many as possible managed posts could get email notification when getting
initial response. Now it is still under trial stage and hasn't applied to all groups yet. For now, it is better for managed users to
post their valid email into the group so that we could reach you. You could add some evident prefix or suffix to protect
yourself from spam emails and mention how to reach you in the signature. For an example:
email: Info_nospam@***.com
Signature: Please remove _nospam from the address to email me.

We are looking at continual improvement, and it's this kind of feedback that let's us know what things you prefer.

Keep the information coming.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Content-Class: urn:content-classes:message
!From: "Mark Fox" <[email protected]>
!Sender: "Mark Fox" <[email protected]>
!References: <[email protected]> <#[email protected]>
<[email protected]> <[email protected]>
!Subject: Re: GetFiles()
!Date: Wed, 6 Aug 2003 19:54:08 -0700
!Lines: 176
!Message-ID: <[email protected]>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: 7bit
!X-Newsreader: Microsoft CDO for Windows 2000
!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Thread-Index: AcNcjyviHONBFYCuTOKiuf+JvYZWEA==
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:165581
!NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!Yanhong and Kevin,
!
! Thank you so much for the generous hand holding and
!explanation. I hadn't thought of implementing IComparer
!to sort the array. Also, I received an e-mail
!notification when Yanhong replied. Getting the
!notification was _very_ helpful. Do you know whether
!that is a new standard feature that was added to the
!managed newsgroups? Will I receive them in the future?
!Thanks for all of your help.
!
!
!>-----Original Message-----
!>Hello Mark,
!>
!>Kevin is right. .NET has a great way to deal with custom
!sorting.
!>
!>You need to create a class that implements IComparer
!that takes two objects and returns a number that
!determines whether
!>Object1 is greater than or less than Object2.
!>
!>You can cast both Object's to FileInfo compare their
!LastWrittenTime properties and return which one should be
!higher and
!>lower.
!>
!>When you call Array.Sort, you can pass in an instance of
!your IComparer to be used in the comparison.
!>
!>For an example:
!>
!>// ----- cut here -----
!>
!>import System.IO
!>import System
!>import System.Collections
!>
!>var files : FileInfo[] = new DirectoryInfo
!("c:\\temp").GetFiles()
!>System.Array.Sort(files, new FileSizeSorter)
!>print(files)
!>
!>class FileSizeSorter implements IComparer
!>{
!> function Compare(a : Object, b : Object) : int
!> {
!> var f1 = FileInfo(a)
!> var f2 = FileInfo(b)
!>
!> return f1.LastWriteTime - f2.LastWriteTime // you need
!to change this line of code :)
!> }
!>}
!>
!>// ----- cut here -----
!>
!>
!>Best regards,
!>Yanhong Huang
!>Microsoft Online Partner Support
!>
!>Get Secure! - www.microsoft.com/security
!>This posting is provided "AS IS" with no warranties, and
!confers no rights.
!>
!>--------------------
!>!Content-Class: urn:content-classes:message
!>!From: "Mark Fox" <[email protected]>
!>!Sender: "Mark Fox" <[email protected]>
!>!References: <[email protected]>
!<#[email protected]>
!>!Subject: Re: GetFiles()
!>!Date: Tue, 5 Aug 2003 10:49:59 -0700
!>!Lines: 75
!>!Message-ID: <[email protected]>
!>!MIME-Version: 1.0
!>!Content-Type: text/plain;
!>! charset="iso-8859-1"
!>!Content-Transfer-Encoding: 7bit
!>!X-Newsreader: Microsoft CDO for Windows 2000
!>!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!>!Thread-Index: AcNbef0AzDXlQBV4TDCTVDxqCKyPqg==
!>!Newsgroups: microsoft.public.dotnet.framework.aspnet
!>!Path: cpmsftngxa06.phx.gbl
!>!Xref: cpmsftngxa06.phx.gbl
!microsoft.public.dotnet.framework.aspnet:165073
!>!NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
!>!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!>!
!>!Then how do people get an alphabetized list of files in
!a
!>!directory for an "Open File" dialog box in their .NET
!>!WinForms projects? There must be some way to get a
!>!sorted list of files.
!>!
!>!>-----Original Message-----
!>!>According to the documentation, "wildcards are
!>!permitted." The only wildcard
!>!>mentioned was the asterix. In other words, you can use
!>!the asterix. Period.
!>!>
!>!>--
!>!>HTH,
!>!>
!>!>Kevin Spencer
!>!>Microsoft MVP
!>!>..Net Developer
!>!>http://www.takempis.com
!>!>Complex things are made up of
!>!>lots of simple things.
!>!>
!>!>!>!>> Hello,
!>!>>
!>!>> I have a very basic question. I am using the
!>!>> System.IO.DirectoryInfo.GetFiles() to get array of
!the
!>!>> files in a folder. But I am looking to get a list of
!>!>> files that is sorted by the date written. Based on
!the
!>!>> documentation for the DirectoryInfo.GetFiles Method
!>!>> (String) overload, which says that you may
!pass "*.txt"
!>!>> or "s*" as an argument, I figured that I could pass
!the
!>!>> standard arguments for the "dir" command line tool.
!>!But
!>!>> when I use
!>!>>
!>!>> FileInfo[] fiArr = di.GetFiles("/o:-d /t:w");
!>!>>
!>!>> I get an exception:
!>!>>
!>!>> Message:
!>!>> Second path fragment must not be a drive or UNC name.
!>!>>
!>!>> Parameter name: path2
!>!>>
!>!>> Source (The application or object that caused the
!>!error):
!>!>> mscorlib
!>!>>
!>!>> The Method That Threw This Exception (Target Site):
!>!>> InternalCombine
!>!>>
!>!>> Stack Trace:
!>!>> at System.IO.Path.InternalCombine(String path1,
!>!String
!>!>> path2)
!>!>> at System.IO.DirectoryInfo.GetFiles(String
!>!>> searchPattern)
!>!>>
!>!>> I get the same error with
!>!>>
!>!>> FileInfo[] fiArr = di.GetFiles("/o:-d");
!>!>>
!>!>> Why doesn't this work? How do I get the GetFiles()
!>!>> method to return a sorted array? If you know how to
!>!get
!>!>> the sorted list of files _and_ restrict the search to
!>!>> JPEG or GIF files (either *.jpeg or *.jpg or *.gif),
!>!that
!>!>> would be even more awesome. Either way, I appreciate
!>!>> your help!
!>!>
!>!>
!>!>.
!>!>
!>!
!>
!>
!>.
!>
!
 
Y

Yan-Hong Huang[MSFT]

Hello Mark,

Good idea. You could refer to
http://support.microsoft.com/default.aspx?scid=/servicedesks/msdn/nospam.asp
&SD=msdn for an email alias that you could send feedback to.

Also, I will forward your feedback. I think for now, we may try to include
such email alias in our email signature.

Thanks very much.

Best regards,
Yanhong Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.

--------------------
!Content-Class: urn:content-classes:message
!From: "Mark Fox" <[email protected]>
!Sender: "Mark Fox" <[email protected]>
!References: <[email protected]>
<#[email protected]>
<[email protected]>
<[email protected]>
<[email protected]>
<viGti#[email protected]>
!Subject: Re: GetFiles()
!Date: Sun, 10 Aug 2003 15:54:32 -0700
!Lines: 271
!Message-ID: <[email protected]>
!MIME-Version: 1.0
!Content-Type: text/plain;
! charset="iso-8859-1"
!Content-Transfer-Encoding: 7bit
!X-Newsreader: Microsoft CDO for Windows 2000
!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!Thread-Index: AcNfkly0ebONKHDJR9eL/RctOhbIPg==
!Newsgroups: microsoft.public.dotnet.framework.aspnet
!Path: cpmsftngxa06.phx.gbl
!Xref: cpmsftngxa06.phx.gbl microsoft.public.dotnet.framework.aspnet:166583
!NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!
!Yanhong,
!
! You're welcome. It would be helpful to have an e-
!mail address or some web form on microsoft's website
!(even under the MSDN Subscriptions Download area) to
!submit suggestions and provide feedback. It would better
!to have a centralized spot to submit them to as opposed
!to just posting in the forums this way.
!
!
!>-----Original Message-----
!>Hello Mark,
!>
!>Thanks very much for your positive feedback.
!>
!>We are working on process to make sure that as many as
!possible managed posts could get email notification when
!getting
!>initial response. Now it is still under trial stage and
!hasn't applied to all groups yet. For now, it is better
!for managed users to
!>post their valid email into the group so that we could
!reach you. You could add some evident prefix or suffix to
!protect
!>yourself from spam emails and mention how to reach you
!in the signature. For an example:
!>email: Info_nospam@***.com
!>Signature: Please remove _nospam from the address to
!email me.
!>
!>We are looking at continual improvement, and it's this
!kind of feedback that let's us know what things you
!prefer.
!>
!>Keep the information coming.
!>
!>Best regards,
!>Yanhong Huang
!>Microsoft Online Partner Support
!>
!>Get Secure! - www.microsoft.com/security
!>This posting is provided "AS IS" with no warranties, and
!confers no rights.
!>
!>--------------------
!>!Content-Class: urn:content-classes:message
!>!From: "Mark Fox" <[email protected]>
!>!Sender: "Mark Fox" <[email protected]>
!>!References: <[email protected]>
!<#[email protected]>
!><[email protected]>
!<[email protected]>
!>!Subject: Re: GetFiles()
!>!Date: Wed, 6 Aug 2003 19:54:08 -0700
!>!Lines: 176
!>!Message-ID: <[email protected]>
!>!MIME-Version: 1.0
!>!Content-Type: text/plain;
!>! charset="iso-8859-1"
!>!Content-Transfer-Encoding: 7bit
!>!X-Newsreader: Microsoft CDO for Windows 2000
!>!X-MimeOLE: Produced By Microsoft MimeOLE V5.50.4910.0300
!>!Thread-Index: AcNcjyviHONBFYCuTOKiuf+JvYZWEA==
!>!Newsgroups: microsoft.public.dotnet.framework.aspnet
!>!Path: cpmsftngxa06.phx.gbl
!>!Xref: cpmsftngxa06.phx.gbl
!microsoft.public.dotnet.framework.aspnet:165581
!>!NNTP-Posting-Host: TK2MSFTNGXA14 10.40.1.166
!>!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!>!
!>!Yanhong and Kevin,
!>!
!>! Thank you so much for the generous hand holding
!and
!>!explanation. I hadn't thought of implementing
!IComparer
!>!to sort the array. Also, I received an e-mail
!>!notification when Yanhong replied. Getting the
!>!notification was _very_ helpful. Do you know whether
!>!that is a new standard feature that was added to the
!>!managed newsgroups? Will I receive them in the
!future?
!>!Thanks for all of your help.
!>!
!>!
!>!>-----Original Message-----
!>!>Hello Mark,
!>!>
!>!>Kevin is right. .NET has a great way to deal with
!custom
!>!sorting.
!>!>
!>!>You need to create a class that implements IComparer
!>!that takes two objects and returns a number that
!>!determines whether
!>!>Object1 is greater than or less than Object2.
!>!>
!>!>You can cast both Object's to FileInfo compare their
!>!LastWrittenTime properties and return which one should
!be
!>!higher and
!>!>lower.
!>!>
!>!>When you call Array.Sort, you can pass in an instance
!of
!>!your IComparer to be used in the comparison.
!>!>
!>!>For an example:
!>!>
!>!>// ----- cut here -----
!>!>
!>!>import System.IO
!>!>import System
!>!>import System.Collections
!>!>
!>!>var files : FileInfo[] = new DirectoryInfo
!>!("c:\\temp").GetFiles()
!>!>System.Array.Sort(files, new FileSizeSorter)
!>!>print(files)
!>!>
!>!>class FileSizeSorter implements IComparer
!>!>{
!>!> function Compare(a : Object, b : Object) : int
!>!> {
!>!> var f1 = FileInfo(a)
!>!> var f2 = FileInfo(b)
!>!>
!>!> return f1.LastWriteTime - f2.LastWriteTime // you
!need
!>!to change this line of code :)
!>!> }
!>!>}
!>!>
!>!>// ----- cut here -----
!>!>
!>!>
!>!>Best regards,
!>!>Yanhong Huang
!>!>Microsoft Online Partner Support
!>!>
!>!>Get Secure! - www.microsoft.com/security
!>!>This posting is provided "AS IS" with no warranties,
!and
!>!confers no rights.
!>!>
!>!>--------------------
!>!>!Content-Class: urn:content-classes:message
!>!>!From: "Mark Fox" <[email protected]>
!>!>!Sender: "Mark Fox" <[email protected]>
!>!>!References: <[email protected]>
!>!<#[email protected]>
!>!>!Subject: Re: GetFiles()
!>!>!Date: Tue, 5 Aug 2003 10:49:59 -0700
!>!>!Lines: 75
!>!>!Message-ID: <[email protected]>
!>!>!MIME-Version: 1.0
!>!>!Content-Type: text/plain;
!>!>! charset="iso-8859-1"
!>!>!Content-Transfer-Encoding: 7bit
!>!>!X-Newsreader: Microsoft CDO for Windows 2000
!>!>!X-MimeOLE: Produced By Microsoft MimeOLE
!V5.50.4910.0300
!>!>!Thread-Index: AcNbef0AzDXlQBV4TDCTVDxqCKyPqg==
!>!>!Newsgroups: microsoft.public.dotnet.framework.aspnet
!>!>!Path: cpmsftngxa06.phx.gbl
!>!>!Xref: cpmsftngxa06.phx.gbl
!>!microsoft.public.dotnet.framework.aspnet:165073
!>!>!NNTP-Posting-Host: TK2MSFTNGXA11 10.40.1.163
!>!>!X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
!>!>!
!>!>!Then how do people get an alphabetized list of files
!in
!>!a
!>!>!directory for an "Open File" dialog box in their .NET
!>!>!WinForms projects? There must be some way to get a
!>!>!sorted list of files.
!>!>!
!>!>!>-----Original Message-----
!>!>!>According to the documentation, "wildcards are
!>!>!permitted." The only wildcard
!>!>!>mentioned was the asterix. In other words, you can
!use
!>!>!the asterix. Period.
!>!>!>
!>!>!>--
!>!>!>HTH,
!>!>!>
!>!>!>Kevin Spencer
!>!>!>Microsoft MVP
!>!>!>..Net Developer
!>!>!>http://www.takempis.com
!>!>!>Complex things are made up of
!>!>!>lots of simple things.
!>!>!>
!>!>!>!>!>!>> Hello,
!>!>!>>
!>!>!>> I have a very basic question. I am using the
!>!>!>> System.IO.DirectoryInfo.GetFiles() to get array of
!>!the
!>!>!>> files in a folder. But I am looking to get a list
!of
!>!>!>> files that is sorted by the date written. Based
!on
!>!the
!>!>!>> documentation for the DirectoryInfo.GetFiles Method
!>!>!>> (String) overload, which says that you may
!>!pass "*.txt"
!>!>!>> or "s*" as an argument, I figured that I could
!pass
!>!the
!>!>!>> standard arguments for the "dir" command line
!tool.
!>!>!But
!>!>!>> when I use
!>!>!>>
!>!>!>> FileInfo[] fiArr = di.GetFiles("/o:-d /t:w");
!>!>!>>
!>!>!>> I get an exception:
!>!>!>>
!>!>!>> Message:
!>!>!>> Second path fragment must not be a drive or UNC
!name.
!>!>!>>
!>!>!>> Parameter name: path2
!>!>!>>
!>!>!>> Source (The application or object that caused the
!>!>!error):
!>!>!>> mscorlib
!>!>!>>
!>!>!>> The Method That Threw This Exception (Target Site):
!>!>!>> InternalCombine
!>!>!>>
!>!>!>> Stack Trace:
!>!>!>> at System.IO.Path.InternalCombine(String path1,
!>!>!String
!>!>!>> path2)
!>!>!>> at System.IO.DirectoryInfo.GetFiles(String
!>!>!>> searchPattern)
!>!>!>>
!>!>!>> I get the same error with
!>!>!>>
!>!>!>> FileInfo[] fiArr = di.GetFiles("/o:-d");
!>!>!>>
!>!>!>> Why doesn't this work? How do I get the GetFiles()
!>!>!>> method to return a sorted array? If you know how
!to
!>!>!get
!>!>!>> the sorted list of files _and_ restrict the search
!to
!>!>!>> JPEG or GIF files (either *.jpeg or *.jpg or
!*.gif),
!>!>!that
!>!>!>> would be even more awesome. Either way, I
!appreciate
!>!>!>> your help!
!>!>!>
!>!>!>
!>!>!>.
!>!>!>
!>!>!
!>!>
!>!>
!>!>.
!>!>
!>!
!>
!>
!>.
!>
!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top