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

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.
murrycAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SStoryCommented:
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
SStoryCommented:
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
murrycAuthor Commented:
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
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

SStoryCommented:
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
murrycAuthor Commented:
Receiving error on line 1, char 1 - Object required: 'fso'
0
SStoryCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
murrycAuthor Commented:
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
SStoryCommented:
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
murrycAuthor Commented:
Just tried the delete script.  I get a 'Cannot use parentheses when calling a Sub' error on Line 9, char 49.
0
SStoryCommented:
I'm sorry, replace that line with this:

File.Delete
0
murrycAuthor Commented:
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
SStoryCommented:
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
murrycAuthor Commented:
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
SStoryCommented:
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
SStoryCommented:
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
murrycAuthor Commented:
You nailed it!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.