简体   繁体   English

vbs 文件搜索目录中的扩展名

[英]vbs file search for extention in a directory

I'm trying to write this code in VBs, (I'm quite new) I want to have the code go through a folder/directory, and pick out all ".txt" files, in something like a for loop.我正在尝试在 VB 中编写此代码,(我很新)我想让代码通过一个文件夹/目录,并以 for 循环之类的方式挑选出所有“.txt”文件。 These text files could then be listed using MsgBox ("txt filename").然后可以使用 MsgBox(“txt 文件名”)列出这些文本文件。 This is what I've got so far:这是我到目前为止所得到的:

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Users\Desktop\folder"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    If UCase(objFSO.GetExtensionName(objFile.name)) = ".txt" Then
    Wscript.Echo objFile.Name

Next

It doesn't seem to be picking out a txt file called "name.txt".它似乎没有挑出一个名为“name.txt”的txt文件。 Any help would be greatly appreciated.任何帮助将不胜感激。

ps: please ignore and bad spelling and my terrible formatting (im new to stackoverflow) Thanks! ps:请忽略拼写错误和我糟糕的格式(我是 stackoverflow 的新手)谢谢! (also the code above is mostly mashed together code i found off the internet) (而且上面的代码主要是我从互联网上找到的代码混搭在一起)

You should modify this line ;你应该修改这一行; if you use UCase :如果您使用UCase

If UCase(objFSO.GetExtensionName(objFile.name)) = ".txt"

To

If UCase(objFSO.GetExtensionName(objFile)) = "TXT"

Or if you use LCase或者,如果您使用LCase

If LCase(objFSO.GetExtensionName(objFile)) = "txt"

And your code looks like this one :你的代码看起来像这样:

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "C:\Users\Desktop\folder"
Set objFolder = objFSO.GetFolder(objStartFolder)
Set colFiles = objFolder.Files
For Each objFile in colFiles
    If LCase(objFSO.GetExtensionName(objFile)) = "txt" Then
        Wscript.Echo objFile.Name
    End If
Next

GetExtensionName Method 获取扩展名方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM