powershell PowerShell获取文件名

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了powershell PowerShell获取文件名相关的知识,希望对你有一定的参考价值。

<#
.Synopsis
    Returns file name from full file path
.DESCRIPTION
    Takes a fully provided path to a file and returns just the file name of that path
.EXAMPLE
    Get-FileName -FilePath C:\afolder\afile.txt

    Returns just the filename from the fully provided path
.EXAMPLE
    Get-FileName -FilePath $ImagePath -Verbose

    Returns just the filename from the fully provided path in $ImagePath with verbose output
.PARAMETER FilePath
    Full file sytem path to specified file
.OUTPUTS
    System.String
.NOTES
    Author: Jake Morrison - @jakemorrison - http://techthoughts.info/
#>
function Get-FileName {
    [CmdletBinding()]
    Param
    (
        [Parameter(Mandatory = $true,
            HelpMessage = 'Full path to file')]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [string]$FilePath
    )
    Write-Verbose -Message "Processing $FilePath ..."
    #-------------------------------------------------
    #$divide = $FilePath.Split("\")
    #$fileName = $divide[$divide.Length - 1]
    #$results = $fileName
    #-------------------------------------------------
    $results = ""
    try {
        $fileInfo = Get-ChildItem -Path $FilePath -ErrorAction Stop
        $fileName = $fileInfo.Name
        Write-Verbose -Message "Filename is: $fileName"
        $results = $fileName
    }#try_Get-ChildItem
    catch {
        Write-Warning "The file information for $FilePath could not be determined."
        $results = "ERROR"
    }#catch_Get-ChildItem
    return $results
}#function_Get-FileName

以上是关于powershell PowerShell获取文件名的主要内容,如果未能解决你的问题,请参考以下文章

Powershell: powershell获取当前目录下最新创建的文件

在 PowerShell 中获取文件版本

powershell PowerShell:获取与HP Print Driver DIFxAPI.dll问题有关的文件大小。

如何在 Powershell 中使用文件命名约定获取文件

powershell 此PowerShell脚本是从SCCM DB获取SCEP日志并将其导出为CSV文件

怎么用BAT或者powershell获取主机名和登录用户保存到TXT文件中,或者用其他啥方式?