gpt4 book ai didi

visual-studio-2010 - VS2010 构建后事件,替换文件中的字符串。电源外壳?

转载 作者:行者123 更新时间:2023-12-01 08:07:07 24 4
gpt4 key购买 nike

在 VS2010 中成功构建后,我需要替换缩小的 .js 文件中的简单字符串。

所以我尝试从构建后事件窗口运行一个简单的命令行调用。

这个例子,来自这里:https://blogs.technet.com/b/heyscriptingguy/archive/2008/01/17/how-can-i-use-windows-powershell-to-replace-characters-in-a-text-file.aspx完全操纵生成的 .js 文件。出了点问题,我怀疑它在我缩小的 .js 文件中遇到了一些奇怪的字符,导致它搞砸了。

(Get-Content C:\Scripts\Test.js) | 
Foreach-Object {$_ -replace "// Old JS comment", "// New JS comment"} |
Set-Content C:\Scripts\Test.js

我怎样才能像在 unix 中一样在一行中完成如此简单的任务......?

最佳答案

如果能看到 diff 文件就好了。没有更多信息,一些信息:

  • Set-Content 在末尾添加一个新的空行(对您来说可能不是问题)
  • 您可以像这样使用 -replace 运算符:

    (gc C:\Scripts\Test.js) -替换 'a','b' | sc C:\Scripts\Test.js

    -replace 也适用于数组。

  • 您可以通过 [io.file]::ReadAllText('c:\scripts\test.js') 读取内容并使用-replace`,但同样,我不认为会有显着差异。

编辑:

计算字符串时使用双引号。示例:

$r = 'x'
$a = 'test'
'beg',1,2,"3x",'4xfour','last' -replace "1|$r","$a"

给予

beg
test
2
3test
4testfour
anything

要保存没有结束换行的内容,只需使用[io.file]::WriteAllText

$repl = (gc C:\Scripts\Test.js) -replace 'a','b' -join "`r`n"
[io.file]::WriteAllText('c:\scripts\test.js', $repl)

关于visual-studio-2010 - VS2010 构建后事件,替换文件中的字符串。电源外壳?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5116477/

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