Results 1 to 5 of 5

Thread: [Resolved] VBScript to bring back length of sound files.

  1. #1
    New Member
    Join Date
    Jul 12
    Posts
    6

    Resolved [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.
    Last edited by jimmysheedah; Jul 24th, 2012 at 05:15 AM. Reason: Resolved

  2. #2
    Addicted Member
    Join Date
    Jul 09
    Posts
    208

    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.

  3. #3
    New Member
    Join Date
    Jul 12
    Posts
    6

    Re: VBScript to bring back length of sound files.

    Quote Originally Posted by His Nibbs View Post
    For a recursive function to find all files in all subfolders of a start folder see http://blogs.technet.com/b/heyscript...ubfolders.aspx.
    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.

    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!!!

  4. #4
    PowerPoster
    Join Date
    Jun 01
    Location
    Trafalgar, IN
    Posts
    3,439

    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

  5. #5
    New Member
    Join Date
    Jul 12
    Posts
    6

    Resolved 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!!!!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •