Why are also *.tiff files output by Windows command DIR on searching for *.tif files?

こ雲淡風輕ζ 提交于 2021-02-05 07:45:47

问题


I want to use the Windows command DIR in order to find only TIF files, i.e. files with extension .tif. Therefore I use the following small batch file:

for /f "delims=" %%a IN ('dir /b /a-d /s "C:\wolter\testversion-input\*.tif"') do echo %%a

for /f "delims=" %%a IN ('dir /b /a-d /s "R:\wolter\testversion-input\*.tif"') do echo %%a

Now I am wondering that this command also finds TIFF files, i.e. files with extension .tiff.

So I made some tests and found out that the command DIR finds TIF AND TIFF files on my drive C: in folder C:\wolter\testversion-input\ with stored TIF and TIFF files, but on my drive R: in folder R:\wolter\testversion-input\ are found by the command DIR only TIF files although this folder contains also TIF and TIFF files.

My goal is to find only TIF files.

How to find and process only files with file extension .tif with excluding files with file extension .tiff?


回答1:


The extensions are being matched, by default, according to their 8.3 compliant file names. You could disable 8.3 naming, (which restricts file names to eight characters and optional extensions to three characters).

To do that for your NTFS partition, open a Command Prompt window 'Run as administrator', and enter:

"%__AppDir__%fsutil.exe" behavior set disable8dot3 1

Please note that disabling 8.3 naming, may generally improve directory enumeration performance especially in situations where large numbers of similarly named files exist in the same directory. However some applications may not be able to locate files and directories which use long file names, LFN's. Also disabling does not delete the already created SFN's.

For alternatives, you have a few options.

  1. Use metavariable expansion of the results to check for a matching extension (slowest):

    For /F "Delims=" %%G In ('Dir /A-D /B /S "R:\wolter\testversion-input\*.tif"') Do If "%%~xG" == ".tif" Echo %%G
    
  2. Pass the results from Dir through findstr.exe to filter only those which end with the case insensitive string .tif:

    For /F "Delims=" %%G In ('Dir /A-D /B /S "R:\wolter\testversion-input\*.tif" ^| "%__AppDir__%findstr.exe" /EIL ".tif"') Do Echo %%G
    
  3. Use where.exe which will match the exact extension:

    For /F "Delims=" %%G In ('""%__AppDir__%where.exe" /R "R:\wolter\testversion-input" "*.tif" 2> NUL"') Do Echo %%G
    

    However, where.exe, also matches against extensions listed under %PATHEXT%, this means that whilst it may omit your .tiff files, it could feasibly return extensions like .tif.com, .tif.exe, .tif.bat, .tif.cmd, .tif.vbs, .tif.vbe, .tif.js, .tif.jse, .tif.wsf, .tif.wsh, and .tif.msc too. You should therefore, for extra robustness, temporarily undefine that variable:

    For /F "Delims=" %%G In ('"(Set PATHEXT=) & "%__AppDir__%where.exe" /R "R:\wolter\testversion-input" "*.tif" 2> NUL"') Do Echo %%G
    


来源:https://stackoverflow.com/questions/63579232/why-are-also-tiff-files-output-by-windows-command-dir-on-searching-for-tif-f

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!