【发布时间】:2013-11-01 14:28:40
【问题描述】:
在一个批处理文件中,我需要在一个文件中使用脚本前面的变量值 (%tokenvar%) 更改 ##token## 的所有实例(只是我自己的占位符),并输出结果到一个文件。从这里的答案我想出了调用powershell:
type file.json|powershell -Command "$input|ForEach-Object{ $_ -replace \"##token##\", \"%tokenvar%\" }" > file.2.json
问题是生成的文件以 80 个字符包装,我不需要它!
我该怎么做?
【问题讨论】:
-
您可以尝试通过管道连接到
Out-String并使用-Width <int>参数吗? -
得到一个错误:Out-File : Cannot validate argument on parameter Encoding'。参数“filepath”不属于 ValidateSet 属性指定的集合“unicode,utf7,utf8,utf32,ascii,bigendianunicode,default,oem”。请提供集合中的参数,然后重试该命令。
-
你会注意到,我尝试使用 Out-File,因为使用 Out-String 然后重定向输出会得到相同的 80 字符裁剪。
-
您是否将
-replace的输出通过管道传输到Out-String?这对我有用"foo bar" -replace "bar", "baz" | out-string -width 120 -
ah - 发现重定向到文件 outside powershell 部分的问题。一旦我把它里面 一切都很好。类型 file.json|powershell -Command "$input|ForEach-Object{ $_ -replace \"##token##\", \"%tokenvar%\" } | Out-String -width 300 > file.2。 json"
标签: powershell batch-file command