在子文件夹中列出前几个文件,并使用VBScript返回带有文件名和文件夹名的列表 [英] List first few files in subfolders and return list with file names and folder names using VBScript

查看:68
本文介绍了在子文件夹中列出前几个文件,并使用VBScript返回带有文件名和文件夹名的列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我当前拥有的代码(如下所示),我提示用户输入文件夹路径,然后从一个文件夹返回前10个文件.然后,我将文件名放入文本文件中.

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.

我希望仅列出10个随机的"wav"每个子文件夹中的文件,然后将文件名及其对应的文件夹名称返回到文本文件.

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.

代码

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中:

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 }

这篇关于在子文件夹中列出前几个文件,并使用VBScript返回带有文件名和文件夹名的列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆