FileSystem Object Read Order 
Author Message
 FileSystem Object Read Order

When I use the MS example script for reading every file in a directory, I
notice that the order that the files in the files collection are returned
are in ascending order according to filename.

' Create FileSytemObject Component
   Set ScriptObject = CreateObject("Scripting.FileSystemObject")
   Set f = ScriptObject.GetFolder("c:\Inetpub\wwwroot\")
   Set fc = f.Files
   For Each f1 in fc
    s = f + "\" + f1.name
    Set MyFile = ScriptObject.OpenTextFile(s, ForReading)
    Response.Write MyFile.ReadAll
   Next

 Does anyone know of a way to sort files in a files collection in descending
order according to filename?

Thanks,

--
Patrick Burgess



Sat, 01 Jun 2002 03:00:00 GMT  
 FileSystem Object Read Order

It's pure coincidence that you get ascending order.  The order is (more or less) in order of creation or perhaps addition to the folder.

You can't sort the files collection.  You need to build an array of filenames, sort the array, then use the array to output filenames or use the array to index back into the Files collection in sorted order if you need dates/size/etc..  As simple bubble sort is OK for small arrays.

You can index into the Files collection with a simple string - Files("myfile.ext") or a variable (including an array element reference) -  Files((arSorted(n))).  In the second case, you'll get a runtime error unless you wrap the entire variable reference in ()s as I did in the example below.

Here's a .wsf (WSH 2.0) example:

<job>
<object id='fso' progid='Scripting.FilesystemObject' reference='yes' />
<script language='VBScript'>

'====================================================
'An example of sorting file names with a simple bubble sort.
'====================================================

set oFolder = fso.GetFolder("c:\")
set oFiles = oFolder.Files

Redim arFiles(oFiles.Count -1)
n = -1

'load the array with file names
'
For Each oFile in oFiles
  n = n + 1
  arFiles(n) = oFile.Name
next

'ascending sort order
'
For n = 0 to ubound(arFiles) -1
  For m = n+1 to ubound(arFiles)
    if lcase(arFiles(m)) < lcase(arFiles(n)) then
      temp = arFiles(m)
      arFiles(m) = arFiles(n)
      arFiles(n) = temp
    end if
  Next
Next

msgbox join(arFiles,vbcrlf)

'use the sorted array to index back
'into the Files collection and list path
'
s = ""
for each f in arFiles
  'wrapping f in extra ()s is a _must_!
  s = s & oFiles((f)).path & vbcrlf
next
msgbox s

'descending sort order
'
For n = 0 to ubound(arFiles) -1
  For m = n+1 to ubound(arFiles)
    if lcase(arFiles(m)) > lcase(arFiles(n)) then
      temp = arFiles(m)
      arFiles(m) = arFiles(n)
      arFiles(n) = temp
    end if
  Next
Next

msgbox join(arFiles,vbcrlf)

'use the sorted array to index back
'into the Files collection and list path
'
s = ""
for each f in arFiles
  'wrapping f in extra ()s is a _must_!
  s = s & oFiles((f)).path & vbcrlf
next
msgbox s

</script>
</job>

-
Michael Harris


  When I use the MS example script for reading every file in a directory, I
  notice that the order that the files in the files collection are returned
  are in ascending order according to filename.

  ' Create FileSytemObject Component
     Set ScriptObject = CreateObject("Scripting.FileSystemObject")
     Set f = ScriptObject.GetFolder("c:\Inetpub\wwwroot\")
     Set fc = f.Files
     For Each f1 in fc
      s = f + "\" + f1.name
      Set MyFile = ScriptObject.OpenTextFile(s, ForReading)
      Response.Write MyFile.ReadAll
     Next

   Does anyone know of a way to sort files in a files collection in descending
  order according to filename?

  Thanks,

  --
  Patrick Burgess



Sat, 01 Jun 2002 03:00:00 GMT  
 FileSystem Object Read Order
Thanks Michael! That was just the trick.


It's pure coincidence that you get ascending order.  The order is (more or
less) in order of creation or perhaps addition to the folder.

You can't sort the files collection.  You need to build an array of
filenames, sort the array, then use the array to output filenames or use the
array to index back into the Files collection in sorted order if you need
dates/size/etc..  As simple bubble sort is OK for small arrays.

You can index into the Files collection with a simple string -
Files("myfile.ext") or a variable (including an array element reference) -
Files((arSorted(n))).  In the second case, you'll get a runtime error unless
you wrap the entire variable reference in ()s as I did in the example below.

Here's a .wsf (WSH 2.0) example:

<job>
<object id='fso' progid='Scripting.FilesystemObject' reference='yes' />
<script language='VBScript'>

'====================================================
'An example of sorting file names with a simple bubble sort.
'====================================================

set oFolder = fso.GetFolder("c:\")
set oFiles = oFolder.Files

Redim arFiles(oFiles.Count -1)
n = -1

'load the array with file names
'
For Each oFile in oFiles
  n = n + 1
  arFiles(n) = oFile.Name
next

'ascending sort order
'
For n = 0 to ubound(arFiles) -1
  For m = n+1 to ubound(arFiles)
    if lcase(arFiles(m)) < lcase(arFiles(n)) then
      temp = arFiles(m)
      arFiles(m) = arFiles(n)
      arFiles(n) = temp
    end if
  Next
Next

msgbox join(arFiles,vbcrlf)

'use the sorted array to index back
'into the Files collection and list path
'
s = ""
for each f in arFiles
  'wrapping f in extra ()s is a _must_!
  s = s & oFiles((f)).path & vbcrlf
next
msgbox s

'descending sort order
'
For n = 0 to ubound(arFiles) -1
  For m = n+1 to ubound(arFiles)
    if lcase(arFiles(m)) > lcase(arFiles(n)) then
      temp = arFiles(m)
      arFiles(m) = arFiles(n)
      arFiles(n) = temp
    end if
  Next
Next

msgbox join(arFiles,vbcrlf)

'use the sorted array to index back
'into the Files collection and list path
'
s = ""
for each f in arFiles
  'wrapping f in extra ()s is a _must_!
  s = s & oFiles((f)).path & vbcrlf
next
msgbox s

</script>
</job>

-
Michael Harris



  When I use the MS example script for reading every file in a directory, I
  notice that the order that the files in the files collection are returned
  are in ascending order according to filename.

  ' Create FileSytemObject Component
     Set ScriptObject = CreateObject("Scripting.FileSystemObject")
     Set f = ScriptObject.GetFolder("c:\Inetpub\wwwroot\")
     Set fc = f.Files
     For Each f1 in fc
      s = f + "\" + f1.name
      Set MyFile = ScriptObject.OpenTextFile(s, ForReading)
      Response.Write MyFile.ReadAll
     Next

   Does anyone know of a way to sort files in a files collection in
descending
  order according to filename?

  Thanks,

  --
  Patrick Burgess



Sun, 02 Jun 2002 03:00:00 GMT  
 FileSystem Object Read Order
Patrick,
what would be the code needed to append to the file created below?

Quote:
>     Set MyFile = ScriptObject.OpenTextFile(s, ForReading)

thanks
Janice Sutherland
Hewlett - Packard



Quote:
> When I use the MS example script for reading every file in a directory, I
> notice that the order that the files in the files collection are returned
> are in ascending order according to filename.

> ' Create FileSytemObject Component
>    Set ScriptObject = CreateObject("Scripting.FileSystemObject")
>    Set f = ScriptObject.GetFolder("c:\Inetpub\wwwroot\")
>    Set fc = f.Files
>    For Each f1 in fc
>     s = f + "\" + f1.name
>     Set MyFile = ScriptObject.OpenTextFile(s, ForReading)
>     Response.Write MyFile.ReadAll
>    Next

>  Does anyone know of a way to sort files in a files collection in
descending
> order according to filename?

> Thanks,

> --
> Patrick Burgess




Sun, 09 Jun 2002 03:00:00 GMT  
 FileSystem Object Read Order
Const ForAppending = 8

This is all in the documentation ;-)

--
Michael Harris


| Patrick,
| what would be the code needed to append to the file created below?
|
| >     Set MyFile = ScriptObject.OpenTextFile(s, ForReading)
|
| thanks
| Janice Sutherland
| Hewlett - Packard
|



| > When I use the MS example script for reading every file in a directory, I
| > notice that the order that the files in the files collection are returned
| > are in ascending order according to filename.
| >
| > ' Create FileSytemObject Component
| >    Set ScriptObject = CreateObject("Scripting.FileSystemObject")
| >    Set f = ScriptObject.GetFolder("c:\Inetpub\wwwroot\")
| >    Set fc = f.Files
| >    For Each f1 in fc
| >     s = f + "\" + f1.name
| >     Set MyFile = ScriptObject.OpenTextFile(s, ForReading)
| >     Response.Write MyFile.ReadAll
| >    Next
| >
| >  Does anyone know of a way to sort files in a files collection in
| descending
| > order according to filename?
| >
| > Thanks,
| >
| > --
| > Patrick Burgess

| >
| >
|
|



Sun, 09 Jun 2002 03:00:00 GMT  
 FileSystem Object Read Order
thank  you,
I did not have documentation at the time.

js

Quote:
> Const ForAppending = 8

> This is all in the documentation ;-)

> --
> Michael Harris



> | Patrick,
> | what would be the code needed to append to the file created below?
> |
> | >     Set MyFile = ScriptObject.OpenTextFile(s, ForReading)
> |
> | thanks
> | Janice Sutherland
> | Hewlett - Packard
> |



> | > When I use the MS example script for reading every file in a
directory, I
> | > notice that the order that the files in the files collection are
returned
> | > are in ascending order according to filename.
> | >
> | > ' Create FileSytemObject Component
> | >    Set ScriptObject = CreateObject("Scripting.FileSystemObject")
> | >    Set f = ScriptObject.GetFolder("c:\Inetpub\wwwroot\")
> | >    Set fc = f.Files
> | >    For Each f1 in fc
> | >     s = f + "\" + f1.name
> | >     Set MyFile = ScriptObject.OpenTextFile(s, ForReading)
> | >     Response.Write MyFile.ReadAll
> | >    Next
> | >
> | >  Does anyone know of a way to sort files in a files collection in
> | descending
> | > order according to filename?
> | >
> | > Thanks,
> | >
> | > --
> | > Patrick Burgess

> | >
> | >
> |
> |



Sat, 22 Jun 2002 03:00:00 GMT  
 FileSystem Object Read Order
How do you read the contents of a particular file?
Scott


Quote:
> When I use the MS example script for reading every file in a directory, I
> notice that the order that the files in the files collection are returned
> are in ascending order according to filename.

> ' Create FileSytemObject Component
>    Set ScriptObject = CreateObject("Scripting.FileSystemObject")
>    Set f = ScriptObject.GetFolder("c:\Inetpub\wwwroot\")
>    Set fc = f.Files
>    For Each f1 in fc
>     s = f + "\" + f1.name
>     Set MyFile = ScriptObject.OpenTextFile(s, ForReading)
>     Response.Write MyFile.ReadAll
>    Next

>  Does anyone know of a way to sort files in a files collection in
descending
> order according to filename?

> Thanks,

> --
> Patrick Burgess




Sat, 03 Aug 2002 03:00:00 GMT  
 
 [ 7 post ] 

 Relevant Pages 

1. FileSystem Object Read Order

2. Reading the Binary file using FileSystem Object

3. dictionary objects: key in order = sequential out order?

4. Extract Image out of OLE Object Field to Filesystem

5. filesystem object

6. filesystem object and UNC?

7. FileSystem Object

8. Filesystem Object

9. filesystem object fails to open long name

10. question on FILESYSTEM object

11. use of the filesystem object

12. performance of FileSystem-object

 

 
Powered by phpBB® Forum Software