【问题标题】:Powershell script to extract folder names and text data from files用于从文件中提取文件夹名称和文本数据的 Powershell 脚本
【发布时间】:2016-06-29 18:13:44
【问题描述】:

我正在为我们的一位副总裁处理一个快速项目。

我们有几千个 CAD 作业存储在网络文件共享中。文件结构使得 CAD 作业有一个父文件夹。文件夹名称的一部分包含作业编号。在文件夹中,有 1 到多个 .ini 文本文件,其中包含我需要的连接信息。

我正在寻找一种 Powershell 解决方案来搜索所有文件夹并从文件夹名称中提取作业编号,以及从 ini 文件中提取所有连接值。

例如,对于名为 CM8252390-3 的文件夹,作业编号为 8252390-3。这个文件夹里面有3个ini文件。 ini 文件里面是这样的:

[Connection]
Name=IMP_Acme_3.5
[Origin]
X=-15.044784
Y=19.620095
Z=44.621395

所以我的程序需要给我以下结果

Job         Connection
8252390-3   IMP_Acme1_3.5
8252390-3   IMP_Acme2_3.5
8252390-3   IMP_Acme3_3.5
8254260-1   IMP_Acme3_2.4
8254260-1   IMP_Acme3_4.1
...continued for all folders in the network share

寻找一些有关如何执行此操作的 Powershell 示例。我是Powershell新手,所以经验有限。一些Windows/DOS bat文件等方面的经验。

谢谢。

更新:

我做得更进一步。这是我到目前为止所拥有的。它将文件夹名称解析为 $job_num。

Get-ChildItem C:\temp\pwrshell | ForEach-Object -Process {  if ($_.PSIsContainer)
                                                              {
                                                                 if ($_.Name.Substring(0,2) -eq 'CM')
                                                                   {
                                                                     $job_num = $_.Name.Substring(4)
                                                                   }
                                                                 elseif ($_.Name.Substring(0,3) -eq 'Pri' -or 'Hyb' -or 'Had' -or 'Dmo')
                                                                   {
                                                                     $job_num = $_.Name.Substring(5)
                                                                   }
                                                                 $_.Name+' '+$job_num
                                                               }
                                                         }

希望有人能帮忙,并给我读取ini文件的代码。如果他们等我弄清楚,副总裁可能会找到另一个解决方案。

【问题讨论】:

  • 您好,尝试编写一些代码,将其包含在问题中,我们会尽力提供帮助。
  • 我会做的步骤:查找目录、解析名称、获取 INI 文件、获取内容。尝试将其放入脚本中
  • Joe,请删除您的(不完整的)答案,并编辑您的问题以添加您的研究。愿意提供帮助的人会发布答案。
  • sodawillow,我删除了我的两个答案并更新了我的问题。感谢您的指导。希望有人可以帮助我阅读文件。
  • 您可以使用 get-content -path PathToMyFile 获取文件的内容;-)

标签: powershell


【解决方案1】:

这可能会提供您正在寻找的输出:

# // Declare an array to hold job objects
$aJobs = @()

Get-ChildItem C:\temp\pwrshell | ForEach-Object -Process {
    if ($_.PSIsContainer)
    {
        # // Store subfolder path in a variable
        $sFolderPath = $_.FullName

        if ($_.Name.Substring(0,2) -eq 'CM')
        {
            $job_num = $_.Name.Substring(2) # // Edited, value 4 returns incomplete string
        }
        elseif ($_.Name.Substring(0,3) -eq 'Pri' -or 'Hyb' -or 'Had' -or 'Dmo')
        {
            $job_num = $_.Name.Substring(5)
        }
        $_.Name+' '+$job_num

        # // Loop through all files in folder with .ini extension

        Get-ChildItem $sFolderPath | Where {$_.Extension -like '.ini'} | foreach {

            $sFilepath = $_.FullName

            # // Read file contents
            $aFiledata = Get-Content $sFilepath

            # // Loop through file lines, looking for line starting with Name=
            foreach ($sFileline in $aFiledata)
            {
                if ($sFileline.SubString(0,5) -eq 'Name=')
                {
                    $connection_name = $sFileline.SubString(5)

                    # // Create a custom PowerShell object to hold values
                    $JobObject = New-Object PSCustomObject -Property @{
                        "Job"        = $job_num
                        "Connection" = $connection_name
                    }

                    # // Add job object to array
                    $aJobs += $JobObject
                }
            }
        }
    }
}

# // Display output
$aJobs | Format-Table Job,Connection

【讨论】:

  • 德拉森,非常感谢!完美的。正是我需要的。
猜你喜欢
  • 2022-06-10
  • 1970-01-01
  • 2018-04-28
  • 1970-01-01
  • 1970-01-01
  • 2016-07-25
  • 2014-11-10
  • 1970-01-01
  • 2014-03-02
相关资源
最近更新 更多