?
Solved

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

Posted on 2009-07-15
16
Medium Priority
?
767 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 2000 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 2000 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

Welcome, welcome!  If you are new to the series and haven't been following along, please take a brief moment to review the first three installments: Part 1 (http://www.experts-exchange.com/Programming/Languages/Visual_Basic/VB_Script/A_266-VBScri…
This script will sweep a range of IP addresses (class c only, 255.255.255.0) and report to a log the version of office installed. What it does: 1.)      Creates log file in the directory the script is run from (if it doesn't already exist) 2.)      Sweep…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
Suggested Courses

777 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