Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

This question already has an answer here:

I have tried CD C:\ DIR /s /b ?????.txt But I will return all files whose names are within 5 characters so how to only display exactly 5 character??

Thank you so much~

share|improve this question

marked as duplicate by Brad Patton, Tog, Ƭᴇcʜιᴇ007, Dave Rook, Dave M Apr 23 '13 at 12:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
I'd just write a Java program to do it. –  Daniel R Hicks Apr 23 '13 at 0:42
    
For wide format it's necessary to know the current wide of the command line window. This information is stored in the registry. Repeat your question at SO. –  Endoro Apr 23 '13 at 5:07
    
From other answer : dir /B | findstr /R "^.......\.txt" –  tgkprog Apr 24 at 7:28
    
Got it thank you. –  Lester Apr 24 at 8:57

1 Answer 1

up vote 0 down vote accepted

VBA script to list all text files ....

'Wscript.Echo "begin."
Set objFSO = CreateObject("Scripting.FileSystemObject")
DIM folderStart,fileOutName, fileH
On Error Resume Next
folderStart = InputBox("Drive or folder to start looking in", , "D:\prog\github\academic")
fileOutName = InputBox("File to write results", "Output file", "c:\tmp\filesFounds.note")
Set objSuperFolder = objFSO.GetFolder(folderStart)

Set fileH = objFSO.OpenTextFile(fileOutName,2 ,1)
fileH.WriteLine ("Start " & Now)  
Call ShowSubfolders (objSuperFolder, fileH)
Call fileH.WriteLine ("end " & Now)  
fileH.Close
Wscript.Echo "Done " & Now
WScript.Quit 0

Sub ShowSubFolders(fFolder, fileH)
    Err.clear
    On Error Resume Next
    Set objFolder = objFSO.GetFolder(fFolder.Path)
    If Err.Number = 0 then
        Set colFiles = objFolder.Files
        If Err.Number = 0 then
            For Each objFile in colFiles
                If Err.Number = 0 then
                If UCase(objFSO.GetExtensionName(objFile.name)) = "TXT"  Then
                    If len(objFile.name) = 9  Then    
                        'Wscript.Echo objFile.Name
                        fileH.WriteLine (objFile.path)  
                        'fileH.WriteLine (objFile.name)  'only short name
                    End If
                End If
            End If
            Next
        End If

        For Each Subfolder in fFolder.SubFolders
        Call ShowSubFolders(Subfolder, fileH)
        Next
    End If
End Sub

Original

'Wscript.Echo "begin." Set objFSO = CreateObject("Scripting.FileSystemObject") DIM folderStart,fileOutName, fileH folderStart = InputBox("Drive or folder to start looking in", , "c:\") fileOutName = InputBox("File to write results", "Output file", "c:\tmp\filesFounds.note") Set objSuperFolder = objFSO.GetFolder(folderStart)

Set fileH = objFSO.OpenTextFile(fileOutName,2 ,1)
fileH.WriteLine ("Start " & Now)  
Call ShowSubfolders (objSuperFolder, fileH)
Call fileH.WriteLine ("end " & Now)  
fileH.Close
Wscript.Echo "Done " & Now
WScript.Quit 0

Sub ShowSubFolders(fFolder, fileH)
On Error Resume Next
    Set objFolder = objFSO.GetFolder(fFolder.Path)
    Set colFiles = objFolder.Files
    For Each objFile in colFiles
        If UCase(objFSO.GetExtensionName(objFile.name)) = "TXT"  Then
            If len(objFile.name) = 9  Then    
                    'Wscript.Echo objFile.Name
                    fileH.WriteLine (objFile.path)  
                    'fileH.WriteLine (objFile.name)  'only short name
                End If
        End If
    Next

    For Each Subfolder in fFolder.SubFolders
        Call ShowSubFolders(Subfolder, fileH)
    Next

End Sub

Or get it from http://sel2in.com/pages/prog/vba/

To use the file name should be a.vbs or names5.vbs (or something else ending with vbs) then just open it - double click or right click and choose open

With error handling but not tried on c:\ (running it now)

share|improve this answer
    
ran it on my c drive. took 20 minutes, found a bunch of txt files with 5 chars in name. –  tgkprog Apr 23 '13 at 2:17

Not the answer you're looking for? Browse other questions tagged or ask your own question.