You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于AutoHotkey实现文件指定行提取与变量拆分

Solution for Your AutoHotkey String Search & Split Task

Got it, let's figure out how to make this AutoHotkey script work exactly how you need it. The key here isn't just checking if the variable %x% exists in the file—it's finding lines that follow your specific [x] - [y] format and pulling out that %y% value cleanly.

Here's a Complete Working Script

; Set your target variable and file path first
x := "word"
targetFile := "C:\Path\To\Your\File.txt"
y := "" ; Initialize y to empty

; Read the entire file content (great for smaller files)
FileRead, fileContent, %targetFile%
if ErrorLevel {
    MsgBox, Oops, couldn't read the file! Check the path.
    return
}

; Loop through each line of the file
Loop, Parse, fileContent, `n, `r
{
    currentLine := A_LoopField
    ; Make sure the line contains x AND follows the "x - y" format
    if InStr(currentLine, x " - ") {
        ; Find the position of the " - " separator
        splitPoint := InStr(currentLine, " - ")
        ; Grab everything after the separator for y
        y := SubStr(currentLine, splitPoint + 3) ; +3 skips " - " (space + dash + space)
        MsgBox, Found your match!`n`nx: %x%`ny: %y%
        break ; Remove this if you want to find ALL matching lines instead of just the first
    }
}

; If no matching line was found
if (y = "") {
    MsgBox, No line with "%x%" in the "x - y" format was found.
}

For Large Files (Memory-Friendly Version)

If your target file is huge and reading the whole thing at once would eat up too much memory, use this line-by-line reading approach instead:

x := "word"
targetFile := "C:\Path\To\Your\LargeFile.txt"
y := ""

; Open the file in read mode
fileHandle := FileOpen(targetFile, "r")
if !fileHandle {
    MsgBox, Failed to open the file. Double-check the path!
    return
}

; Read line by line until end of file
while !fileHandle.AtEOF {
    currentLine := fileHandle.ReadLine()
    if InStr(currentLine, x " - ") {
        splitPoint := InStr(currentLine, " - ")
        y := SubStr(currentLine, splitPoint + 3)
        MsgBox, Found your match!`n`nx: %x%`ny: %y%
        break ; Remove to find all matches
    }
}
fileHandle.Close()

if (y = "") {
    MsgBox, No matching line found.
}

Why Your Previous Reference Code Didn't Work

The "Determine if string exists in file" code you looked at was only designed to check if a string is present somewhere in the file. It doesn't account for your specific line format, nor does it handle splitting the line to extract %y%. The scripts above fix that by:

  • Checking for the exact x - pattern to avoid false matches (like lines where x appears elsewhere without the separator)
  • Precisely splitting the line at the - marker to pull out the y value
  • Giving you options for small vs. large files

内容的提问来源于stack exchange,提问作者user6382336

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:53:47