Solved

Need VB script to scan folders and sub folders for file extensions

Posted on 2009-07-15
16
759 Views
Last Modified: 2012-06-27
Need a VB script to scan a folder and all of its sub-folders (d:\audio) and return a text file list of all folders and files that contains .wav files.  So the text file would show something like...

d:\Audio\A Client\ABC Auto Parts.wav
d:\Audio\G Client\023409\Great Lakes.wav

I also need to script to search the same folders for all files with extension .pk and delete these files.
0
Comment
Question by:murryc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 7
16 Comments
 
LVL 25

Expert Comment

by:SStory
ID: 24861311
You may have to tweak a bit.. I just put the code here.
A good reference for these objects is:
http://www.devguru.com/technologies/vbscript/quickref/file.html
Set FSO = CreateObject("Scripting.FileSystemObject")
ShowWAVs FSO.GetFolder("C:\Scripts")
 
Sub ShowWAVs(Folder)
    For Each Subfolder in Folder.SubFolders
        For Each File in Folder.Files
              if right(File,4)=".wav" then
                     Wscript.Echo File.Path
              end if
        Next
        ShowSubFolders Subfolder
    Next
End Sub
	

Open in new window

0
 
LVL 25

Expert Comment

by:SStory
ID: 24861350
as for deleting you could do the same sort of thing.
change .wav to .pk (probably make a different function).

Or you could make a simple .bat file and use the erase command
rem (put the following in a batch file)
@echo off
cd whatever folder
erase *.pk /S /F /Q
rem (end of batch file)


oops...(the code in the previous post should be as follows--see below)
Set FSO = CreateObject("Scripting.FileSystemObject")
ShowWAVs FSO.GetFolder("C:\Scripts")
 
Sub ShowWAVs(Folder)
    For Each Subfolder in Folder.SubFolders
        For Each File in Folder.Files
              if right(File,4)=".wav" then
                     Wscript.Echo File.Path
              end if
        Next
        ShowWavs Subfolder
    Next
End Sub

Open in new window

0
 

Author Comment

by:murryc
ID: 24862636
Thanks for the help!  I am not good at scripting.  Your code scans and echos each .wav found within the provided location.  Can you edit your code to write the results to a text file?
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 25

Expert Comment

by:SStory
ID: 24864430
be sure to change the c:\scripts to whatever you need
Set OutFile = fso.CreateTextFile("e:\ezine\newsletter13.txt", True)
Set FSO = CreateObject("Scripting.FileSystemObject")
 
ShowWAVs FSO.GetFolder("C:\Scripts")
OutFile.Close
 
Sub ShowWAVs(Folder)
    For Each Subfolder in Folder.SubFolders
        For Each File in Folder.Files
              if right(File,4)=".wav" then
                     OutFile.WriteLine(File.Path)
              end if
        Next
        ShowWavs Subfolder
    Next
End Sub

Open in new window

0
 

Author Comment

by:murryc
ID: 24864453
Receiving error on line 1, char 1 - Object required: 'fso'
0
 
LVL 25

Accepted Solution

by:
SStory earned 500 total points
ID: 24868783
Sorry about that. Try the one below.
Set FSO = CreateObject("Scripting.FileSystemObject")
Set OutFile = fso.CreateTextFile("e:\ezine\newsletter13.txt", True)
 
ShowWAVs FSO.GetFolder("C:\Scripts")
OutFile.Close
 
Sub ShowWAVs(Folder)
    For Each Subfolder in Folder.SubFolders
        For Each File in Folder.Files
              if right(File,4)=".wav" then
                     OutFile.WriteLine(File.Path)
              end if
        Next
        ShowWavs Subfolder
    Next
End Sub

Open in new window

0
 

Author Comment

by:murryc
ID: 24872100
Works perfect on the .wav finding and reporting.  Can you edit the script for scanning and deleting .pk files as well?  Use the same script, where I just specifiy a folder, then have it scan and delete.
0
 
LVL 25

Expert Comment

by:SStory
ID: 24878423
sure...

Basically you just change the action line to perform whatever command.
In the above that would be the line that says:
OutFile.WriteLine(File.Path)

WARNING: The following is UNTESTED.
It should work, but it is deleting files, so you might want a system backup before testing.  It will delete all files from the folder specified and its subfolders having the fileextension given. So if you passed "*.*" you would wipe out everything in the folder and subfolders, so be careful.
Set FSO = CreateObject("Scripting.FileSystemObject")
 
DeleteFiles FSO.GetFolder("C:\Temp"),".pk"
 
Sub DeleteFiles(Folder,FileExtension)
    For Each Subfolder in Folder.SubFolders
        For Each File in Folder.Files
              if right(File,len(FileExtension))=FileExtension then
                     File.Delete(File.Path,True)
              end if
        Next
        DeleteFiles Subfolder
    Next
End Sub

Open in new window

0
 

Author Comment

by:murryc
ID: 24896201
Just tried the delete script.  I get a 'Cannot use parentheses when calling a Sub' error on Line 9, char 49.
0
 
LVL 25

Expert Comment

by:SStory
ID: 24896487
I'm sorry, replace that line with this:

File.Delete
0
 

Author Comment

by:murryc
ID: 24896557
Below is what I am using, but it is not deleting any files within C:\Temp.  I also tried using just .pk.
Set FSO = CreateObject("Scripting.FileSystemObject")
 
DeleteFiles FSO.GetFolder("C:\Temp"),"*.*"
 
Sub DeleteFiles(Folder,FileExtension)
    For Each Subfolder in Folder.SubFolders
        For Each File in Folder.Files
              if right(File,len(FileExtension))=FileExtension then
                     File.Delete
              end if
        Next
        DeleteFiles Subfolder
    Next
End Sub

Open in new window

0
 
LVL 25

Expert Comment

by:SStory
ID: 24903884
OK, Sorry about that. I guess I should have tested it. I just thought I could get it right from memory.
This has been tested and works as follows. You should be able to pass *.* as the file extension to delete everything (not recommended). It deletes everything in the path given and all subfolders in that folder.

Or if you want a file extension, pass it without the period, like

TXT for *.txt

The IF line is wrapped below in my view..make sure everything between the word IF and the word then is on one line.
Set FSO = CreateObject("Scripting.FileSystemObject")
 
DeleteFiles FSO.GetFolder("C:\Temp"),"TXT"
 
 
Sub DeleteFiles(Folder,FileExtension)
    For Each File in Folder.Files
        IF FileExtension="*.*" OR UCASE(FSO.GetExtensionName(File.Path))=UCASE(FileExtension) then
            File.Delete
        end if
    Next
 
    For Each Subfolder in Folder.SubFolders
        DeleteFiles Subfolder
    Next
End Sub

Open in new window

0
 

Author Comment

by:murryc
ID: 24905383
Very close...

The script deletes files in the root of the Temp folder, but throws an error when you have subfolders.
Line 14, Char 9
Wrong number of arguments or invalid property assignment: 'DeleteFiles'
0
 
LVL 25

Expert Comment

by:SStory
ID: 24906213
OOPs, well my test didn't have subfolders.... I think that I forgot to add the extension to that call:

try this:

 DeleteFiles Subfolder, FileExtension
0
 
LVL 25

Assisted Solution

by:SStory
SStory earned 500 total points
ID: 24906229
Below is the corrected code:
Set FSO = CreateObject("Scripting.FileSystemObject")
 
DeleteFiles FSO.GetFolder("C:\Temp"),"TXT"
 
 
Sub DeleteFiles(Folder,FileExtension)
    For Each File in Folder.Files
        IF FileExtension="*.*" OR UCASE(FSO.GetExtensionName(File.Path))=UCASE(FileExtension) then
            File.Delete
        end if
    Next
 
    For Each Subfolder in Folder.SubFolders
         DeleteFiles Subfolder, FileExtension
    Next
End Sub

Open in new window

0
 

Author Comment

by:murryc
ID: 24906326
You nailed it!
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction During my participation as a VBScript contributor at Experts Exchange, one of the most common questions I come across is this: "I have a script that runs against only one computer. How can I make it run against a list of computers in …
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question