创建一个热键,以便将我们的工具从定义的地图复制到当前 Windows 11 上的地图中

问题描述 投票:0回答:1

我正在将大部分任务自动化,以尽量减少误差幅度。目前,我正在探索一种方法,例如设置热键(例如 Alt + Ins)以将工具从“C:/Tooling/”复制到我当前正在使用的文件夹。从我迄今为止的研究来看,定义一个结束文件夹似乎是可行的,但不是当前打开的文件夹。

我尝试过自动热键,但似乎一定有比这更简单的东西。

^!c:: ; Ctrl+Alt+C hotkey to trigger the file copy
    ; Replace "C:\Path\To\Your\Folder" with the path of your source folder
    SourceFolder := "C:\test"

    ; Get the current location in Windows Explorer
    if WinActive("ahk_class CabinetWClass") ; Check if Windows Explorer is active
    {
        ; Send Ctrl+L to focus on the address bar
        Send, ^l
        ; Wait for the address bar to be active
        WinWaitActive, ahk_class CabinetWClass
        ; Send Ctrl+C to copy the path
        Send, ^c
        ; Wait for the clipboard to contain the path
        ClipWait

        ; Get the path from the clipboard
        CurrentPath := Clipboard

        ; Construct the destination path
        DestFolder := CurrentPath "\" (FileExist(SourceFolder) ? "" : "test")

        ; Create the destination folder if it doesn't exist
        IfNotExist, %DestFolder%
            FileCreateDir, %DestFolder%

        ; Copy files using FileCopy command
        Loop, %SourceFolder%\*.* ; Loop through all files in the source folder
        {
            FileCopy, %A_LoopFileFullPath%, %DestFolder%
        }

        ; Copy specific folders recursively
        Folders := "Docs|Input|Tools"
        Loop, Parse, Folders, |
        {
            FolderName := A_LoopField
            SourceFolderPath := SourceFolder "\" FolderName
            DestFolderPath := DestFolder "\" FolderName

            ; Check if the source folder exists
            IfExist, %SourceFolderPath%
            {
                ; Create the destination folder if it doesn't exist
                IfNotExist, %DestFolderPath%
                    FileCreateDir, %DestFolderPath%

                ; Copy files and subfolders from the specific folder
                CopyFolderRecursive(SourceFolderPath, DestFolderPath)
            }
        }

        MsgBox, Files and folders copied to %DestFolder%
    }
    else
    {
        MsgBox, Please activate Windows Explorer.
    }
return

CopyFolderRecursive(Source, Dest) {
    Loop, %Source%\*.* ; Loop through all files in the source folder
    {
        FileCopy, %A_LoopFileFullPath%, %Dest%
    }

    Loop, %Source%\*., 2 ; Loop through all subfolders in the source folder
    {
        SubfolderName := A_LoopFileName
        FullSourcePath := Source "\" SubfolderName
        FullDestPath := Dest "\" SubfolderName
        CopyFolderRecursive(FullSourcePath, FullDestPath)
    }
}
autohotkey windows-11
1个回答
0
投票
SourceFolder := "C:\test"
SplitPath, SourceFolder, SourceFolderName  ; Extract only the folder name from its full path.
SpecificFolders := "Docs|Input|Tools"

#IfWinActive ahk_class CabinetWClass ; Explorer

    ^!c:: ; Ctrl+Alt+C hotkey to trigger the Source Folder copy
        If !FileExist(SourceFolder)
        {
            MsgBox,48, Error!, %SourceFolder% `ndoesn't exist
            return
        }
        DestFolder := GetActiveExplorerPath()
        FileCreateDir, %DestFolder%\%SourceFolderName% ; Creates the folder if it doesn't exist
        FileCopyDir, %SourceFolder%, %DestFolder%\%SourceFolderName%, 1 ; Overwrite existing files  
    return  

    ^!d:: ; Ctrl+Alt+D hotkey to trigger the Specific Folders recursive copy
        If !FileExist(SourceFolder)
        {
            MsgBox,48, Error!, %SourceFolder% `ndoesn't exist
            return
        }
        DestFolder := GetActiveExplorerPath()
        Loop, Parse, SpecificFolders, |     
        If FileExist(SourceFolder "\" A_LoopField)
        {
            SourceFolderPath = %SourceFolder%\%A_LoopField%
            DestFolderPath = %DestFolder%\%SourceFolderName%\%A_LoopField%
            FileCreateDir, %DestFolderPath% ; Creates the folder if it doesn't exist    
            CopyFolderRecursive(SourceFolderPath, DestFolderPath)
        }
    return

#IfWinActive


; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
GetActiveExplorerPath()
{
    explorerHwnd := WinActive("ahk_class CabinetWClass")
    if (explorerHwnd)
    {
        for window in ComObjCreate("Shell.Application").Windows
        {
            try if (window.hwnd==explorerHwnd)
            {
                return window.Document.Folder.Self.Path
            }
        }
    }
}

CopyFolderRecursive(Source, Dest) {
    If FileExist(Source) AND FileExist(Dest)
    {
        FileCopyDir, %Source%, %Dest%, 1 ; Overwrite existing files 
        FileCopyDir, %Dest%, %Source%, 1
    }   
}

参见 #IfWinActiveFileCopyDirFileExist()

© www.soinside.com 2019 - 2024. All rights reserved.