AutoHotkey Homepage AutoHotkey Community
Let's help each other out
 
 FAQFAQ   SearchSearch   MemberlistMemberlist   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Explorer Extensions: create, duplicate, rename

 
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions
View previous topic :: View next topic  
Author Message
PhiLho



Joined: 27 Dec 2005
Posts: 6836
Location: France (near Paris)

PostPosted: Tue Jul 10, 2007 9:58 am    Post subject: Explorer Extensions: create, duplicate, rename Reply with quote

I am limited by the subject's length... It should be: create folder, duplicate file or folder, or rename files.

How does it work?
The hotkey (I chose F12 and Shift+F12) is active only in Windows Explorer.
I chose simplicity, and detect the current path by reading the Edit1 control of Explorer (the address bar), it might fail in some cases. If you have the address bar of Explorer hidden, you might need to add DetectHiddenWindows On at the top of the script.
Or just activate it, it is so convenient!

I get the first selected item in the ListView of Explorer.
If no file nor folder is selected, it shows a dialog inputting a folder name, and creates it upon validation. Faster than using a slow menu...
If a folder is selected, for F12 (and Shift+F12) it shows a dialog proposing to duplicate the folder (the whole hierarchy) defaulting to the folder name.
If a file is selected, for F12 it shows a dialog proposing to duplicate the file, defaulting to the file name, separating name and extension to ease separate editing (tab to go to the extension field). And for Shift+F12, it shows a similar dialog to rename the file.
When duplicating, you can type * (in name or extension) to increment the name: if the file is foo.txt, it creates foo1.txt. If it is foo11.txt, it creates foo12.txt. If the destination name exists, it just increments again.

Code:
SetTitleMatchMode RegEx
#NoEnv

;----------------------------------------------------------------
; Explorer Extensions
; The Explorer class below may change depending on system
; and the way the window is created.

#IfWinActive ahk_class ExploreWClass|CabinetWClass

;--- F12: in Explorer, duplicate current file or duplicate or create new folder
;--- Shift+F12: in Explorer, rename current file
F12::
+F12::
   ; Get current path in Explorer
   explorerHwnd := WinExist("A")
   ControlGetText currentPath, Edit1, ahk_id %explorerHwnd%

   ; GUI constants
   width = 300
   Gui 14:+LabelEEGui   ; hexa E = 14...
   appName = Explorer Extensions

   ; Get current (selected/with focus) filename in Windows Explorer
   ; If several files/folders are selected, the returned one is semi-random...
   selectedItem := GetWindowsExplorerSelectedFile(explorerHwnd)
   If (selectedItem == "")
   {
      ; No file nor folder selected, create a new folder in the current dir
      bCreateFolder := true
      Goto DuplicateOrCreateFolder
   }
   If InStr(FileExist(currentPath . "\" . selectedItem), "D")
   {
      ; That's a dir
      bCreateFolder := false
      Goto DuplicateOrCreateFolder
   }
   bDuplicateFile := (A_ThisHotkey = "F12")
   Goto DuplicateOrRenameFile

#IfWinActive

EEGuiCancel:
EEGuiClose:
EEGuiEscape:
   Gui 14:Destroy
Return

DuplicateOrCreateFolder:
   Gui 14:Add, Text, , %currentPath%
   Gui 14:Font, c400080 s12 bold
   If bCreateFolder
      Gui 14:Add, Text, , Create Folder
   Else
      Gui 14:Add, Text, , Duplicate Folder
   Gui 14:Font
   Gui 14:Add, Edit, w%width% vfolderName, %selectedItem%
   Gui 14:Add, Button, w50 xm+100 gEEProcessFolder Default, OK
   Gui 14:Add, Button, w50 x+20 gEEGuiCancel, Cancel
   Gui 14:Show, , %appName%
   Return

EEProcessFolder:
   Gui 14:Submit
   Gui 14:Destroy
   IfExist %currentPath%\%folderName%
   {
      Send {F5}
      MsgBox 16, %appName%, A folder or file with this name already exists!
      Return
   }
   If bCreateFolder
   {
      FileCreateDir %currentPath%\%folderName%
   }
   Else
   {
      FileCopyDir %currentPath%\%selectedItem%, %currentPath%\%folderName%
   }
Return

DuplicateOrRenameFile:
   SplitPath selectedItem, , , siExt, siNameNE
   Gui 14:Add, Text, , %currentPath%
   Gui 14:Font, c400080 s12 bold
   If bDuplicateFile
      Gui 14:Add, Text, , Duplicate File
   Else
      Gui 14:Add, Text, , Rename File
   Gui 14:Font
   Gui 14:Add, Edit, w%width% vnameNoExt, %siNameNE%
   Gui 14:Add, Edit, w100 x+10 vext, %siExt%
   If bDuplicateFile
      Gui 14:Add, Text, xm, Type * to increment file name
   Gui 14:Add, Button, w50 xm+130 y+20 gEEProcessFile Default, OK
   Gui 14:Add, Button, w50 x+20 gEEGuiCancel, Cancel
   Gui 14:Show, , %appName%
   Return

EEProcessFile:
   Gui 14:Submit
   Gui 14:Destroy
   If (nameNoExt = "*" or ext = "*")
   {
      nameNoExt := siNameNE
      Loop
      {
         IfExist %currentPath%\%nameNoExt%.%ext%   ; First time is always true...
         {
            nameNoExt := IncrementName(nameNoExt)
         }
         Else
         {
            Break
         }
      }
   }
   newName = %nameNoExt%.%ext%
   IfExist %currentPath%\%newName%
   {
      Send {F5}
      MsgBox 16, %appName%, A folder or file with this name (%newName%) already exists!
      Return
   }
   If bDuplicateFile
      FileCopy %currentPath%\%selectedItem%, %currentPath%\%newName%
   Else
      FileMove %currentPath%\%selectedItem%, %currentPath%\%newName%
Return

GetWindowsExplorerSelectedFile(_hWnd)
{
   local selectedFiles, file

   ; I can send ^C and parse Clipboard, but this way don't mess with clipboard at all, seems nicer.
   ; Warning: with this, you get only what is displayed in Explorer!
   ; If you kept the default Windows setting of not displaying file extensions (bad idea...),
   ; you will get partial file names...
   ControlGet, selectedFiles, List, Selected Col1, SysListView321, ahk_id %_hWnd%
   Loop, Parse, selectedFiles, `n  ; Rows are delimited by linefeeds (`n).
   {
      If (A_Index = 1)
      {
         file := A_LoopField
      }
      Else
      {
         ; Indicate that several files are selected, we return only the first one
         ; but count the total number of selected files, to indicate we return a partial result
         ErrorLevel := A_Index
      }
   }
   Return file
}

IncrementName(_nameNoExt)
{
   local d, dd

   Loop
   {
      ; Take out rightmost char (digit?)
      StringRight d, _nameNoExt, 1
      StringTrimRight _nameNoExt, _nameNoExt, 1
      If d = 9
      {
         dd = 0%dd%
      }
      Else If d between 0 and 8
      {
         dd := (d + 1) . dd
         Break
      }
      Else
      {
         ; Not a digit: put back non digit char, add a 1
         _nameNoExt = %_nameNoExt%%d%1
         Break
      }
   }
   Return _nameNoExt . dd
}

I use it daily for some months now, it is a great time saver! I am annoyed when working on a computer without it... Smile
The rename facility has been added just now, so it might be buggy...
_________________
vPhiLho := RegExReplace("Philippe Lhoste", "^(\w{3})\w*\s+\b(\w{3})\w*$", "$1$2")
Back to top
View user's profile Send private message Visit poster's website
looky



Joined: 28 Aug 2009
Posts: 4

PostPosted: Fri Dec 04, 2009 4:53 am    Post subject: Re: Explorer Extensions: create, duplicate, rename Reply with quote

thank u for your script.
After creating folder, How can I go focus to the created folder?
_________________
^___^
Back to top
View user's profile Send private message
emmanuel d



Joined: 29 Jan 2009
Posts: 436
Location: Belgium

PostPosted: Sun Mar 07, 2010 1:50 pm    Post subject: Reply with quote

I use this to get a file in explorer:
Code:
#singleinstance force
GroupAdd, Explore, ahk_class CabinetWClass
GroupAdd, Explore, ahk_class ExploreWClass
return

#IfWinActive, ahk_group Explore
F9::
MsgBox,% ExplorerGetFileName()
return

ExplorerGetFileName() {
   ControlGet, Pad,Line,1, Edit1,A   ; Get the pad
   ControlGet, Selected, List, Selected Col1, SysListView321, A   ; Get the file, Foldername
   Return,% Pad "\" Selected   ; Return the full pad and name of the selected item
}


KIS (Keep it Simple) is the key to me, it wil reduce the code.
_________________
Stopwatch
emdkplayer
http://www.autohotkey.com/forum/viewtopic.php?p=306819

the code i post falls under the:
WTFYW license
, wich meens its free to use
Back to top
View user's profile Send private message
fragman



Joined: 13 Oct 2009
Posts: 1194

PostPosted: Mon Mar 08, 2010 11:59 am    Post subject: Reply with quote

This won't work on Vista and higher though.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    AutoHotkey Community Forum Index -> Scripts & Functions All times are GMT
Page 1 of 1

 
Jump to:  
You can post new topics in this forum
You can reply to topics in this forum


Powered by phpBB © 2001, 2005 phpBB Group