Autohotkey 获取文件名的第一个字符?

Autohotkey 获取文件名的第一个字符?,autohotkey,Autohotkey,我试图解析一个文件名,并从中获取第一个字符作为字符串,将其与以前输入的变量进行比较。我的代码如下所示: 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 withi

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

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
不是字符串类型?或者输入的字母与第一个文件名字母的类型不匹配


如果输入的字母和文件名的第一个字母不匹配,我希望它
继续
,但如果匹配,则继续执行脚本的其余部分。有没有办法让这两个人成功匹配?谢谢

首先,AHK没有真正的类型。至少不是你在其他语言中体验过的类型。
所以你关于“不是正确类型”的假设几乎总是错误的。
因此,实际原因是因为在遗留的if语句中,语法是
如果

所以你会这样做:
如果文件是第一个字母!=%输入字母%

我们正在比较变量
files\u first\u letter
是否等于文本
inputed\u 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
}

另外,您不必担心
的情况=,它总是会不敏感地比较大小写。

感谢您的精彩解释,我从中学到了一些新东西。我会在今晚的工作中做这个项目,所以我会尝试一下,然后我会确认答案。再次感谢你,工作很有魅力。非常感谢!我自己可能很难弄明白,现在我知道了正确的语法。