powershell - extract file name and extension

前端 未结 9 892
温柔的废话
温柔的废话 2020-12-24 04:09

I need to extract file name and extension from e.g. my.file.xlsx. I don\'t know the name of file or extension and there may be more dots in the name, so I need to search the

相关标签:
9条回答
  • 2020-12-24 04:56

    This is an adaptation, if anyone is curious. I needed to test whether RoboCopy successfully copied one file to multiple servers for its integrity:

       $Comp = get-content c:\myfile.txt
    
    ForEach ($PC in $Comp) {
        dir "\\$PC\Folder\Share\*.*" | Select-Object $_.BaseName
    }
    

    Nice and simple, and it shows the directory and the file inside it. If you want to specify one file name or extension, just replace the *'s with whatever you want.

        Directory: \\SERVER\Folder\Share
    
    Mode                LastWriteTime     Length Name                                                                                                                                             
    ----                -------------     ------ ----                                                                                                                                             
    -a---         2/27/2015   5:33 PM    1458935 Test.pptx                                                                                                             
    
    0 讨论(0)
  • 2020-12-24 04:57

    Check the BaseName and Extension properties of the FileInfo object.

    0 讨论(0)
  • 2020-12-24 04:58
    PS C:\Users\joshua> $file = New-Object System.IO.FileInfo('file.type')
    PS C:\Users\joshua> $file.BaseName, $file.Extension
    file
    .type
    
    0 讨论(0)
提交回复
热议问题