Powershell更换-如何避免在80个字符时断线

Powershell更换-如何避免在80个字符时断线,powershell,batch-file,command,Powershell,Batch File,Command,在批处理文件中,我需要在一个文件中更改#########(仅我自己的占位符)的所有实例,并将结果输出到一个文件中。根据这里的回答,我想到了调用powershell: @echo off setlocal DisableDelayedExpansion for /F "delims=" %%a in (file.json) do ( set "line=%%a" setlocal EnableDelayedExpansion echo !line:##token##=%toke

在批处理文件中,我需要在一个文件中更改#########(仅我自己的占位符)的所有实例,并将结果输出到一个文件中。根据这里的回答,我想到了调用powershell:

@echo off
setlocal DisableDelayedExpansion

for /F "delims=" %%a in (file.json) do (
   set "line=%%a"
   setlocal EnableDelayedExpansion
   echo !line:##token##=%tokenvar%!
   endlocal
)
type file.json | powershell-Command“$input | ForEach Object{$\uu-replace\”###token##\,\%tokenvar%\”}>file.2.json

问题是结果文件被包装为80个字符,我不需要它


如何执行此操作?

您可以直接在批处理文件中执行此操作,而无需Powershell:

@echo off
setlocal DisableDelayedExpansion

for /F "delims=" %%a in (file.json) do (
   set "line=%%a"
   setlocal EnableDelayedExpansion
   echo !line:##token##=%tokenvar%!
   endlocal
)

此批处理文件假定
file.json
中没有空行,并且
%tokenvar%
的值没有特殊的批处理字符。如果需要,这两点可能是固定的。

在这种情况下,您不需要使用
输出字符串
,因为
输出文件
也有一个
宽度
参数:

... | Out-File File.2.json -Width 300"

只需确保
| Out File…
是PowerShell命令的一部分。

问题在于PowerShell(>)中的重定向操作符将变成宽度为80个字符的Out文件。您可以使用任意大宽度的Out文件(我更喜欢10k)或管道来设置内容(速度较慢)。您还可以将结果分配到一个变量中,并使用[IO.File]::WriteAllText执行更新。

您是否可以尝试通过管道连接到
输出字符串
并使用
-Width
参数?获取错误:输出文件:无法验证参数编码的参数”。参数“filepath”不属于ValidateSet属性指定的集合“unicode、utf7、utf8、utf32、ascii、bigendianunicode、default、oem”。请提供集合中的参数,然后重试该命令。您会注意到,我尝试使用Out File,因为使用Out String然后重定向输出会得到相同的80个字符的裁剪。您是否将输出从
-replace
传输到Out String?这适用于我
“foo bar”-替换“bar”,“baz”|输出字符串-宽度120
ah-发现问题重定向到powershell部件外部的文件。一旦我把它放进去,一切都很好。键入file.json | powershell-Command“$input | ForEach Object{$|-replace\”,\%tokenvar%\”}| Out String-width 300>file.2.json”