【问题标题】:Concatenate network path + variable连接网络路径 + 变量
【发布时间】:2020-02-26 02:29:15
【问题描述】:

如何将此路径与此变量连接?

$file = "My file 01.txt" #The file name contains spaces

$readfile = gc "\\server01\folder01\" + ($file) #It doesn't work

谢谢

【问题讨论】:

    标签: powershell


    【解决方案1】:

    有几种方法。最简单的:

    $readfile = gc \\server01\folder01\$file
    

    你的方法很接近:

    $readfile = gc ("\\server01\folder01\" + $file)
    

    您也可以使用Join-Path 例如:

    $path = Join-Path \\server01\folder01 $file
    $readfile = gc $path
    

    【讨论】:

    • 如果我想给现有文件名添加后缀怎么办?这不起作用:copy-Item ".png" -destination ".png"+(Get-Date -Format g).Replace("/", "-").Substring(0, 10)
    • 使用Join-Path 更好。所以你看到发生了什么,我不知道“gc”做了什么。这就是我使用 -> Join-Path -Path "path" -ChildPath "childpath" 的原因
    • "gc" 是 Get-Content 的别名
    猜你喜欢
    • 2019-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-09-13
    • 2015-09-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多