0

The code that I currently have (given below), I prompt the user for the folder path then return the first 10 files from one folder. I then put the file names into a text file.

I want to be able to list only 10 random "wav" files from each of the subfolders, then return the names of the files and its corresponding folder name to the text file.

CODE

Const WINDOW_HANDLE = 0
Const BIF_EDITBOX = &H10
Const BIF_NONEWFOLDER = &H0200
Const BIF_RETURNONLYFSDIRS = &H1

Set objShell = CreateObject("Shell.Application")
Set wshShell = CreateObject("WScript.Shell")
Set objFSO = CreateObject("Scripting.FileSystemObject")
Dim fso, folder, files, OutputFile
Dim strPath

' Define folder we want to list files from
strPrompt = "Please select the folder to process."
intOptions = BIF_RETURNONLYFSDIRS + BIF_NONEWFOLDER + BIF_EDITBOX
strTargetPath = "G:\FAU\PythonCode\Urban-Sound-Classification-master\data\"
strFolderPath = Browse4Folder(strPrompt, intOptions, strTargetPath)


Set objFolder = objFSO.GetFolder(strFolderPath)

Set files = objFolder.Files

' Create text file to output test data
Set OutputFile = objFSO.CreateTextFile(strTargetPath & "\Trainfilelist.txt", True)

' Loop through each file
intCount = 0  
For each item In files
    If UCase(objFSO.GetExtensionName(item.name)) = "WAV" Then

  'Output file properties to a text file
  OutputFile.WriteLine(item.Name & vbTab & folder)
    End If
    intCount = intCount + 1
    If intCount = 11 Then Exit For
Next

' Close text file
OutputFile.Close

'**Browse4Folder Function
Function Browse4Folder(strPrompt, intOptions, strRoot)
    Dim objFolder, objFolderItem

    On Error Resume Next

    Set objFolder = objShell.BrowseForFolder(0, strPrompt, intOptions, strRoot)
    If (objFolder Is Nothing) Then
      Wscript.Quit
    End If
    Set objFolderItem = objFolder.Self
    Browse4Folder = objFolderItem.Path
    Set objFolderItem = Nothing
    Set objFolder = Nothing
End Function
Joe
  • 357
  • 2
  • 10
  • 32
  • 1
    Make nested loops. Outer loop for subfolders, 1st inner loop for retrieveing files from each subfolder into dictionary, 2nd inner loop (running after the 1st) to choose random file from dictionary and write to otput file. – omegastripes Aug 24 '20 at 06:21

1 Answers1

1

Just a little try from me, but in Powershell :

$LogFile="$Env:AppData\Wav_List.txt"
$Source = "$Env:LocalAppData\Microsoft\Windows Sidebar\Gadgets\NetworkMonitorII.gadget\media"
$Array_WAVS = (Get-ChildItem -Path $Source).FullName | Select-Object -First 10 | Out-File -FilePath $LogFile
$Array_WAVS = GC $LogFile
$dir = (${env:ProgramFiles(x86)}, ${env:ProgramFiles} -ne $null)[0]
$VLC_Program = $dir+"\VideoLAN\VLC\vlc.exe"
$randomNo = Get-Random -Maximum 10 -Minimum 0
$programArgs = $Array_WAVS[$randomNo]
$programArgs
Start-Process $LogFile
Invoke-Command -ScriptBlock { & $VLC_Program $programArgs }
Hackoo
  • 18,337
  • 3
  • 40
  • 70
  • The question is tagged [tag:vbscript], what about that suggests [tag:powershell]? What's stopping me posting a [tag:c#] answer which has [been covered many times](https://stackoverflow.com/a/8677658/692942) before, I'll tell you why, because I have no business posting a [tag:c#] solution on the [tag:vbscript] question. Sorry, but this doesn't belong here and adding the [tag:powershell] tag to the question will just make it "Too Broad" and be closed anyway. – user692942 Aug 24 '20 at 13:01
  • Relevant (but not relevant to this question) - [Fliter results of gci with strings from a textfile in Powershell](https://stackoverflow.com/a/9044603) – user692942 Aug 24 '20 at 13:07