从路径中删除最后一个文件

时间:2014-10-16 11:21:04

标签: file powershell path directory

我有文件夹字符串

$filefolder = "C:\Users\user\Desktop\solr-4.10.1\solr-4.10.1\example\resources"

$filefolder = "C:\Users\user\Desktop\solr-4.10.1\solr-4.10.1\example\resources\"

我想要

$filefolder = "C:\Users\user\Desktop\solr-4.10.1\solr-4.10.1\example"

我的尝试没有帮助

4 个答案:

答案 0 :(得分:6)

Split-Path "C:\Users\user\Desktop\solr-4.10.1\solr-4.10.1\example\resources\"

答案 1 :(得分:2)

另一种选择:

$yourString -replace "\\[^\\]*(?:\\)?$"

Demo

答案 2 :(得分:1)

您可以在-replace运算符中使用正则表达式:

"C:\Users\user\Desktop\solr-4.10.1\solr-4.10.1\example\resources\" -replace "(.*)\\.+\\?$",'$1'
C:\Users\user\Desktop\solr-4.10.1\solr-4.10.1\example

"C:\Users\user\Desktop\solr-4.10.1\solr-4.10.1\example\resources" -replace "(.*)\\.+\\?$",'$1'
C:\Users\user\Desktop\solr-4.10.1\solr-4.10.1\example

答案 3 :(得分:0)

对于那些绊脚石寻找答案的人,我可以使用以下文档中的解决方案,将最后一个文件夹(或文件)与路径分开:https://ruby-doc.org/stdlib-2.3.3/libdoc/pathname/rdoc/Pathname.html#class-Pathname-label-Example+2-3A+Using+standard+Ruby

使用OP的示例字符串进行演示:

dir, base = File.split(filefolder)
puts dir  # "C:\Users\user\Desktop\solr-4.10.1\solr-4.10.1\example"
puts base  # "resources"
相关问题