Advertisement
jcunews

CopyToNewFolder.ahk

Jun 9th, 2021 (edited)
2,114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ;context: https://www.reddit.com/r/AutoHotkey/comments/nvdyil/need_help/
  2.  
  3. sa:= comobjcreate("shell.application")
  4.  
  5. +f12::
  6. dirname:= "new destination folder name without path"
  7. wingetclass cn, a
  8. if (cn == "CabinetWClass") {
  9.   winget hw, id, a
  10.   ws:= sa.windows()
  11.   loop % ws.count {
  12.     wd:= ws.item(a_index - 1)
  13.     if (wd.hwnd == hw) {
  14.       dc:= wd.document
  15.       if (substr(comobjtype(dc, "name"), 1, 12) == "IShellFolder") {
  16.         si:= dc.selecteditems
  17.         if (si.count > 0) {
  18.           sd:= dc.folder
  19.           dir:= sd.self.path
  20.           if (substr(dir, 1, 1) != ":") {
  21.             tp:= dir "\" dirname
  22.             if (fileexist(tp) == "") {
  23.               filecreatedir %tp%
  24.               if (errorlevel != 0) {
  25.                 msgbox 16, Copy To New Folder
  26.                   , % "Failed to create new folder. " errMsg()
  27.                 return
  28.               }
  29.             }
  30.             ig:= false
  31.             loop % si.count {
  32.               it:= si.item(a_index - 1)
  33.               sp:= it.path
  34.               if (instr(fileexist(sp), "D") > 0) {
  35.                 filemovedir %sp%, %tp%, 1
  36.               } else {
  37.                 filemove %sp%, %tp%, 1
  38.               }
  39.               if ((errorlevel != 0) && !ig) {
  40.                 msgbox 19, Copy To New Folder
  41.                   , % "Failed to move below file/folder. " errMsg() "`n`n"
  42.                   . sp "`n`nDo you want to ignore further errors?"
  43.                 ifmsgbox Yes
  44.                   ig:= true
  45.                 ifmsgbox Cancel
  46.                   return
  47.               }
  48.             }
  49.           } else {
  50.             msgbox 16, Copy To New Folder, This is not a file system folder.
  51.           }
  52.         } else {
  53.           msgbox 16, Copy To New Folder, No file/folder is selected.
  54.         }
  55.       }
  56.       return
  57.     }
  58.   }
  59. }
  60. return
  61.  
  62. errMsg() {
  63.   varsetcapacity(s, 4096)
  64.   dllcall("FormatMessage", int, 0x1000, int, 0, int, a_lasterror, int, 0
  65.     , str, s, int, 4096, str, "")
  66.   return strreplace(s, "`r`n", " ")
  67. }
  68.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement