IT CAMPER
asked on
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.
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.
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)
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
ASKER
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?
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
ASKER
Receiving error on line 1, char 1 - Object required: 'fso'
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.Pat h)
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.
Basically you just change the action line to perform whatever command.
In the above that would be the line that says:
OutFile.WriteLine(File.Pat
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
ASKER
Just tried the delete script. I get a 'Cannot use parentheses when calling a Sub' error on Line 9, char 49.
I'm sorry, replace that line with this:
File.Delete
File.Delete
ASKER
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
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.
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
ASKER
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'
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'
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
try this:
DeleteFiles Subfolder, FileExtension
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
You nailed it!
A good reference for these objects is:
http://www.devguru.com/technologies/vbscript/quickref/file.html
Open in new window