[Resolved] VBScript to bring back length of sound files.
Hi All,
I have created a small script to bring back the length of a sound file; currently it brings back the name and length of the sound file in the specified directory. Is it possible to make this look under that particular directory for all sound files irrespective of how many folders there are? I know I should be using a recursive approach but am not really getting it to work. Could anyone advise on how best to create this?
Code:
Dim list(35)
Set objShell = CreateObject("Shell.Application")
Set objFolder = objShell.Namespace("C:\test\")
For i = 27 to 27
list(i) = objFolder.GetDetailsOf(objFolder.Items, i)
Next
For Each strFileName in objFolder.Items
For i = 27 to 27
Wscript.Echo strFileName & vbtab & list(i) _
& ": " & objFolder.GetDetailsOf(strFileName, i)
Next
Next
Thanks in advance.
Re: VBScript to bring back length of sound files.
For a recursive function to find all files in all subfolders of a start folder see http://blogs.technet.com/b/heyscript...ubfolders.aspx.
Re: VBScript to bring back length of sound files.
Quote:
Originally Posted by
His Nibbs
Thanks for that I can see how this brings back a list of all documents and this runs fine. I am having issues returning the specific property of length of the audio file so it manages it for all sound files.
Quote:
Dim arrTemp(35)
Dim arrTemp1(35)
Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "Z:\Test"
Set objFolder = objFSO.GetFolder(objStartFolder)
Wscript.Echo objFolder.Path
Set ObjShell = CreateObject("Shell.Application")
Set objTempFolder = objShell.NameSpace(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
Wscript.Echo
For i = 27 to 27
arrTemp(i) = objTempFolder.GetDetailsOf(objTempFolder.Items, i)
Next
ShowSubfolders objFSO.GetFolder(objStartFolder)
Sub ShowSubFolders(Folder)
Set ObjShell = CreateObject("Shell.Application")
Set objTempFolder2 = objShell.NameSpace(objStartFolder)
Set objTemp = Folder
For i = 27 to 27
arrTemp1(i) = objTempFolder2.GetDetailsOf(objTempFolder2.Items, i)
Next
For Each Subfolder in Folder.SubFolders
Wscript.Echo Subfolder.Path
Set objFolder = objFSO.GetFolder(Subfolder.Path)
Set colFiles = objFolder.Files
For Each objFile in colFiles
Wscript.Echo objFile.Name
Next
For Each strFileName in objTempFolder2.Items
For i = 27 to 27
Wscript.Echo strFileName & vbtab & arrTemp1(i) _
& ": " & objTempFolder2.GetDetailsOf(strFileName, i)
Next
Next
Wscript.Echo
ShowSubFolders Subfolder
Next
End Sub
I can't seem to change the folder so that the new subfolder is selected so that it will pull out all the sound files. I think I may not be using my loop correctly. I have tried to change it but no to avail. Someone please help!!! :o
Re: VBScript to bring back length of sound files.
Here is how I do a recursive search for files in the current and sub folders.
Code:
Option Explicit
Dim fso, fold
Dim strFolder
'Change as needed
strFolder = "Z:\Test"
'Create the filesystem object
Set fso = CreateObject("Scripting.FileSystemObject")
'Get a reference to the folder you want to search
Set fold = FSO.GetFolder(strFolder)
RecursiveSearch fold
'Clean up
Set fold = Nothing
Set fso = Nothing
Private Sub RecursiveSearch(flder)
Dim fil, fld
For Each fld In flder.SubFolders
RecursiveSearch fld
Next
'loop through the folder and get the file names
For Each fil In flder.Files
'fil will now be the file being worked on.
'some important properties for you may be Name and Path
Wscript.Echo fil.Name
Wscript.Echo fil.Path
'#####################################################
' Call your code here to get the details of the file
'#####################################################
Next
End Sub
Re: VBScript to bring back length of sound files.
Hi MarkT,
Thank you for your help was perfect; just what I needed. I managed to put together something that worked recursively outputting the intended file properties. I shall put what I ended up using here:
Code:
Dim objShell, objFolder, objFolderItem
Dim FSO, oFolder, Fil
Dim MainFolderName
Set objShell = CreateObject("Shell.Application")
MainFolderName = "Z:\Test"
Set FSO = CreateObject("scripting.FileSystemObject")
Set oFolder = FSO.GetFolder(MainFolderName)
Wscript.Echo oFolder.path
For Each Fil In oFolder.Files
Set objFolder = objShell.Namespace(oFolder.path)
Set objFolderItem = objFolder.ParseName(Fil.Name)
Wscript.Echo Fil.Name & vbtab & "Length:" & vbtab & objFolder.GetDetailsOf(objFolderItem, 27)
Next
'Get subdirectories
Call RecursiveFolder(oFolder)
Sub RecursiveFolder(xFolder)
Dim SubFld
For Each SubFld In xFolder.SubFolders
Set oFolder = FSO.GetFolder(SubFld)
Set objFolder = objShell.Namespace(SubFld.path)
Wscript.Echo SubFld.path
For Each Fil In SubFld.Files
Set objFolder = objShell.Namespace(oFolder.path)
'Problem with objFolder at times
If Not objFolder Is Nothing Then
Set objFolderItem = objFolder.ParseName(Fil.Name)
Wscript.Echo Fil.Name & vbtab & "Length:" & vbtab & objFolder.GetDetailsOf(objFolderItem, 27)
Else
Debug.Print Fil.path & " " & Fil.Name
End If
Next
Call RecursiveFolder(SubFld)
Next
End Sub
In the search to ever improve the code I may tamper with it slightly when I have a moment to only bring back the properties based on certain file types found below the folder being searched.
MASSIVE help big thank you!!!!