Get First Character of A_LoopFileName?

I'm trying to parse a filename and get the first character from it as a string to compare it to a previously inputted variable. My code looks like:

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.
} else {
    inputted_letter = %UserInput%
    tooltip %inputted_letter%       ; Show the inputted letter
    sleep, 2000

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

    if files_first_letter != inputted_letter

Right now, it clearly shows in the tooltips the user-entered capital letter, and then the first letter of each file name from within the selected folder, but for some reason when the two look alike, it doesn't recognize them as a match. I'm thinking maybe because technically A_LoopFileName is not of a string type? Or maybe the inputted letter doesn't match the type of the first filename's letter?

I want it to continue if the inputted letter and the first letter of the filename don't match, but if they do, to carry on with the rest of the script. Any ideas on how I can get these two to successfully match? Thanks!

1 answer

  • answered 2020-01-14 02:36 0x464e

    Firstly, AHK doesn't really have types. At least not how you've experienced types in other languages.
    So your assumption about "not being correct type" will pretty much always be wrong.
    So the actual cause is because in a legacy if statement, the syntax is
    if <name of variable> <operator> <legacy way of representing a value>
    So you'd do it like this:
    if files_first_letter != %inputted_letter%
    You we're comparing if the variable files_first_letter is equal to the literal text inputted_letter.

    However, I highly recommend you stop using legacy syntax. It's really just that old. It'll differ horribly much from any other programming language and you run into confusing behavior like this. Expression syntax is what you want to use in AHK nowadays.

    Here's your code snippet converted over to expression syntax in case you're interested:

    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."
        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)

    Also you don't have to be concerned about case with !=, it'll always compare case insensitively.