anonymous1184

NewFolderFromSelection v2

Mar 26th, 2022 (edited)
1,300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ; Example:
  2. F1::NewFolderFromSelection()
  3.  
  4. ; Put in Lib\NewFolderFromSelection.ahk
  5.  
  6. NewFolderFromSelection()
  7. {
  8.     cwd := files := ""
  9.     hWnd := WinActive("A")
  10.     WinGetClass wClass, % "ahk_id" hWnd
  11.     if (wClass = "CabinetWClass") {
  12.         WinGetText wText, A
  13.         RegExMatch(wText, "Address: \K.+", cwd)
  14.         files := NewFolderFromSelection_Folder(hWnd)
  15.     } else if (wClass ~= "Progman|WorkerW") {
  16.         cwd := A_Desktop
  17.         files := NewFolderFromSelection_Desktop(hWnd)
  18.     } else {
  19.         MsgBox 0x40010, Error, Window not supported.
  20.         return
  21.     }
  22.  
  23.     if (!files.Count())
  24.         return
  25.  
  26.     InputBox newFolder, New folder name:,,, 200, 100
  27.     if (!newFolder)
  28.         return
  29.  
  30.     exist := FileExist(cwd "\" newFolder)
  31.     if (exist ~= "D") {
  32.         MsgBox 0x40024, Continue?, The folder already exists`, move files into it?
  33.         IfMsgBox No
  34.             return
  35.     } else if (exist) {
  36.         MsgBox 0x40010, Error, A file with the given name already exists.
  37.         return
  38.     } else {
  39.         FileCreateDir % cwd "\" newFolder
  40.         if (ErrorLevel) {
  41.             MsgBox 0x40010, Error, Folder couldn't be created.
  42.             return
  43.         }
  44.     }
  45.  
  46.     errors := []
  47.     for _,file in files {
  48.         if (FileExist(file) ~= "D")
  49.             FileMoveDir % file, % cwd "\" newFolder, 1
  50.         else
  51.             FileMove % file, % cwd "\" newFolder
  52.         if (ErrorLevel)
  53.             errors.Push(file)
  54.     }
  55.  
  56.     total := errors.Count()
  57.     if (total) {
  58.         s := total = 1 ? "" : "s"
  59.         msg := "File" s " couldn't be moved:`n`n"
  60.         loop % total
  61.             msg .= "- " errors[A_Index]
  62.         MsgBox 0x40010, % "Error" s, % msg
  63.     }
  64.  
  65. }
  66.  
  67. NewFolderFromSelection_Desktop(hWnd)
  68. {
  69.     ControlGet selection, List, Selected, SysListView321
  70.     files := []
  71.     loop Parse, selection, `n
  72.         files.Push(A_Desktop "\" StrSplit(A_LoopField, "`t")[1])
  73.     return files
  74. }
  75.  
  76. NewFolderFromSelection_Folder(hWnd)
  77. {
  78.     files := [], selected := {}
  79.     for window in ComObjCreate("Shell.Application").Windows {
  80.         if (hWnd != window.hWnd)
  81.             continue
  82.         selected := window.Document.SelectedItems
  83.         break
  84.     }
  85.     for item in selected
  86.         files.Push(item.Path)
  87.     return files
  88. }
  89.  
Add Comment
Please, Sign In to add comment