Script to copy selected file names / create folder / copy file to folder Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Script to copy selected file names / create folder / copy file to folder

Post by siomosp » 28 Jan 2022, 15:34

At this post
viewtopic.php?f=76&t=60237 the code is working fine for files selected one by one
Any one knows how to adapt it for working for more than on selected files?

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Script to copy selected file names / create folder / copy file to folder

Post by Smile_ » 28 Jan 2022, 16:17

Press F1

Code: Select all

Gui, -MinimizeBox
Gui, Add, Edit, w400 vCopy2 Center, %A_ScriptDir%\TestFolder
Gui, Show,, CopyFiles
Return

GuiClose:
ExitApp

F1::
Gui, Submit, NoHide

If (!Copy2) {
    MsgBox, 16, NoDest, No Destination!
} Else {
    FileCreateDir, %Copy2%
    If (ErrorLevel) {
        MsgBox, 16, FolderCreationError, Code: %ErrorLevel%
    } Else {
        If (!List := Explorer_GetSelection()) {
            MsgBox, 64, Info, No selection
        } Else {
            Loop, Parse, % List, `n, `r
            {
                If InStr(FileExist(A_LoopField), "D") {
                    MsgBox, 64, Info, %A_LoopField%`n`nfolders are not included
                } Else {
                    FileCopy, % A_LoopField, % Copy2
                    If (ErrorLevel) {
                        MsgBox, 16, CopyingError, Code: %ErrorLevel%
                    }
                }
            }
        }
    }
}
Return

Explorer_GetSelection() { ; https://www.autohotkey.com/boards/viewtopic.php?p=255256#p255256
   WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
   if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
      Return
   shellWindows := ComObjCreate("Shell.Application").Windows
   if (winClass ~= "Progman|WorkerW")
      shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
   else {
      for window in shellWindows
         if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
   }
   for item in shellFolderView.SelectedItems
      result .= (result = "" ? "" : "`n") . item.Path
   if !result
      result := shellFolderView.Folder.Self.Path
   Return result
}

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Script to copy selected file names / create folder / copy file to folder

Post by siomosp » 28 Jan 2022, 16:49

Hi,
thank you for the code.
I am not sure how to use it :oops:
I was expected something like this:
Run script
Press the Hotkey ( e.g F1::)
Select 2 or more files in a folder
A folder with the same name of first selected file is created.
This first file is moved inside the folder (named as the first file)
A folder with the same name of second selected file is created.
The second file is moved inside the folder (named as the second file)
and so on with the rest selected files

The script at viewtopic.php?f=76&t=60237
is doing what i need , but i have to select every file one by one
Thank you!

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Script to copy selected file names / create folder / copy file to folder  Topic is solved

Post by Smile_ » 28 Jan 2022, 17:10

Well Well try this

Code: Select all

Gui, -MinimizeBox
Gui, Add, Text, w400 r6 Center Border, % "`n1 - Select the files."
                                              . "`n2 - Press F1 to apply."
                                              . "`n3 - Wait for a message saying it is done."
                                              . "`n4 - Done."
Gui, Show,, CopyFiles
Return

Esc::
GuiClose:
ExitApp

F1::
    Cnt := 0
    Loop, Parse, % Explorer_GetSelection(), `n, `r
    {
        If InStr(FileExist(A_LoopField), "D") {
            MsgBox, 64, Info, %A_LoopField%`n`nfolders are not included
        } Else {
            SplitPath, % A_LoopField, OutName, OutDir,, OutNameNoExt
            ToolTip, Processing %OutName%
            FileCreateDir, % OutDir "\" OutNameNoExt
            If (ErrorLevel) {
                MsgBox, 16, FolderCreationError, Code: %ErrorLevel%
            } Else {
                FileCopy, % A_LoopField, % OutDir "\" OutNameNoExt
                If (ErrorLevel) {
                    MsgBox, 16, CopyingError, Code: %ErrorLevel%
                }
                (!Cnt) ? Cnt := 1
            }
        }
    }
    ToolTip
    If (Cnt)
        MsgBox, 64, Done, Done.
Return

Explorer_GetSelection() { ; https://www.autohotkey.com/boards/viewtopic.php?p=255256#p255256
    ToolTip, Getting selection %OutName%
    WinGetClass, winClass, % "ahk_id" . hWnd := WinExist("A")
    if !(winClass ~="Progman|WorkerW|(Cabinet|Explore)WClass")
        Return
    shellWindows := ComObjCreate("Shell.Application").Windows
    if (winClass ~= "Progman|WorkerW")
        shellFolderView := shellWindows.FindWindowSW(0, 0, SWC_DESKTOP := 8, 0, SWFO_NEEDDISPATCH := 1).Document
    else {
        for window in shellWindows
            if (hWnd = window.HWND) && (shellFolderView := window.Document)
            break
    }
    for item in shellFolderView.SelectedItems
        result .= (result = "" ? "" : "`n") . item.Path
Return result
}
Edit: I modified it a little.

siomosp
Posts: 27
Joined: 18 Jan 2022, 10:21

Re: Script to copy selected file names / create folder / copy file to folder

Post by siomosp » 29 Jan 2022, 04:22

Hi, excellent !!
The gui generated how to guide is helpful, and the rest code works as expected!
(Changing , FileCopy to FileMove at line 26 is better for my needs)
Thank you!! :) :) :)

User avatar
Smile_
Posts: 857
Joined: 03 May 2020, 00:51

Re: Script to copy selected file names / create folder / copy file to folder

Post by Smile_ » 29 Jan 2022, 04:32

Gud to know! :thumbup:, have fun
You can also add progress bar if you have a lot of files to process.

Post Reply

Return to “Ask for Help (v1)”