Problem
In many industries, files are named using conventions that embed useful metadata directly in the file name. For example, in e-commerce, logistics, or billing systems, file names might include prices, order numbers, or other key details.
In my case, I have a folder full of PDF files, each named with a format like Hotel-996.35.pdf
, where the last part (996.35
) represents the price of this invoice. Now, I want to calculate the total price of all transactions by summing up the prices embedded in these file names. Doing this manually for a large number of files is time-consuming and is easy to make errors.
By using PowerShell, you can quickly extract prices from file names and calculate the total. This blog post show you a PowerShell script that solves this problem efficiently.
Solution
The following PowerShell script below automates the process of extracting prices from file names and calculating their total.
$directory = Get-Location
$totalPrice = 0
$pdfFiles = Get-ChildItem -Path $directory
foreach ($file in $pdfFiles) {
if ($file.BaseName -match "-([0-9]+(\.[0-9]{1,2})?)") {
Write-Host "Found: " $file.Name
$unitPrice = [decimal]::Parse($matches[1], [Globalization.NumberStyles]::Float)
$totalPrice += $unitPrice
}
}
Write-Host "Total: $totalPrice"
Let’s break down how it works step by step.
1. Working Directory
Get the current working directory using the Get-Location
cmdlet. This is the folder where the script will look for files.
$directory = Get-Location
2. Retrieve Files in the Directory
The Get-ChildItem
cmdlet retrieves all files in the specified directory. By default, it fetches all file types, but you can filter it to only include PDF files by adding a -Filter
parameter (e.g., -Filter *.pdf
).
$pdfFiles = Get-ChildItem -Path $directory
3. Extract the Price Using Regex
The -match
operator is used to apply a regular expression (regex) to the file's base name (the file name without extension). The regex pattern -([0-9]+(\.[0-9]{1,2})?)
is designed to match a hyphen (-
) followed by a number.
[0-9]+
: Matches one or more digits (e.g.,996
).(\.[0-9]{1,2})?
: Optionally matches a decimal point followed by 1 or 2 digits (e.g.,.35
).- The entire pattern is enclosed in parentheses to capture the matched number for later use.
If a match is found, the matched value is stored in the $matches
automatic variable.
$file.BaseName -match "-([0-9]+(\.[0-9]{1,2})?)"
4. Convert the Price to Decimal
The matched price (stored in $matches[1]
) is converted to a decimal number using the [decimal]::Parse()
method.
$unitPrice = [decimal]::Parse($matches[1], [Globalization.NumberStyles]::Float)
5. Add Total Price
$totalPrice += $unitPrice
Conclusion
This PowerShell script provides a simple yet powerful solution to a common problem: extracting numeric data from file names and performing calculations. You can modify it to make it works for your scenarios!
Comments