问题
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
回答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 }
来源:https://stackoverflow.com/questions/63555232/list-first-few-files-in-subfolders-and-return-list-with-file-names-and-folder-na