例如,如果键入,cmd.exe echo %1
它会显示所选文件的文件名和扩展名。但我想要做的只是获取所选文件的扩展名。
有人知道怎么做吗?
答案1
cmd.exe echo %~x1
%* return the remainder of the command line starting at the first command line argument (in Windows NT 4, %* also includes all leading spaces)
%~dn return the drive letter of %n (n can range from 0 to 9) if %n is a valid path or file name (no UNC)
%~pn return the directory of %n if %n is a valid path or file name (no UNC)
%~nn return the file name only of %n if %n is a valid file name
%~xn return the file extension only of %n if %n is a valid file name
%~fn return the fully qualified path of %n if %n is a valid file name or directory
答案2
您可以使用扩展属性在 Powershell 中轻松执行此操作。
举个例子(尽管还有更多方法可以实现这一点:
Get-ChildItem | select Extension
在 cmd.exe 中:
for %i in (*.*) do echo "%~xi"