2016-11-09 78 views
0

我想修改位于计算机上所有用户的AppData文件夹中的文件中的一行。在位于计算机上所有用户的AppData文件夹中的文件中修改一行

线在prefs.js文件来改变开始:

user_pref("mail.server.server1.directory", "C:\\Users\ 

我会更换整个行:

user_pref("mail.server.server1.directory", "C:\\ Thunderbird Mail\\Local Folders") 

我在每个用户配置文件这样做。

我开始了一个剧本,但它不工作:

$configFiles = Get-ChildItem C:\users\*\appdata prefs.js -rec 
$ligne1 = 'user_pref("mail.server.server1.directory", "C:\\Users\' 
$ligne2 = 'user_pref("mail.server.server1.directory", "C:\\Thunderbird\\Mail\\Local Folders");' 
foreach ($file in $configFiles) { 
    (Get-Content $file.PSPath) | 
     ForEach-Object { $_ -replace $ligne1.+, $ligne2 } | 
     Set-Content $file.PSPath 
} 
+0

因为每个用户只有一个活动的Thundebird配置文件,所以我会在另一个方法后接受一个用户。带有-rec的gci将启动一些dir级别为高。我会以'$ configfiles = Get-ChildItem C:\ users \ $ env:username \ appdata \ roaming \ Thunderbird \ Profiles \ *。default \ prefs.js'开始 – LotPings

+0

yes是很好的LotPings! 事实上,我会进一步修改每行中的prefs.js文件,但在txt文件中的用户列表中 此脚本的最大问题是,我无法在第二个值中替换第二个值,因为在那里是莎莎和报价 – Rosita

回答

0

-replace运营商做正则表达式替换,所以你一定要逃逸的匹配字符串($ligne1)特殊字符,特别是反斜线和括号。

替换此:

(Get-Content $file.PSPath) | 
    ForEach-Object { $_ -replace $ligne1.+, $ligne2 } | 
    Set-Content $file.PSPath 

与此:

(Get-Content $file.FullName) -replace ([regex]::Escape($ligne1) + '.+'), $ligne2 | 
    Set-Content $file.FullName 

,问题就会消失。

+0

谢谢Ansgar!是工作。 我只想要最后一个问题。 $ line1必须以user_pref(“mail.server.server1.directory”,“C:\\ Users \”)开头,因为在C:\用户之后有很多用户。 – Rosita

+0

什么问题?Get-Content生成如果你想确保'$ ligne1'位于行的开头,用逗号('-replace('^'+ [regex] :: Esc ...)')。 –

+0

例如: 该行是:user_pref(“mail.server.server1.directory”,“C:\\ Users \ toto \ appdata \ 54654654 \ 151555566 \ 644454545454 我想用我的脚本替换user_pref(“mail.server.server1.directory”,“C:\\ Thunderbird Mail \\ Local Folders”) 的所有行,结果为:user_pref(“mail.server.server1.directory” ,“C:\\ Thunderbird Mail \\本地文件夹”)566 \ 644454545454 行结束好,因为第一行的结尾是 它不能与'^'一起使用 – Rosita

相关问题