检查文件扩展名 [英] Check file extension

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

问题描述

我正在使用以下PowerShell代码,并且需要在if条件下检查其扩展名

I am using the following PowerShell code and I need to check its extension in an if condition

foreach ($line in $lines) {
    $extn = $line.Split("{.}")[1]
    if ($extn -eq "xml" )
    {
    }
}

有没有一种简单的方法可以在PowerShell脚本中检查字符串的字符串扩展名?

Is there a straightforward way to check string extensions in PowerShell script in case of strings?

推荐答案

您可以简单地使用

You can simply use the GetExtension function from System.IO.Path:

foreach ($line in $lines) {
    $extn = [IO.Path]::GetExtension($line)
    if ($extn -eq ".xml" )
    {
    }
}

演示:

PS > [IO.Path]::GetExtension('c:\dir\file.xml')
.xml   
PS > [IO.Path]::GetExtension('c:\dir\file.xml') -eq '.xml'
True
PS > [IO.Path]::GetExtension('Test1.xml') # Also works with just file names
.xml    
PS > [IO.Path]::GetExtension('Test1.xml') -eq '.xml'
True    
PS > 

这篇关于检查文件扩展名的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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