batch-filefindstr

Batch file recursion for a letter range, avoiding weird shortname selections


My code

for %f in (c:\mPodcasts\ipod\s*.lnk c:\mPodcasts\ipod\ta*.lnk c:\mPodcasts\ipod\te*.lnk c:\mPodcasts\ipod\tha*.lnk) do echo %f

is unexpectedly picking up a number of shortname files (e.g., the john batchelor show - xyz which is THA1ED~1.lnk).

this is discussed here: Strange Windows DIR command behavior

I would prefer to NOT disable shortnames overall. I tried building a .txt file beforehand for the directory listings, but dir * | findstr /r ^tha seems to ignore the caret, and finds any filename containing tha.

I'm open to other solutions that will only give me results based on the long file names.

Your thoughts?

Thx


Solution

  • Without being able to test, I believe that using the where command, (Windows Vista onwards), should ignore those 8.3 filenames:

    @For /F "EOL=| Delims=" %%G In ('%__AppDir__%where.exe^
         C:\mPodcasts\ipod:s*.lnk^
         C:\mPodcasts\ipod:ta*.lnk^
         C:\mPodcasts\ipod:te*.lnk^
         C:\mPodcasts\ipod:tha*.lnk^
         2^>NUL') Do @Echo %%G
    @Pause
    

    For the purposes of your provided information, the above should work without modification. However, should your directories or file pattern contain spaces, problematic characters, you can also doublequote them as necessary:

    @For /F "EOL=| Delims=" %%G In ('%__AppDir__%where.exe^
         "C:\mPodcasts\ipod":"s*.lnk"^
         "C:\mPodcasts\ipod":"ta*.lnk"^
         "C:\mPodcasts\ipod":"te*.lnk"^
         "C:\mPodcasts\ipod":"tha*.lnk"^
         2^>NUL') Do @Echo %%G
    @Pause
    

    I hope this helps.

    BTW, this solution also solves the other issue mentioned in the question you linked in your post above. The where command does not output extensions 'beginning with', it properly matches it. Taking the linked 'discussion' question as a basis, *.htm only matches .htm extensions, (it does not include .html), and *.man does not match .manifest extensions.

    If you wish to test the output directly in your cmd.exe window, then here's the single line version:

    For /F "EOL=| Delims=" %G In ('%__AppDir__%where.exe C:\mPodcasts\ipod:s*.lnk C:\mPodcasts\ipod:ta*.lnk C:\mPodcasts\ipod:te*.lnk C:\mPodcasts\ipod:tha*.lnk 2^>NUL') Do @Echo %G