windows 批量查找文件扩展名
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/138819/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
batch find file extension
提问by Vhaerun
If I am iterating over each file using :
如果我使用以下方法遍历每个文件:
@echo off
FOR %%f IN (*\*.\**) DO (
echo %%f
)
how could I print the extension of each file? I tried assigning %%f to a temporary variable, and then using the code : echo "%t:~-3%"
to print but with no success.
如何打印每个文件的扩展名?我尝试将 %%f 分配给一个临时变量,然后使用代码 :echo "%t:~-3%"
打印但没有成功。
回答by Sam Holloway
The FOR command has several built-in switches that allow you to modify file names. Try the following:
FOR 命令有几个允许您修改文件名的内置开关。请尝试以下操作:
@echo off
for %%i in (*.*) do echo "%%~xi"
For further details, use help for
to get a complete list of the modifiers - there are quite a few!
有关更多详细信息,请使用help for
获取完整的修饰符列表 - 有很多!
回答by paxdiablo
This works, although it's not blindingly fast:
这行得通,虽然速度并不快:
@echo off
for %%f in (*.*) do call :procfile %%f
goto :eof
:procfile
set fname=%1
set ename=
:loop1
if "%fname%"=="" (
set ename=
goto :exit1
)
if not "%fname:~-1%"=="." (
set ename=%fname:~-1%%ename%
set fname=%fname:~0,-1%
goto :loop1
)
:exit1
echo.%ename%
goto :eof
回答by system PAUSE
Sam's answer is definitely the easiest for what you want. But I wanted to add:
Sam的答案绝对是您想要的最简单的答案。但我想补充一点:
Don't set
a variable inside the ()
's of a for
and expect to use it right away, unless you have previously issued
不要在set
a 的()
's 中for
使用变量并期望立即使用它,除非您之前已发出
setlocal ENABLEDELAYEDEXPANSION
and you are using !
instead of % to wrap the variable name. For instance,
并且您正在使用!
而不是 % 来包装变量名称。例如,
@echo off
setlocal ENABLEDELAYEDEXPANSION
FOR %%f IN (*.*) DO (
set t=%%f
echo !t:~-3!
)
Check out
查看
set /?
for more info.
了解更多信息。
The other alternative is to call a subroutine to do the set
, like Paxshows.
另一种选择是调用一个子程序来执行set
,就像Pax显示的那样。