Iterating through all files on Hard drive 
Author Message
 Iterating through all files on Hard drive

I want to write a script which iterates through all the folders and files on
my C drive and find all files with .tmp extensions

Then I want to delete them

Can anybody help me with the code?

The subfolders property only sees one level down from the parent folder, so
how do you iterate through deeper folders?

Regards
Steve



Sun, 27 Feb 2005 12:43:53 GMT  
 Iterating through all files on Hard drive
Quote:
> how do you iterate through deeper folders

Use a recursive function that calls itself.

I wouldn't use the FSO if the folders in question have thousands of files in
them, since it cache's them all up (eek).

+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
Monte Hansen
http://KillerVB.com
+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+


Quote:
> I want to write a script which iterates through all the folders and files
on
> my C drive and find all files with .tmp extensions

> Then I want to delete them

> Can anybody help me with the code?

> The subfolders property only sees one level down from the parent folder,
so
> how do you iterate through deeper folders?

> Regards
> Steve



Sun, 27 Feb 2005 14:07:58 GMT  
 Iterating through all files on Hard drive
Same thing happens with using the shell DIR command, of course.

I recall a discussion in here early this year where Michael Harris ran a
search via FSO and via DIR. speeds were roughly similar, but what was
interesting was what happened if you ran the same search twice in rapid
succession: time taken would drop by a factor of 4-8 or so.

--
Please respond in the newsgroup so everyone may benefit.
 http://dev.remotenetworktechnology.com
 ----------
 Subscribe to Microsoft's Security Bulletins:
 http://www.microsoft.com/technet/security/bulletin/notify.asp


Quote:
> > how do you iterate through deeper folders
> Use a recursive function that calls itself.

> I wouldn't use the FSO if the folders in question have thousands of
files in
> them, since it cache's them all up (eek).

> +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
> Monte Hansen
> http://KillerVB.com
> +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+



> > I want to write a script which iterates through all the folders and
files
> on
> > my C drive and find all files with .tmp extensions

> > Then I want to delete them

> > Can anybody help me with the code?

> > The subfolders property only sees one level down from the parent
folder,
> so
> > how do you iterate through deeper folders?

> > Regards
> > Steve



Sun, 27 Feb 2005 17:15:34 GMT  
 Iterating through all files on Hard drive

Quote:

> I want to write a script which iterates through all the folders and files on
> my C drive and find all files with .tmp extensions

> Then I want to delete them

> Can anybody help me with the code?

> The subfolders property only sees one level down from the parent folder, so
> how do you iterate through deeper folders?

Hi

Below is a VBScript that deletes files with a certain file extension (it is
based on a Michael Harris script that deletes files based on date).

If bIncludeSubFolders is set to True, it will include subfolders

' folder to start search in...
path = "c:\"

' delete files with this extension...
killextension = "tmp"

arFiles = Array()
set fso = createobject("scripting.filesystemobject")

' Don't do the delete while you still are looping through a
' file collection returned from the File System Object (FSO).
' The collection may get mixed up.
' Create an array of the file objects to avoid this.
'
SelectFiles path, killextension, arFiles, true

nDeleted = 0
for n = 0 to ubound(arFiles)

'=================================================

  ' Files deleted via FSO methods do *NOT* go to the recycle bin!!!

'=================================================

  on error resume next 'in case of 'in use' files...
  arFiles(n).delete True

  if err.number <> 0 then
    wscript.echo "Unable to delete: " & arFiles(n).path
  else
    nDeleted = nDeleted + 1
  end if
  on error goto 0
next

msgbox nDeleted & " of " & ubound(arFiles)+1 _
  & " eligible files were deleted"

sub SelectFiles(sPath,vKillExtension,arFilesToKill,bIncludeSubFolders)
   on error resume next
  'select files to delete and add to array...
  '
  set folder = fso.getfolder(sPath)
  set files = folder.files

  for each file in files
    if lcase(fso.getextensionname(file)) = lcase(vKillExtension) then
      count = ubound(arFilesToKill) + 1
      redim preserve arFilesToKill(count)
      set arFilesToKill(count) = file
    end if
  next

  if bIncludeSubFolders then
    for each fldr in folder.subfolders
      SelectFiles fldr.path,vKillExtension,arFilesToKill,true
    next
  end if
end sub

--
torgeir



Sun, 27 Feb 2005 17:43:40 GMT  
 Iterating through all files on Hard drive

Quote:
> I recall a discussion in here early this year where Michael Harris ran a
> search via FSO and via DIR. speeds were roughly similar, but what was

I'm not surprised. But FSO was supposed to be a "step up" :>. I just can't
believe they designed it such that you have to access folder contents as an
array. Sad thing, they went and did it again in .NET (hello, McFly). The
designers didn't realize that folders can contain thousounds upon thousands
of files. They should have used an enumerator-based interface instead.

Quote:
> interesting was what happened if you ran the same search twice in rapid
> succession: time taken would drop by a factor of 4-8 or so.

That's the cache hit by the disk and/or O/S.

Regards,

+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
Monte Hansen
http://KillerVB.com
+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+



Quote:
> Same thing happens with using the shell DIR command, of course.

> I recall a discussion in here early this year where Michael Harris ran a
> search via FSO and via DIR. speeds were roughly similar, but what was
> interesting was what happened if you ran the same search twice in rapid
> succession: time taken would drop by a factor of 4-8 or so.

> --
> Please respond in the newsgroup so everyone may benefit.
>  http://dev.remotenetworktechnology.com
>  ----------
>  Subscribe to Microsoft's Security Bulletins:
>  http://www.microsoft.com/technet/security/bulletin/notify.asp



> > > how do you iterate through deeper folders
> > Use a recursive function that calls itself.

> > I wouldn't use the FSO if the folders in question have thousands of
> files in
> > them, since it cache's them all up (eek).

> > +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
> > Monte Hansen
> > http://KillerVB.com
> > +|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+



> > > I want to write a script which iterates through all the folders and
> files
> > on
> > > my C drive and find all files with .tmp extensions

> > > Then I want to delete them

> > > Can anybody help me with the code?

> > > The subfolders property only sees one level down from the parent
> folder,
> > so
> > > how do you iterate through deeper folders?

> > > Regards
> > > Steve



Sun, 27 Feb 2005 22:03:11 GMT  
 Iterating through all files on Hard drive

Quote:
> Same thing happens with using the shell DIR command, of course.

> I recall a discussion in here early this year where Michael Harris ran a
> search via FSO and via DIR. speeds were roughly similar,

Hi

I think you remember wrong here. It was WMI cim_datafile and dir that was
roughly similar, FSO is left in the dust by dir or a optimized cim_datafile
query.

Here is the post from Michael:


Subject: Re: Find all files of type doc in C:\ and subdirectories
Newsgroups: microsoft.public.scripting.vbscript
Date: 2001-11-14 13:17:30 PST
http://groups.google.com/groups?selm=%23aBF7FVbBHA.1996%40tkmsftngp03

--
torgeir



Sun, 27 Feb 2005 22:11:40 GMT  
 Iterating through all files on Hard drive
Yep, I totally misremembered everything but the caching:


--
Please respond in the newsgroup so everyone may benefit.
 http://dev.remotenetworktechnology.com
 ----------
 Subscribe to Microsoft's Security Bulletins:
 http://www.microsoft.com/technet/security/bulletin/notify.asp


Quote:

> > Same thing happens with using the shell DIR command, of course.

> > I recall a discussion in here early this year where Michael Harris
ran a
> > search via FSO and via DIR. speeds were roughly similar,

> Hi

> I think you remember wrong here. It was WMI cim_datafile and dir that
was
> roughly similar, FSO is left in the dust by dir or a optimized
cim_datafile
> query.

> Here is the post from Michael:


> Subject: Re: Find all files of type doc in C:\ and subdirectories
> Newsgroups: microsoft.public.scripting.vbscript
> Date: 2001-11-14 13:17:30 PST
> http://groups.google.com/groups?selm=%23aBF7FVbBHA.1996%40tkmsftngp03

> --
> torgeir



Mon, 28 Feb 2005 01:12:04 GMT  
 Iterating through all files on Hard drive

Quote:

> > I recall a discussion in here early this year where Michael Harris ran a
> > search via FSO and via DIR. speeds were roughly similar, but what was

> I'm not surprised. But FSO was supposed to be a "step up" :>. I just can't
> believe they designed it such that you have to access folder contents as an
> array. Sad thing, they went and did it again in .NET (hello, McFly). The
> designers didn't realize that folders can contain thousounds upon thousands
> of files. They should have used an enumerator-based interface instead.

Hi

Use WMI then :-)

WMI comes default with W2k/WXP, can be installed on W9x/WNT.

To get all doc-files on a drive:

Set oWMI = GetObject("winmgmts:")
sWQL = "select Name from cim_datafile where Drive='C:' AND Extension='doc'"

Set oResult = oWMI.execquery(sWQL)

WScript.Echo "# of files found: " & oResult.Count

For Each oFile In oResult
  WScript.Echo oFile.Name
Next

' using a semisynchronous call:
' Note: you cannot use oResult.Count here!!!

Set oResult = oWMI.execquery(sWQL,,48)
For Each oFile In oResult
  WScript.Echo oFile.Name
Next

--
torgeir



Mon, 28 Feb 2005 03:50:10 GMT  
 Iterating through all files on Hard drive
Torgeir and others

Thanks very much for the replies.

Very helpful

Regards
Steve

Quote:

> > > I recall a discussion in here early this year where Michael Harris ran
a
> > > search via FSO and via DIR. speeds were roughly similar, but what was

> > I'm not surprised. But FSO was supposed to be a "step up" :>. I just
can't
> > believe they designed it such that you have to access folder contents as
an
> > array. Sad thing, they went and did it again in .NET (hello, McFly). The
> > designers didn't realize that folders can contain thousounds upon
thousands
> > of files. They should have used an enumerator-based interface instead.

> Hi

> Use WMI then :-)

> WMI comes default with W2k/WXP, can be installed on W9x/WNT.

> To get all doc-files on a drive:

> Set oWMI = GetObject("winmgmts:")
> sWQL = "select Name from cim_datafile where Drive='C:' AND
Extension='doc'"

> Set oResult = oWMI.execquery(sWQL)

> WScript.Echo "# of files found: " & oResult.Count

> For Each oFile In oResult
>   WScript.Echo oFile.Name
> Next

> ' using a semisynchronous call:
> ' Note: you cannot use oResult.Count here!!!

> Set oResult = oWMI.execquery(sWQL,,48)
> For Each oFile In oResult
>   WScript.Echo oFile.Name
> Next

> --
> torgeir



Mon, 28 Feb 2005 06:38:48 GMT  
 Iterating through all files on Hard drive
50 lashes ;) Alex!

+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
Monte Hansen
http://KillerVB.com
+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+



Quote:
> Yep, I totally misremembered everything but the caching:


> --
> Please respond in the newsgroup so everyone may benefit.
>  http://dev.remotenetworktechnology.com
>  ----------
>  Subscribe to Microsoft's Security Bulletins:
>  http://www.microsoft.com/technet/security/bulletin/notify.asp




> > > Same thing happens with using the shell DIR command, of course.

> > > I recall a discussion in here early this year where Michael Harris
> ran a
> > > search via FSO and via DIR. speeds were roughly similar,

> > Hi

> > I think you remember wrong here. It was WMI cim_datafile and dir that
> was
> > roughly similar, FSO is left in the dust by dir or a optimized
> cim_datafile
> > query.

> > Here is the post from Michael:


> > Subject: Re: Find all files of type doc in C:\ and subdirectories
> > Newsgroups: microsoft.public.scripting.vbscript
> > Date: 2001-11-14 13:17:30 PST
> > http://groups.google.com/groups?selm=%23aBF7FVbBHA.1996%40tkmsftngp03

> > --
> > torgeir



Mon, 28 Feb 2005 09:29:48 GMT  
 Iterating through all files on Hard drive
Thanks, but I have my FileSystemLib does more and performs better :>

But I was driven to write it because of intrinsic VB I/O and the
FileSystemAbomination.

+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+
Monte Hansen
http://KillerVB.com
+|+|+|+|+|+|+|+|+|+|+|+|+|+|+|+


Quote:

> > > I recall a discussion in here early this year where Michael Harris ran
a
> > > search via FSO and via DIR. speeds were roughly similar, but what was

> > I'm not surprised. But FSO was supposed to be a "step up" :>. I just
can't
> > believe they designed it such that you have to access folder contents as
an
> > array. Sad thing, they went and did it again in .NET (hello, McFly). The
> > designers didn't realize that folders can contain thousounds upon
thousands
> > of files. They should have used an enumerator-based interface instead.

> Hi

> Use WMI then :-)

> WMI comes default with W2k/WXP, can be installed on W9x/WNT.

> To get all doc-files on a drive:

> Set oWMI = GetObject("winmgmts:")
> sWQL = "select Name from cim_datafile where Drive='C:' AND
Extension='doc'"

> Set oResult = oWMI.execquery(sWQL)

> WScript.Echo "# of files found: " & oResult.Count

> For Each oFile In oResult
>   WScript.Echo oFile.Name
> Next

> ' using a semisynchronous call:
> ' Note: you cannot use oResult.Count here!!!

> Set oResult = oWMI.execquery(sWQL,,48)
> For Each oFile In oResult
>   WScript.Echo oFile.Name
> Next

> --
> torgeir



Mon, 28 Feb 2005 09:29:10 GMT  
 
 [ 11 post ] 

 Relevant Pages 

1. Detect NEW file/files on the hard drive

2. How do attach file from local hard drive

3. Search hard driveS for file help

4. How to read the hard drives drive no.

5. Insert records from a database in floppy drive to hard drive

6. Hard Drive Serial Number/Total Drive Size

7. Deleting file on hard drive

8. Saving Email as .msg file on hard drive

9. Copy files from hard drive to Public Folders

10. Extended file/hard drive operations

11. Modifying all files on hard drive

12. Find file on hard drive

 

 
Powered by phpBB® Forum Software