windows - PS不是每次都创建一个新文件
问题描述
每次执行时,我都使用以下 PS cmdlet 创建一个新的 json 文件。应该覆盖现有的 json 文件
$jsonformatOutput = "JSON-BEGIN" + $jsonOutput + "JSON-END"
$jsonformatOutput | New-Item -path $myFileName -Force
但是,如果它们已经是具有相同文件名的现有文件,则不会创建新的 json 文件。
解决方案
New-Item
不是为这种情况选择的功能(因为它实际上应该只用于创建新项目)。
你应该改用的是Out-File
$jsonformatOutput = "JSON-BEGIN" + $jsonOutput + "JSON-END"
$jsonformatOutput | Out-File -Filepath $myFileName
这会将变量写入文件$myFileName
并覆盖该文件(如果它仍然存在)。
如果您想将内容添加到现有文件而不是覆盖它,您可以使用-Append
.
推荐阅读
- c++ - 为什么 ComPtr 的函数返回的值与 & 不同?
- javascript - 在另一个innerHTML js中输出html元素内容
- python-3.x - 接收 python 类型错误:预期的 str、字节或 os.PathLike 对象,而不是 StringVar
- python - 加载权重 TensorFlow 2.0 模型错误
- javascript - 来自 json 的数据未填充到我的 Angular 8 应用程序中
- r - R:在数据框的列之间添加插值?
- android - 如何检查选项卡式布局中可见的片段?
- python - 在 Windows 上安装 requirements-test.txt 时出错
- javascript - 无法使用 Javascript 更改按钮的链接
- ionic-framework - 在 ionic react 应用程序中创建 www 文件夹以进行发布