获取 A_LoopFileName 的第一个字符?

Get First Character of A_LoopFileName?

我正在尝试解析文件名并从中获取第一个字符作为字符串,以便将其与先前输入的变量进行比较。我的代码如下所示:

FileSelectFolder, WhichFolder  ; Ask the user to pick a folder.

; Ask what letter you want to start the loop from
InputBox, UserInput, Start At What Letter?, Please enter a letter to start at within the folder (CAPITALIZE IT!)., , 450, 150

if ErrorLevel {
    MsgBox, CANCEL was pressed.
    ExitApp
} else {
    inputted_letter = %UserInput%
    tooltip %inputted_letter%       ; Show the inputted letter
    sleep, 2000
    tooltip
}

Loop, %WhichFolder%\*.*
{

    current_filename_full = %A_LoopFileName%

    files_first_letter := SubStr(current_filename_full, 1, 1)
    tooltip %files_first_letter%             ; Show the file's first letter
    sleep, 2000
    tooltip

    if files_first_letter != inputted_letter
        continue
...

现在,它在工具提示中清楚地显示了用户输入的大写字母,然后是所选文件夹中每个文件名的第一个字母,但由于某些原因,当两者看起来很相似时,它并不将它们识别为匹配项。我在想也许是因为技术上 A_LoopFileName 不是字符串类型?或者输入的字母与第一个文件名字母的类型不匹配?

如果输入的字母和文件名的第一个字母不匹配,我希望它 continue,但如果匹配,则继续执行脚本的其余部分。关于如何使这两者成功匹配的任何想法?谢谢!

首先,AHK 并没有真正的类型。至少不是您在其他语言中体验过的类型。
所以你对 "not being correct type" 的假设几乎总是错误的。
所以实际原因是因为在 legacy if 语句中,语法是
if <name of variable> <operator> <legacy way of representing a value>
所以你会这样做:
if files_first_letter != %inputted_letter%
您正在比较变量 files_first_letter 是否等于文字文本 inputted_letter.

但是,我强烈建议您停止使用旧语法。真的就这么老了。 它与任何其他编程语言都大不相同,您 运行 会陷入像这样令人困惑的行为。表达式语法是你现在想在 AHK 中使用的语法。

如果您感兴趣,这是转换为表达式语法的代码片段:

FileSelectFolder, WhichFolder

;Forcing an expression like this with % in every parameter 
;is really not needed of course, and could be considered
;excessive, but I'm doing it for demonstrational
;purposes here. Putting everything in expression syntax.
;also, not gonna lie, I always do it myself haha
InputBox, UserInput, % "Start At What Letter?", % "Please enter a letter to start at within the folder (CAPITALIZE IT!).", , 450, 150

if (ErrorLevel) 
;braces indicate an expression and the non-legacy if statement
;more about this, as an expression, ErrorLevel here holds the value
;1, which gets evaluated  to true, so we're doing 
;if (true), which is true
{
    MsgBox, % "CANCEL was pressed."
    ExitApp
} 
else 
    inputted_letter := UserInput ; = is never used, always :=


Loop, Files, % WhichFolder "\*.*" 
;non-legacy file loop
;note that here forcing the expression statement
;with % is actually very much needed
{

    current_filename_full := A_LoopFileName
    files_first_letter := SubStr(current_filename_full, 1, 1)

    if (files_first_letter != inputted_letter)
        continue
}

此外,您不必担心 != 的大小写,它总是会不区分大小写地进行比较。