使用 VBScript 列出子文件夹中的前几个文件和 return 列出文件名和文件夹名
List first few files in subfolders and return list with file names and folder names using VBScript
我目前拥有的代码(如下所示),我提示用户输入文件夹路径,然后 return 一个文件夹中的前 10 个文件。然后我将文件名放入文本文件中。
我希望能够从每个子文件夹中仅列出 10 个随机“wav”文件,然后 return 将文件名称及其对应的文件夹名称添加到文本文件中。
代码
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
只是我的一点尝试,但在 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 }
我目前拥有的代码(如下所示),我提示用户输入文件夹路径,然后 return 一个文件夹中的前 10 个文件。然后我将文件名放入文本文件中。
我希望能够从每个子文件夹中仅列出 10 个随机“wav”文件,然后 return 将文件名称及其对应的文件夹名称添加到文本文件中。
代码
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
只是我的一点尝试,但在 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 }