Powershell - 在多个位置列出文件夹并在这些位置更改文件 [英] Powershell - listing folders in mulitple places and changing files in those places

查看:36
本文介绍了Powershell - 在多个位置列出文件夹并在这些位置更改文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置一个脚本,旨在更改大约 50 个文件中的 100 多个占位符.一般来说,我得到了一个可能占位符的列表,以及它们的值.我得到了一些具有 exe.config 文件和 ini 文件的应用程序.这些应用程序存储在 c:\programfiles(x86)\ 和 d:\ 中,一般来说,我设法让它在一个路径下工作,但不是两个.我可以很容易地编写代码来替换两次,但这给我留下了很多混乱的代码,其他人更难阅读.

I'm trying to set up a script designed to change a bit over 100 placeholders in probably some 50 files. In general I got a list of possible placeholders, and their values. I got some applications that have exe.config files as well as ini files. These applications are stored in c:\programfiles(x86)\ and in d:\In general I managed to make it work with one path, but not with two. I could easily write the code to replace twice, but that leaves me with a lot of messy code and would be harder for others to read.

ls c:\programfiles(x86) -Recurse | where-object {$_.Extension -eq ".config" -or $_.Extension -eq ".ini"} | %{(gc $PSPath) | %{
$_ -replace "abc", "qwe" `
-replace "lkj", "hgs" `
-replace "hfd", "fgd"
} | sc $_PSPath; Write-Host "Processed: " + $_.Fullname}

我尝试通过放置 $a = path1, $b = path2, c$ = $a + $b 来包含 2 条路径,这似乎可以让 ls 命令在两个不同的地方运行.但是,它似乎没有存储文件所在的路径,因此它会尝试替换在您当前运行脚本的文件夹中找到的文件名.因此,即使我可能在文件应该在的地方之一,它也不在另一个......

I've tried to include 2 paths by putting $a = path1, $b = path2, c$ = $a + $b and that seems to work as far as getting the ls command to run in two different places. however, it does not seem to store the path the files are in, and so it will try to replace the filenames it has found in the folder you are currently running the script from. And thus, even if I might be in one of the places where the files is supposed to be, it's not in the other ...

所以.. 知道如何让 Powershell 在 2 个不同的地方列出文件并在两个地方替换相同的变量而不必两次代码吗?我考虑将我必须使用两次的代码放入一个变量中,在我需要时调用它而不是再次编写它,但它似乎在使用它之前解决了代码,并且这并没有完全给我结果,因为数据来自第一部分.

So .. Any idea how I can get Powershell to list files in 2 different places and replace the same variables in both places without haveing to have the code twice ? I thought about putting the code I would have to use twice into a variable, calling it when I needed to instead of writing it again, but it seemed to resolve the code before using it, and that didn't exactly give me results since the data comes from the first part.

推荐答案

如果你有一个很酷的管道,那么每个问题看起来就像......呃......流体?对象?我没有任何线索.但无论如何,只需添加另一个层(并在此过程中修复一些问题):

If you got a cool pipeline, then every problem looks like ... uhm ... fluids? objects? I have no clue. But anyway, just add another layer (and fix a few problems along the way):

$places = 'C:\Program Files (x86)', 'D:\some other location'
$places |
  Get-ChildItem -Recurse -Include *.ini,*.config |
  ForEach-Object {
    (Get-Content $_) -replace 'abc', 'qwe' `
                     -replace 'lkj', 'hgs' `
                     -replace 'hfd', 'fgd' |
      Set-Content $_
    'Processed: {0}' -f $_.FullName
  }

显着变化:

  • 第一步只需遍历要抓取的文件夹列表.
  • 直接在 Get-ChildItem 中进行过滤可以加快速度并保存 Where-Object.
  • -replace 可以直接应用于数组,不需要另一个 ForEach-Object 在那里.
  • Just iterate over the list of folders to crawl as the first step.
  • Doing the filtering directly in Get-ChildItem makes it faster and saves the Where-Object.
  • -replace can be applied directly to an array, no need for another ForEach-Object there.

如果替换的数量很大,您可以考虑使用哈希表来存储它们,这样您就不会出现 20 行 -replace 'foo', 'bar'.

If the number of replacements is large you may consider using a hashtable to store them so that you don't have twenty lines of -replace 'foo', 'bar'.

这篇关于Powershell - 在多个位置列出文件夹并在这些位置更改文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆