【问题标题】:Split path by backslashes and join it with slashes用反斜杠分割路径并用斜杠连接它
【发布时间】:2017-07-14 19:04:45
【问题描述】:

我想在\ 的反斜杠处拆分两条路径,并用斜杠/ 将它们连接起来。

但我收到一个错误,提示我没有该文件夹的权限,但我有管理员权限。

$PathOne = C:\example\example
$PathTwo = C:\example\example

$PathOne
$PathOne $PathOne("\")
(Get-Content $PathTwo) -Join ("/")
$$PathTwo = $NewPath
$$PathTwo -Split("\")
(Get-Content $PathTwo) -Join ("/")

错误是用“/”加入。

完整的错误代码: + FullyQualifiedErrorId : GetContentReaderUnauthorizedAccessError,Microsoft.PowerShell.Commands.GetContentCommand

【问题讨论】:

  • 您能否编辑问题并显示您想要达到的具体目标?添加任何错误消息和产生错误的实际命令。
  • $PathOne $$PathOne("\") 的目标是什么?最后一个标记$$ 运算符在这里是一个奇怪的构造,因此缺少对变量的赋值和使用 Get-Content 来做什么,呃,什么?
  • 你只是想把C:\example\example变成C:/example/example吗?如果是这样$NewPathOne = $PathOne.Replace('\','/')
  • 是的,詹姆斯,我想这样做,谢谢它有效
  • 很高兴我能提供帮助,如果您对我的回答感到满意,您可以Mark it as Accepted

标签: powershell


【解决方案1】:

要将C:\example\example 转换为C:/example/example,您可以使用替换方法(Blog on it),如下所示:

$NewPathOne = $PathOne.Replace('\','/')

【讨论】:

    【解决方案2】:

    如果您没有指定您尝试将其视为 get-content 命令的文件,则会发生此错误。您必须指定文件的路径:get-content c:\patch\textfile.text 或: $PathOne = "c:\patch\textfile.text" $PathTwo = "c:\patch2\textfile2.text"(这是一个例子 - 你可以选择你的路径)
    如果要将分隔符“\”替换为“/”,请尝试以下操作:

        $PathOne = "C:\path1" 
        $PathTwo = "C:\path2"  
        $repl1 = Get-ChildItem $PathOne -Recurse -force   
        $repl2 = Get-ChildItem $PathTwo -Recurse -force   
    
        $repl1 | % {
    
        $_.FullName.ToString().Replace("\","/")
    
        } 
    
       $repl2| % {
    
        $_.FullName.ToString().Replace("\","/")
    
        }
    

    【讨论】:

      猜你喜欢
      • 2013-07-23
      • 2012-08-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-11-25
      相关资源
      最近更新 更多