VS2010生成后事件,在一个文件中替换字符串。 PowerShell的? [英] VS2010 Post-build event, replace string in a file. Powershell?
问题描述
我需要VS2010编译成功后,更换一个简单的字符串在缩小的.js文件。
I need to replace a simple string in a minified .js file after a successful build in VS2010.
所以我想从运行生成后事件窗口一个简单的命令行调用。
So I'm trying to run a simple command line call from the Post-build events window.
这个例子,从这里开始:<一href=\"https://blogs.technet.com/b/heyscriptingguy/archive/2008/01/17/how-can-i-use-windows-powershell-to-replace-characters-in-a-text-file.aspx\" rel=\"nofollow\">https://blogs.technet.com/b/heyscriptingguy/archive/2008/01/17/how-can-i-use-windows-powershell-to-replace-characters-in-a-text-file.aspx完全mangulates生成的.js文件。什么是错的,我怀疑它是跨在我的精缩的.js一些奇怪的字符来文件螺丝它。
This example, from here: https://blogs.technet.com/b/heyscriptingguy/archive/2008/01/17/how-can-i-use-windows-powershell-to-replace-characters-in-a-text-file.aspx totally mangulates the resulting .js file. Something is wrong, I suspect it is coming across some weird chars in my minified .js file that screws it up.
(Get-Content C:\Scripts\Test.js) |
Foreach-Object {$_ -replace "// Old JS comment", "// New JS comment"} |
Set-Content C:\Scripts\Test.js
我
如何才能实现这样一个简单的任务,像我可以在UNIX做一行..?
How can I achieve such a simple task like I could do in unix in a single line..?
推荐答案
这将是巨大看到差异文件。没有更多的信息,一些信息:
It would be great to see the diff file. Without more info, some info:
-
设置内容
添加在最后一个新的空行(可能不是你的问题) -
您可以使用
-replace
运营商是这样的:
Set-Content
adds a new empty line at the end (probably not a problem for you)You can use
-replace
operator like this:
(GC C:\\脚本\\ Test.js)-replace'A','B'| SC C:\\脚本\\ Test.js
(gc C:\Scripts\Test.js) -replace 'a','b' | sc C:\Scripts\Test.js
-replace
阵列上也可以。
编辑:
评估字符串时双引号。例如:
Double quotes are used when evaluating the string. Example:
$r = 'x'
$a = 'test'
'beg',1,2,"3x",'4xfour','last' -replace "1|$r","$a"
给
beg
test
2
3test
4testfour
anything
要保存没有结束新行的内容,只需要使用 [io.file] :: WriteAllText
To save the content with no ending new line, just use [io.file]::WriteAllText
$repl = (gc C:\Scripts\Test.js) -replace 'a','b' -join "`r`n"
[io.file]::WriteAllText('c:\scripts\test.js', $repl)
这篇关于VS2010生成后事件,在一个文件中替换字符串。 PowerShell的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!