gpt4 book ai didi

Powershell Add-Content 应该创建路径但抛出异常 "Could not find a part of the path"

转载 作者:行者123 更新时间:2023-12-02 23:01:56 26 4
gpt4 key购买 nike

我正在使用 powershell Add-Content创建一个文件。但是,当文件的文件夹不存在时,我收到错误消息:
Add-Content : Could not find a part of the path 'C:\tests\test134\logs\test134.log'.
根据文档,这应该创建文件夹:

PS C:\> Add-Content -Value (Get-Content "test.log") -Path  "C:\tests\test134\logs\test134.log" 

This command creates a new directory and file and copies the content of an existing file to the newly created file.

This command uses the Add-Content cmdlet to add the content. The value of the Value parameter is a Get-Content command that gets content from an existing file, Test.log.

The value of the path parameter is a path that does not exist when the command runs. In this example, only the C:\Tests directories exist. The command creates the remaining directories and the Test134.log file.



https://docs.microsoft.com/en-us/powershell/module/microsoft.powershell.management/add-content?view=powershell-5.1

似乎是 Add-Content 中的一个明显问题,不是吗?
你能重现这个吗?

编辑:我正在运行 PowerShell 版本 5.1.16299.64

BR马蒂亚斯

最佳答案

Add-Content cmdlet 不能创建路径,只能创建文件。有用:

$Path = "C:\tests\test134\logs2\test134.log"
$Path |% {
If (Test-Path -Path $_) { Get-Item $_ }
Else { New-Item -Path $_ -Force }
} | Add-Content -Value 'sample content'

关于Powershell Add-Content 应该创建路径但抛出异常 "Could not find a part of the path",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48425562/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com