Powershell体验了3个小时...
场景:需要在多个文件夹和子文件夹中的多个ASPX文件的顶部添加一行代码
这个是可能的吗?
我已经知道了如何搜索这些文件,但是添加这行代码是我遇到麻烦的地方。这就是我所拥有的,它不能工作
Get-ChildItem C:\domain_3 -recurse -include "*.aspx" |
Foreach-Object {
Add-Content -Path $targetFile -Value "<%@ Page Language="C#" Inherits="LandingPages.LandingPage" %>";
}谢谢大家
发布于 2012-06-23 06:00:11
$header=@"
"<%@ Page Language="C#" Inherits="LandingPages.LandingPage" %>
"@
Get-ChildItem C:\domain_3 -Recurse -Filter *.aspx | Foreach-Object {
$header`n" + (Get-Content $_.FullName | Out-String) | Set-Content -Path $_.FullName
}发布于 2012-06-23 03:52:21
试试这个(我现在不能测试)
创建一个函数:
function Insert-Content ($file) {
BEGIN {
$content = Get-Content $file
}
PROCESS {
$_ | Set-Content $file
}
END {
$content | Add-Content $file
}
}然后使用它:
(dir c:\domain_3 -r -filter *.txt ) | % { '<%@ Page Language="C#" Inherits="LandingPages.LandingPage" %>' | insert-content $_.fullname }发布于 2012-06-23 05:04:38
这项工作对编码部分不是很确定
$file="F:\Default2.aspx"
$content = Get-Content $file
$content = "<%@ Page Language=`"C#`" Inherits=`"LandingPages.LandingPage`" %>" + $content;
Set-Content $file $content https://stackoverflow.com/questions/11162939
复制相似问题