如何在 PowerShell 中获取没有扩展名的文件路径? [英] How do I get a file path without extension in PowerShell?
本文介绍了如何在 PowerShell 中获取没有扩展名的文件路径?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我的 powershell 2.0 脚本中有一个变量的绝对路径.我想去掉扩展名但保留完整路径和文件名.最简单的方法是什么?
I have an absolute path in a variable in my powershell 2.0 script. I want to strip off the extension but keep the full path and file name. Easiest way to do that?
因此,如果我在名为 $file
我想回来
C:\Temp\MyFolder\mytextfile.fake.ext
推荐答案
if 是 [string]
类型:
$file.Substring(0, $file.LastIndexOf('.'))
如果是 [system.io.fileinfo]
类型:
join-path $File.DirectoryName $file.BaseName
或者你可以投射它:
join-path ([system.io.fileinfo]$File).DirectoryName ([system.io.fileinfo]$file).BaseName
这篇关于如何在 PowerShell 中获取没有扩展名的文件路径?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文