top of page

IF YOU CAN WRITE THE FLOW YOU CAN DO ANYTHING

May 17
3 min read

- Realizing my former boss's words in PowerShell


I created a simple analysis script in PowerShell to inspect the contents of data involving character encodings. The motivation was a situation where I needed to verify file contents in a human-readable format, and I took the opportunity to try it out as part of my learning.


While the implementation itself was partially generated by AI, I provided the overall flow (the comments in the PowerShell file at the bottom) and the encoding requirements. To ensure a solid understanding, I have re-organized the code by tracing through its contents.


Note: I am not currently in an IT role, but I continue to learn independently.

---

PS

Add-Type -AssemblyName System.Windows.Forms

$dialog = New-Object System.Windows.Forms.OpenFileDialog


PY

import os

dialog = tkinter.filedialog.Open()


VBA

CreateObject

Set dialog = Application.FileDialog(msoFileDialogFilePicker)

---

It is a realization in hindsight, but fundamentally, what any language does is the same: call libraries, create objects, and manipulate them. Because of this, I feel once again that rather than "learning something new," I am simply expressing the same process using different syntax.


It reminds me of what the director at my first company told me: "Just master how to write the flow. If you can do that, you can do anything." I now truly feel that is exactly the case.


A minor difference I noticed is that in PowerShell, you think by connecting processes with pipelines, which feels a bit different from other languages and seems to require some getting used to.


Also, I felt the following descriptions were the core of this project:


$enc = [System.Text.Encoding]::GetEncoding(20290)

$blocks = $blocks | Where-Object { $_.Trim() -ne "" }


The former explicitly specifies the character encoding, and the latter removes unnecessary elements while streaming data through the pipe.

I felt these parts represent the distinct characteristics of PowerShell.


■ Meaning of :: (PowerShell) [System.Text.Encoding]::GetEncoding(20290) 👉 This :: is a symbol for directly accessing a class (type).


■ Simple Understanding

  • Symbol: . (Period)

    • Meaning: Used for objects you have created.

  • Symbol: :: (Double Colon)

    • Meaning: Used to directly access a class.


■ Key Point [System.Text.Encoding]::GetEncoding(20290) 👉 This directly calls the encoding retrieval function without creating an object.


■ Summary in a Nutshell 👉 :: is a "symbol for using functions without creating an instance."

Why don't we use :: for the File Open Dialog?


■ Why the dialog does not use :: A file selection dialog is not just a "function"—it is an object that holds state. Specifically:

  • It holds information about the file selected by the user.

  • It has a display state (whether it is open or closed).

  • The result of the operation changes for each instance.


Because the "content changes every time it is used," it must be generated as an object each time. $dialog = New-Object System.Windows.Forms.OpenFileDialog $dialog.ShowDialog() Therefore, instead of :: for direct class access, we use . for the created object.


👉 The dialog is an "entity with state," so we use . 👉 :: can only be used for "stateless common functions."


If you are in an environment where you cannot run the PowerShell script file directly, you can create a shortcut for it. This way, you can execute the script simply by double-clicking the shortcut.


Shortcut target: C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -NoExit -ExecutionPolicy Bypass -File "C:\ datachcker.ps1"


datachecker.ps1Script↓

----

Add-Type -AssemblyName System.Windows.Forms

 

# -----------------------------

# Select file(All default)

# -----------------------------

$dialog = New-Object System.Windows.Forms.OpenFileDialog

$dialog.Filter = "All files (*.*)|*.*|LAW files (*.dat)|*.dat"

$dialog.FilterIndex = 1

 

if ($dialog.ShowDialog() -ne "OK") {

    exit

}

 

$file = $dialog.FileName

 

# -----------------------------

# input

# -----------------------------

$recNo = [int](Read-Host "Target record")

$range = [int](Read-Host "How many before and after(0 is OK)")

 

# -----------------------------

# Read

# -----------------------------

$bytes = [System.IO.File]::ReadAllBytes($file)

$enc = [System.Text.Encoding]::GetEncoding(20290)

$text = $enc.GetString($bytes)

 

# -----------------------------

# ★ Split by order unit (most important)

# -----------------------------

$blocks = $text -split " (?=A1234567\s+7400\d{3}B5)"

$blocks = $blocks | Where-Object { $_.Trim() -ne "" }

 

# -----------------------------

# Check the number of items

# -----------------------------

Write-Host "Total:" $blocks.Count

 

if ($recNo -lt 1 -or $recNo -gt $blocks.Count) {

    Write-Host "Out of range"

    exit

}

 

# -----------------------------

# Scope (strict)

# -----------------------------

$start = [Math]::Max(0, $recNo - 1 - $range)

$end   = [Math]::Min($blocks.Count - 1, $recNo - 1 + $range)

 

# -----------------------------

# output

# -----------------------------

$result = @()

 

for ($i = $start; $i -le $end; $i++) {

 

    if ($i -eq ($recNo - 1)) {

        $title = ">>> RECORD $($i + 1) <<<"

        Write-Host $title -ForegroundColor Yellow

    } else {

        $title = "--- RECORD $($i + 1) ---"

        Write-Host $title

    }

 

    Write-Host $blocks[$i]

    Write-Host ""

 

    $result += $title

    $result += $blocks[$i]

    $result += ""

}

 

# -----------------------------

# Write the file

# -----------------------------

$outputFile = [System.IO.Path]::Combine(

    [System.IO.Path]::GetDirectoryName($file),

    "result_$recNo.txt"

)

 

$result | Out-File $outputFile -Encoding utf8

----

By the way, I was able to display any desired records before and after a specified record number. It is a shame I cannot show it since the data is confidential company information.

Comments


bottom of page