Solved

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

Posted on 2009-07-15
16
723 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
  • 9
  • 7
16 Comments
 
LVL 25

Expert Comment

by:SStory
Comment Utility
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
Comment Utility
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
Comment Utility
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
 
LVL 25

Expert Comment

by:SStory
Comment Utility
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
Comment Utility
Receiving error on line 1, char 1 - Object required: 'fso'
0
 
LVL 25

Accepted Solution

by:
SStory earned 500 total points
Comment Utility
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
Comment Utility
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
Comment Utility
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
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Author Comment

by:murryc
Comment Utility
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
Comment Utility
I'm sorry, replace that line with this:

File.Delete
0
 

Author Comment

by:murryc
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
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
Comment Utility
You nailed it!
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

Unlike scripting languages such as C# where a semi-colon is used to indicate the end of a command, Microsoft's VBScript language relies on line breaks to determine when a command begins and ends. As you can imagine, this quickly results in messy cod…
Welcome back!  My apologies for taking so long to write part two of this series; it's been a long time coming!  As I promised in Part 1, this article will focus on how to locate those elusive AD properties that you are searching for.  Why is this us…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

728 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now