PoshCode Archive  Artifact [e16520f30e]

Artifact e16520f30e487f70eeb95f6a91e332e6e881d45c07bbcebd5e652d5ea296c564:

  • File which-where.ps1 — part of check-in [d03f051cb9] at 2018-06-10 13:42:34 on branch trunk — This demo looks for files in PATH variable (extensions are from PATHEXT) and presents path for a file in two ways: full path (which mode) and only path for a file (where mode). For example, enter in TextBox field something like ‘notepad’ and click ‘Search’. (user: greg zakharov size: 2980)

# encoding: ascii
# api: powershell
# title: which\where
# description: This demo looks for files in PATH variable (extensions are from PATHEXT) and presents path for a file in two ways: full path (which mode) and only path for a file (where mode). For example, enter in TextBox field something like ‘notepad’ and click ‘Search’.
# version: 0.1
# type: function
# author: greg zakharov
# license: CC0
# function: Search-WithMode
# x-poshcode-id: 4528
# x-archived: 2013-10-21T07:29:09
# x-published: 2013-10-18T14:41:00
#
#
function Search-WithMode {
  if (-not [String]::IsNullOrEmpty($txtFile.Text)) {
    foreach ($p in ($env:path -split ';')) {
      foreach ($e in ($env:pathext -split ';')) {
        $mat = $p + '\' + $txtFile.Text + $e.ToLower()
        if (Test-Path $mat) {
          if ($rbWhich.Checked) {$lstView.Items.Add($mat)}
          elseif ($rbWhere.Checked) {$lstView.Items.Add((Split-Path $mat))}
        }
      }
    }
  }
}

function frmMain_Show {
  Add-Type -AssemblyName System.Windows.Forms
  [Windows.Forms.Application]::EnableVisualStyles()
  
  $ico = [Drawing.Icon]::ExtractAssociatedIcon(($PSHome + '\powershell.exe'))
  
  $frmMain = New-Object Windows.Forms.Form
  $txtFile = New-Object Windows.Forms.TextBox
  $btnFind = New-Object Windows.Forms.Button
  $rbWhich = New-Object Windows.Forms.RadioButton
  $rbWhere = New-Object Windows.Forms.RadioButton
  $lstView = New-Object Windows.Forms.ListView
  $chFound = New-Object Windows.Forms.ColumnHeader
  #
  #txtFile
  #
  $txtFile.Anchor = "Left, Top, Right"
  $txtFile.Location = New-Object Drawing.Point(13, 13)
  $txtFile.Size = New-Object Drawing.Size(400, 27)
  #
  #btnFind
  #
  $btnFind.Anchor = "Top, Right"
  $btnFind.Location = New-Object Drawing.Point(423, 13)
  $btnFind.Size = New-Object Drawing.Size(90, 22)
  $btnFind.Text = "Search"
  $btnFind.Add_Click({Search-WithMode})
  #
  #rbWhich
  #
  $rbWhich.Anchor = "Left, Top"
  $rbWhich.Checked = $true
  $rbWhich.Location = New-Object Drawing.Point(13, 33)
  $rbWhich.Text = "Which mode"
  #
  #rbWhere
  #
  $rbWhere.Anchor = "Left, Top"
  $rbWhere.Location = New-Object Drawing.Point(123, 33)
  $rbWhere.Text = "Where mode"
  #
  #lstView
  #
  $lstView.Anchor = "Left, Bottom, Right, Top"
  $lstView.Columns.AddRange(@($chFound))
  $lstView.FullRowSelect = $true
  $lstView.Location = New-Object Drawing.Point(13, 57)
  $lstView.Size = New-Object Drawing.Size(500, 237)
  $lstView.View = "Details"
  #
  #chFound
  #
  $chFound.Text = "Match(es) history"
  $chFound.Width = 495
  #
  #frmMain
  #
  $frmMain.ClientSize = New-Object Drawing.Size(527, 312)
  $frmMain.Controls.AddRange(@($txtFile, $btnFind, $rbWhich, $rbWhere, $lstView))
  $frmMain.FormBorderStyle = "FixedSingle"
  $frmMain.Icon = $ico
  $frmMain.StartPosition = "CenterScreen"
  $frmMain.Text = "Which"
  
  [void]$frmMain.ShowDialog()
}

frmMain_Show