通配符以精确扩展名获取文件 [英] Wildcard to get files by exact extension

查看:267
本文介绍了通配符以精确扩展名获取文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在诊断需要使用 *。log 扩展的循环文件的更大批处理脚本时,我发现了一个有趣的行为。在包含如下文件的示例目录中:

While diagnosing a larger batch script that needs to loop files with *.log extension I've found a funny behaviour. In a sample directory with files like this:

bar.log
foo.log
foo.log.ignore
foo.log.log-1676521099
not-related

...我的小测试脚本:

... my little test script:

@echo off
setlocal enabledelayedexpansion
set DEF_LOG="C:\test\*.log"
for %%i in (%DEF_LOG%) do (
    echo %%i
)

...打印:

C:\test\bar.log
C:\test\foo.log
C:\test\foo.log.log-1676521099

进一步深入,我发现这是Windows通配符的设计方式:

Digging deeper, I've found that's how Windows wildcards have been designed:

C:\>dir "C:\test\*.log" /b
bar.log
foo.log
foo.log.log-1676521099

我的问题是:如何列出所有以 .log

My question is: how can I list all files that end exactly with .log?

推荐答案

行为的来源是短文件名。除非禁用,超过旧8.3格式的文件名将被分配一个与8.3格式匹配的短名称。如果扩展名超过3个字符,则短名称将由长名称扩展名的前3个字符组成。执行 dir / xC:\test\ * .log以查看短名称。

The source of the behavior is short file names. Unless disabled, file names that exceed the old 8.3 format will be assigned a short name that does match the 8.3 format. If the extension exceeds 3 characters, then the short name will consist of the first 3 characters of the long name extension. Execute the dir /x "C:\test\*.log" to see the short names.

所有命令将在查找匹配项时测试长名和短名。

All commands will test both the long and the short name when looking for matches.

获取所需结果的常用方法是使用DIR / B管道传送到FINDSTR。请注意,在扩展FOR变量时应避免使用延迟扩展,因为这会损坏包含的值。 字符可能出现在文件名中。

A common way to get your desired result is to use DIR /B piped to FINDSTR. Note that you should avoid using delayed expansion when expanding a FOR variable because that will corrupt values that contain !. The ! character may appear in a file name.

@echo off
pushd "C:\test"
for /f "eol=: delims=" %%F in ('dir *.log^|findstr /lie ".log"') do echo %%~fF
popd

这篇关于通配符以精确扩展名获取文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆