【问题标题】:Powershell Path ReplacementPowershell 路径替换
【发布时间】:2023-03-20 23:22:02
【问题描述】:

嘿,我的道路又是我了:D

我将路径写入 TXT 文件。

例如:

C:\Ex\am\ple\path.txt  
C:\Ex\am\ple\file.txt  
C:\Ex\am\ple\text.txt 

现在我想替换每个路径末尾的“*.txt”。

我尝试了以下方法:

(Get-Content ".\MYPATHS.txt") | Foreach-Object {$_ -replace "\*.txt"}| Set-Content ".\MYPATHS.txt"

我的错在哪里?

【问题讨论】:

  • 试试$_ -replace "\.txt"
  • 什么都不能代替。
  • 或者只是 [string]'C:\Ex\am\ple\file.txt'.TrimEnd('txt') + 'new' ...顺便说一句,'-replace' 不需要 2操作数?
  • 或者如果你坚持使用正则表达式 -- 'C:\Ex\am\ple\file.txt' -replace '.txt', '.new' -- 你最好将正则表达式锚定在虽然结束
  • @JaquelineVanek 抱歉,我是 powershell 的新手,我的英语技能不是很好。你能解释得更准确一点吗?

标签: string powershell text replace


【解决方案1】:

k,如果您只需要路径(不包括文件):

'C:\Ex\am\ple\path.txt', 'C:\Ex\am\ple\file.txt' | Split-Path -Parent

上面的列表

'C:\Ex\am\ple\path.txt', 'C:\Ex\am\ple\file.txt'

替换“获取内容”。输出:

C:\Ex\am\ple
C:\Ex\am\ple

【讨论】:

  • 没有很好解释的问题的好例子,@Jaqueline 投票支持理解
猜你喜欢
  • 2011-01-20
  • 2014-07-13
  • 2020-10-30
  • 1970-01-01
  • 2011-01-24
  • 2018-01-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多