filevbscript

Remove file extension from returned list of files


I have the below code that obtains a list of txt files from a folder, I would like to remove the .txt from the returned file name, how may I achieve this?

Set objFSO = CreateObject("Scripting.FileSystemObject")
objStartFolder = "Notes"

Set objFolder = objFSO.GetFolder(objStartFolder)

Set colFiles = objFolder.Files
   For Each objFile in colFiles
   If UCase(objFSO.GetExtensionName(objFile.name)) = "TXT" Then
       document.write objFile.name & "<br>"
   End If
Next

Solution

  • This will work:

    ...
    For Each objFile in colFiles
       If UCase(objFSO.GetExtensionName(objFile.name)) = "TXT" Then
           document.write Left(objFile.name, Len(objFile.name)-4) & "<br>"
       End If
    Next
    

    According to the docs, Left()

    Returns a specified number of characters from the left side of a string.

    You just need to know how many characters to return. Since you already know that the filename ends in .txt, you can return all but the last 4 characters. Len(objFile.name) will give you the full length, and you can subtract from there.