基于扩展的递归文件循环

时间:2018-07-06 16:03:49

标签: scripting autohotkey

我试图遍历目录,获取以特定扩展名结尾的文件,并将匹配的文件填充到文本框中。但是,我的循环似乎没有执行。这是我想出的:

searchDir = D:\some_directory

^g:: CaptureLoop()

CaptureLoop(){
    Loop, Files, %searchDir%\*.xyz, R ; Recurse, get files with .xyz ext
    {
        MsgBox, loop ; Not reached
        SplitPath, %A_LoopFileLongPath%, file_name, dir, ext, name_no_ext, drive
        MsgBox, %name_no_ext% ; Populating this field is the goal.
    }
}

我确定我的搜索目录包含.xyz文件。我的平台是Windows 7。

一种可能的解决方案

由于某些原因,AHK不能看到我的字符串,除非它们在函数中。不过,这似乎并不是标准行为。如果遇到此问题,请尝试在函数内移动变量:

CaptureLoop(){
    searchDir = D:\some_directory
    Loop, Files, %searchDir%\*.xyz, R ; Recurse, get files with .xyz ext
    { ... }

1 个答案:

答案 0 :(得分:1)

我能看到的唯一原因是

以这种方式作为独立脚本进行尝试:

#NoEnv
#SingleInstance Force

; If the script is not elevated, relaunch as administrator and kill current instance:

full_command_line := DllCall("GetCommandLine", "str")

if not (A_IsAdmin or RegExMatch(full_command_line, " /restart(?!\S)"))
{
    try ; leads to having the script re-launching itself as administrator
    {
        if A_IsCompiled
            Run *RunAs "%A_ScriptFullPath%" /restart
        else
            Run *RunAs "%A_AhkPath%" /restart "%A_ScriptFullPath%"
    }
    ExitApp
}

searchDir := "D:\some_directory"

        RETURN   ; === end of auto-execute section ===

^g:: CaptureLoop()

CaptureLoop(){
    Loop, Files, %searchDir%\*.xyz, R ; Recurse, get files with .xyz ext
    {
        MsgBox, loop
        SplitPath, A_LoopFileLongPath, file_name, dir, ext, name_no_ext, drive
        MsgBox, %name_no_ext% ; Populating this field is the goal.
    }
}