vba 如何删除找到的文件 FSO 的扩展名?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16763675/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 21:17:16  来源:igfitidea点击:

how to remove the extension of a found file FSO?

vba

提问by user2151190

The code I wrote can display filenames into a sheet, but I want to remove the extension when displayed. I know that should be a little correction, but I burned out trying options. Can Somebody tell me where exaclty I must add a piece of code that I miss please? My attempt of code below. Many similar issues on the net, but I can not manage to find it.Thanks in advance....

我编写的代码可以将文件名显示到工作表中,但我想在显示时删除扩展名。我知道这应该是一点修正,但我尝试了很多选择。有人可以告诉我我必须在哪里添加一段我想念的代码吗?我在下面的代码尝试。网上有很多类似的问题,但我找不到。提前致谢....

Option Explicit
Sub fileNames_in_folder()
Application.ScreenUpdating = True
Application.DisplayAlerts = True
Dim fldpath
Dim fld As Object, fil As Object, fso As Object, j As Long
 fldpath = "C:\"
   On Error Resume Next
Thisworkbook.Sheets("1").Activate
'start count row
    j = 11
Set fso = CreateObject("scripting.filesystemobject")
Set fld = fso.getfolder(fldpath)
    For Each fil In fld.Files
'here I have to add something due to expell the ".extension" 
Cells(j, 34).Value = fso.GetBaseName(fil.path)
'count behaviour
    j = j + 1
  Next
Columns("AH").AutoFit
End Sub

回答by IvanH

A file name without extension you can get with GetBaseName Method:

您可以使用GetBaseName 方法获得没有扩展名的文件名:

Cells(j, 34).Value = fso.GetBaseName(fil.path)

回答by shahkalpesh

If InStrRev(fil.Path, ".") <> 0 Then
   Cells(j, 34).Value = Left(fil.Path, InStrRev(fil.Path, ".") - 1)
End If

Assuming the presence "." in the file name.
i.e. C:\Test.txt will be shown as C:\Test

假设存在“.” 在文件名中。
即 C:\Test.txt 将显示为 C:\Test