Powershell脚本无法运行:按买入额归类扑克手账文件求助
扑克手账按买入额归类的PowerShell脚本问题及修复
问题背景
需将同一文件夹内的扑克手账.txt文件按买入额(BUY IN)归类到对应文件夹,ChatGPT生成的脚本无法运行(已启用PowerShell脚本执行权限),且希望仅读取文件必要内容获取买入额,无需读取完整文件。
扑克手账示例
以下是PokerStars格式的手账示例:
PokerStars Hand #212603518891: Tournament #2869138653, $30+$3 USD Hold'em No Limit - Level I (15/30) - 2020/04/24 1:00:02 ET Table '2869138653 1' 9-max Seat #3 is the button Seat 3: klutke21 (5000 in chips) Seat 4: Rochinha2011 (5000 in chips) Seat 5: Septenzo (5000 in chips) Seat 6: Djuk7 (5000 in chips) Seat 7: Royal Krab (5000 in chips) Seat 8: katuwo (5000 in chips) Seat 9: Coronavirus101 (5000 in chips) klutke21: posts the ante 4 Rochinha2011: posts the ante 4 Septenzo: posts the ante 4 Djuk7: posts the ante 4 Royal Krab: posts the ante 4 katuwo: posts the ante 4 Coronavirus101: posts the ante 4 Rochinha2011: posts small blind 15 Septenzo: posts big blind 30 *** HOLE CARDS *** Djuk7: folds Royal Krab: folds katuwo: raises 60 to 90 Coronavirus101: folds klutke21: calls 90 Rochinha2011: folds Septenzo: folds *** FLOP *** [4d 2h 6s] katuwo: bets 90 klutke21: calls 90 *** TURN *** [4d 2h 6s] [5h] katuwo: bets 120 klutke21: calls 120 *** RIVER *** [4d 2h 6s 5h] [Kh] katuwo: checks klutke21: bets 390 katuwo: calls 390 *** SHOW DOWN *** klutke21: shows [Ac Td] (high card Ace) katuwo: shows [Ts Th] (a pair of Tens) katuwo collected 1453 from pot *** SUMMARY *** Total pot 1453 | Rake 0 Board [4d 2h 6s 5h Kh] Seat 3: klutke21 (button) showed [Ac Td] and lost with high card Ace Seat 4: Rochinha2011 (small blind) folded before Flop Seat 5: Septenzo (big blind) folded before Flop Seat 6: Djuk7 folded before Flop (didn't bet) Seat 7: Royal Krab folded before Flop (didn't bet) Seat 8: katuwo showed [Ts Th] and won (1453) with a pair of Tens Seat 9: Coronavirus101 folded before Flop (didn't bet)
原脚本问题分析
原脚本存在多个关键问题:
- 读取完整文件内容,效率低下,不符合仅读取必要内容的需求
- 正则表达式
\$([\d.]+) USD无法正确匹配$30+$3这类带附加费用的买入格式 - 提取买入额时用
-replace '\D', ''会将30+3转换为303,丢失买入结构信息 - 文件夹路径拼接错误,
Join-Path后手动添加\$导致路径格式异常
修复后的脚本
# 目标文件夹路径 $targetDir = "D:\HAND HISTORY\POKERSTARS\HH DEALER\2020\4\2020 - 04" # 遍历所有txt文件 Get-ChildItem -Path $targetDir -Filter *.txt | ForEach-Object { Write-Host "处理文件: $($_.Name)" $filePath = $_.FullName # 仅读取文件第一行(买入额固定在首行) $firstLine = Get-Content -Path $filePath -TotalCount 1 # 匹配买入额格式,支持$X+$Y USD或$X USD if ($firstLine -match '\$([\d+]+) USD') { $buyInValue = $matches[1] # 创建买入额对应的文件夹(保留原始格式,比如$30+$3) $buyInFolder = Join-Path -Path $targetDir -ChildPath "$buyInValue" # 检查文件夹是否存在,不存在则创建 if (-not (Test-Path -Path $buyInFolder -PathType Container)) { Write-Host "创建文件夹: $buyInFolder" New-Item -Path $buyInFolder -ItemType Directory | Out-Null } # 移动文件到目标文件夹 $destPath = Join-Path -Path $buyInFolder -ChildPath $_.Name Write-Host "移动文件到: $destPath" Move-Item -Path $filePath -Destination $destPath -Force } else { Write-Host "未在文件$($_.Name)中找到买入额信息" } }
修复说明
- 仅读取首行:使用
Get-Content -TotalCount 1只读取文件第一行,大幅提升大文件处理速度,避免冗余内容读取 - 修正正则匹配:调整正则为
\$([\d+]+) USD,可正确捕获$30+$3或$10等多种买入格式 - 保留买入额格式:直接使用匹配到的原始字符串作为文件夹名称,避免数值转换丢失结构信息
- 路径拼接正确:依靠
Join-Path自动处理路径分隔符,避免手动添加符号导致的路径错误 - 添加异常提示:对未找到买入额的文件给出明确提示,便于排查问题
内容的提问来源于stack exchange,提问作者virtudrama
相关产品推荐
相关产品推荐

