Static vs. Dynamic Files Collection 
Author Message
 Static vs. Dynamic Files Collection

I'm trying to do an operation on each file in a collection.  What happens is
that after the operation is done and the file is saved the collection
registers this updated file as part of the collection and the operation is
attempted on  the file again.  I had the same problem when I used the For
DOS Batch command.  The workaround for that was that I also turned the
hidden attribute of the file on so that it wouldn't be seen for the rest of
the operations.  However, this does not work using VBScipt.  I'm sure that
there are some workarounds in VBScript such as moving the file after
alteration or setting the hidden attribute and manually checking for that
attribute, but is there a better way to ensure a static Files collection?


Sat, 14 Sep 2002 03:00:00 GMT  
 Static vs. Dynamic Files Collection

The best workaround is to move the processed files to a temporary
folder and then move them all back when done. The next best workaround
is to get the file count and then only iterate the For...Each loop for
that many times. For example:

set system = createobject("scripting.filesystemobject")
set folder = system.getfolder("c:\some folder")
filecount = folder.files.count
for each file in folder.files
    filecount = filecount - 1
    ' do something to file...
   if filecount = 0 then exit for
next

The only problem with the method above is that it makes the assumption
that all of the original files will be processed in the collection
before any "processed" files. This sounds logical, and I've never seen
it fail, but it's not a documented feature of collections, which is
why I prefer the first method.


: I'm trying to do an operation on each file in a collection.  What
happens is
: that after the operation is done and the file is saved the
collection
: registers this updated file as part of the collection and the
operation is
: attempted on  the file again.  I had the same problem when I used
the For
: DOS Batch command.  The workaround for that was that I also turned
the
: hidden attribute of the file on so that it wouldn't be seen for the
rest of
: the operations.  However, this does not work using VBScipt.  I'm
sure that
: there are some workarounds in VBScript such as moving the file after
: alteration or setting the hidden attribute and manually checking for
that
: attribute, but is there a better way to ensure a static Files
collection?
:
:



Sat, 14 Sep 2002 03:00:00 GMT  
 Static vs. Dynamic Files Collection
Moving the files is an option I'm considering.  I agree with the count
option that there's no guarantee that the files will be processed in order.
For the interim I will probably move the files to a new directory if there's
no static forms collection.  Thanks for your input.


Sat, 14 Sep 2002 03:00:00 GMT  
 Static vs. Dynamic Files Collection
I've dealt with this problem by first dumping the file collection into an
array and then just iterating through the array to do what I need to do on
the files.  This also lets you filter what you put into the array so you
only do your work on the files you want.

I tend to get nervouse moving files around, I'd rather work with an array.
I use this idea to split  large folders down to cd burning size and keep the
files I want in the correct folders.

a= Array()
nCount = 0
For Each Item in <files collection>
    Redim Preserve a(nCount)
    a(nCount) = Item.name
   nCount = nCount + 1
Next

For nNewCount = 0 to Ubound(a)
    filename = a(nNewCount)
    <do bad stuff to file>

Next

--
David Hamel

NBT Inc.

Quote:
> Moving the files is an option I'm considering.  I agree with the count
> option that there's no guarantee that the files will be processed in
order.
> For the interim I will probably move the files to a new directory if
there's
> no static forms collection.  Thanks for your input.



Sat, 14 Sep 2002 03:00:00 GMT  
 Static vs. Dynamic Files Collection


Quote:
> I've dealt with this problem by first dumping the file collection into an
> array and then just iterating through the array to do what I need to do on
> the files.  This also lets you filter what you put into the array so you
> only do your work on the files you want.

> I tend to get nervouse moving files around, I'd rather work with an array.
> I use this idea to split  large folders down to cd burning size and keep
the
> files I want in the correct folders.

> a= Array()
> nCount = 0
> For Each Item in <files collection>
>     Redim Preserve a(nCount)
>     a(nCount) = Item.name
>    nCount = nCount + 1
> Next

or more efficiently, you should be able to do the redim once at the top...

a= Array()
Redim Preserve a(<file collection>.Count)

 nCount = 0
 For Each Item in <files collection>
     a(nCount) = Item.name
    nCount = nCount + 1
 Next



Sat, 14 Sep 2002 03:00:00 GMT  
 Static vs. Dynamic Files Collection

Attached is a script component (FileFilter.wsc)...

Methods:
  GetFiles(oFolder,sFilter)
  Returns a sorted array of filtered file objects

Properties:
  fso
  shell

Usage example:

  set ff = createobject("filefilter.wsc")
  set fo = ff.fso.getfolder("c:\")
  '
  'Tip: use *.* if what you REALLY want
  '     is the sorted array equivalent
  '     of the Files collection of the
  '     passed folder.
  '
  arFiles = ff.getfiles(fo,"*.txt")
  for each fi in arFiles
    s = s & br & fi.path
    br = vbcrlf
  next
  msgbox s

--
Michael Harris
MVP Scripting

I've dealt with this problem by first dumping the file collection into an
array and then just iterating through the array to do what I need to do on
the files.  This also lets you filter what you put into the array so you
only do your work on the files you want.

I tend to get nervouse moving files around, I'd rather work with an array.
I use this idea to split  large folders down to cd burning size and keep the
files I want in the correct folders.

a= Array()
nCount = 0
For Each Item in <files collection>
    Redim Preserve a(nCount)
    a(nCount) = Item.name
   nCount = nCount + 1
Next

For nNewCount = 0 to Ubound(a)
    filename = a(nNewCount)
    <do bad stuff to file>

Next

--
David Hamel

NBT Inc.

Quote:
> Moving the files is an option I'm considering.  I agree with the count
> option that there's no guarantee that the files will be processed in
order.
> For the interim I will probably move the files to a new directory if
there's
> no static forms collection.  Thanks for your input.

  FileFilter.wsc
2K Download


Sat, 14 Sep 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. Changing from Static IP to Dynamic - Display Window

2. WSH to change a machine from Static IP to Dynamic

3. ASP: static vs. dynamic files - opinion please?

4. isapi returned vs static html

5. checkbox collection vs single checkbox

6. collection vs. dictionary :-(

7. checkbox collection vs single checkbox

8. dynamic onclick using innerHTML vs el.attachEvent

9. Static Pdf OutPut File

10. Copying Files in Jscript using File Collection

11. Sorting files in the Files Collection?

12. Enumerator won't enumerate all files in Files collection

 

 
Powered by phpBB® Forum Software