按键精灵调用指定文件夹内随机文本
下文代码是我上有偿定制出的,使用按键精灵调用指定文件夹内随机文本名、文本内容、文本内容随机行,转载请注明出处!
1.首先写入调用随机文件名所需要的函数:
Function FindAllFiles(FolderPath) Dim AllFiles,fso,Folder,f,files AllFiles = "" Set fso = CreateObject("Scripting.FileSystemObject") Set Folder = fso.getfolder(FolderPath) Set files = Folder.files For Each f In files AllFiles = AllFiles & f.name & "," Next Set fso = Nothing FindAllFiles = Split(AllFiles, ",") End Function
2.取随机文件名:
FileEx = FindAllFiles(文件夹路径) If UBound(FileEx) > 0 Then Randomize 随机文件名 = FileEx(int(rnd * UBound(FileEx))) Else MessageBox "文件夹没有文件" EndScript End If
例1:写出指定文件夹随机文档名:
Function FindAllFiles(FolderPath) Dim AllFiles,fso,Folder,f,files AllFiles = "" Set fso = CreateObject("Scripting.FileSystemObject") Set Folder = fso.getfolder(FolderPath) Set files = Folder.files For Each f In files AllFiles = AllFiles & f.name & "," Next Set fso = Nothing FindAllFiles = Split(AllFiles, ",") End Function 文件夹路径 ="C:\Users\Administrator\Desktop\读取" FileEx = FindAllFiles(文件夹路径) If UBound(FileEx) > 0 Then Randomize 随机文件名 = FileEx(int(rnd * UBound(FileEx))) Else MessageBox "文件夹没有文件" EndScript End If TXT路径 = 文件夹路径 + "\" + 随机文件名 SayString (Cstr(随机文件名))
例2:写出指定文件夹随机文档内容:
Function FindAllFiles(FolderPath) Dim AllFiles,fso,Folder,f,files AllFiles = "" Set fso = CreateObject("Scripting.FileSystemObject") Set Folder = fso.getfolder(FolderPath) Set files = Folder.files For Each f In files AllFiles = AllFiles & f.name & "," Next Set fso = Nothing FindAllFiles = Split(AllFiles, ",") End Function 文件夹路径 ="C:\Users\Administrator\Desktop\读取" FileEx = FindAllFiles(文件夹路径) If UBound(FileEx) > 0 Then Randomize 随机文件名 = FileEx(int(rnd * UBound(FileEx))) Else MessageBox "文件夹没有文件" EndScript End If TXT路径 = 文件夹路径 + "\" + 随机文件名 内容 = Plugin.File.ReadFileEx(TXT路径) SayString (Cstr(内容))
例3:写出指定文件夹随机文档内容的随机行:
Function FindAllFiles(FolderPath) Dim AllFiles,fso,Folder,f,files AllFiles = "" Set fso = CreateObject("Scripting.FileSystemObject") Set Folder = fso.getfolder(FolderPath) Set files = Folder.files For Each f In files AllFiles = AllFiles & f.name & "," Next Set fso = Nothing FindAllFiles = Split(AllFiles, ",") End Function 文件夹路径 ="C:\Users\Administrator\Desktop\读取" FileEx = FindAllFiles(文件夹路径) If UBound(FileEx) > 0 Then Randomize 随机文件名 = FileEx(int(rnd * UBound(FileEx))) Else MessageBox "文件夹没有文件" EndScript End If TXT路径 = 文件夹路径 + "\" + 随机文件名 内容 = Plugin.File.ReadFileEx(TXT路径) //调用随机文档内的随机行 Randomize Text = Plugin.File.ReadFileEx(TXT路径) MyArray = Split(Text, "|") SayString MyArray(Int((UBound(MyArray) * Rnd) + 0))
例4:写出制定文件夹内的随机文章内容并删除,如文件夹内容为空则写出提示的话:
Function FindAllFiles(FolderPath) Dim AllFiles,fso,Folder,f,files AllFiles = "" Set fso = CreateObject("Scripting.FileSystemObject") Set Folder = fso.getfolder(FolderPath) Set files = Folder.files For Each f In files AllFiles = AllFiles & f.name & "," Next Set fso = Nothing FindAllFiles = Split(AllFiles, ",") End Function 文件夹路径 ="C:\Users\Administrator\Desktop\读取1" FileEx = FindAllFiles(文件夹路径) If UBound(FileEx) > 0 Then Randomize 随机文件名 = FileEx(int(rnd * UBound(FileEx))) TXT路径 = 文件夹路径 + "\" + 随机文件名 内容 = Plugin.File.ReadFileEx(TXT路径) SayString (Cstr(内容)) Plugin.File.DeleteFile 参数1 Call Plugin.File.DeleteFile(TXT路径) ElseIf UBound(FileEx) < 0 Then SayString (Cstr("文件夹 < "+ 文件夹路径 +" > 内无文章!")) End If