Help me write a script to group files in windows.
July 30, 2010 7:17 AM   Subscribe

Help me write a script to group some selected files in windows.

I would like to have a script that, when I press a keyboard shortcut, it moves some selected files in windows explorer to a new folder in the same path. I'm using windows 7 (bonus points if it can work on XP too).

I've tried with Autohotkey ("Send !^n", then "Send ^x" and "Send ^v") but it doesn't always work, i.e. sometimes it copies the files before the new folder is created. I could set it to wait, say, 1secs before copying but that wouldn't be very efficient. Clearly, the script has to be faster than typing the keystrokes myself.
I know there are other tools (Windows Script Host and Powershell), but before learning them I would like to figure out if they can do what I need.
posted by volpe to Computers & Internet (6 answers total) 2 users marked this as a favorite
You should be using AutoHotKey to create the folder, not having AutoHotKey tell Explorer to do it (as you have noticed, it is hard to predict when Explorer will get around to doing something you have asked it to do). The commands you want are FileCreateDir and FileMove, along with some way to get the path of the current Explorer window, such as this.
posted by kindall at 7:50 AM on July 30, 2010

Thank you, that's a better way to create the folder. But the real problem is: how do I tell Autohotkey that I want to move exactly those files that I have selected in explorer? I've tried with

Send ^x
ClipSave := ClipboardAll
FileMove, %ClipSave%, new dir

but it doesn't work.
posted by volpe at 9:00 AM on July 30, 2010

Yeah, AutoHotkey is really awful with files on the clipboard. That ClipboardAll trick you're using is one they recommend, I know, but I've had limited success with it. This is largely because for some inane reason AutoHotkey likes to try to interpret the clipboard every time you reference in a script; as a result, the built-in variable "Clipboard" does not always correspond directly to the actual clipboard, and calling that variable tends to automatically perform conversions on it. That's not a big deal when you're dealing with plain text on the clipboard, but AutoHotkey can choke on full binary files and even images on the clipboard, or mess them up in unexpected or counter-intuitive ways.

In this case, what you want most is to extract the file paths when the whole binary files themselves are placed on the clipboard through an in-explorer file select and copy. Since AutoHotkey is performing conversions on the clipboard whenever it's referenced, the trick is to perform the right conversion to extract those file paths and use them. Using the handy technique listed on the page for the Clipboard built-in variable, this is some code that should do what you're hoping to do:

Loop, parse, clipboard, `n, `r
FileMove,%A_LoopField%,new dir

posted by koeselitz at 1:45 PM on July 30, 2010

(I've tested that in XP SP2, and it seems to work fine for me; your mileage may vary.)
posted by koeselitz at 1:45 PM on July 30, 2010

(Also, part of the principle of wanting to do it that way is that the clipboard is not actually directly involved when you're cutting and pasting files in Explorer. What actually happens is that Explorer sees what you're trying to do and steps in and flips the inodes so that the file just moves; if Explorer actually took all the binary data from a file, copied it to the clipboard buffer, deleted it from its original location, copied it to the new location, and deleted it from the clipboard buffer - the way it usually does when you copy and paste - it would slow noticeably, because that's a really topheavy way to move files. But if you use AutoHotkey to try to manually copy files, you're forcing Explorer to perform the actual buffering and transfer of all that binary data. It's much more efficient just to extract the file paths so that you can use AutoHotkey's FileMove function; I guess you were already doing that a bit, though.)
posted by koeselitz at 1:53 PM on July 30, 2010

So, here's the finished script. Thank you!

@kindall: your method for getting the current path didn't work on my computer, but I've found a workaround.
; this script is bound to Ctrl+Alt+Shift+N 
; (while Ctrl+Shift+N is create a new folder by default
; (at least on win 7))
Send ^c ;copy the files you selected to the clipboard
WinGetTitle, thePath, A ;get current window's title
; Remember to edit the folder preferences in Windows Explorer,
; enabling "Show full path in titlebar"
; here you choose the name of the new folders
; if a folder with the name you chose alread exists, 
; append -- and a progressive number
IfExist %theFirstFolder%
		theNewFolder = %theFirstFolder%--%A_Index%
		IfNotExist %theNewFolder%
FileCreateDir, %theNewFolder% ;creates the new folder
; for every file path in the clipboard...
Loop, parse, clipboard, `n, `r
        ; check if it's a folder or a file
	FileGetAttrib, theAttributes, %A_LoopField%
	IfInString, theAttributes, D
                ; get the name of the folder and move
	} else {
                ; move the file

posted by volpe at 5:52 AM on July 31, 2010

« Older I have a bunch of funny/intere...   |  Finding the network of differe... Newer »
This thread is closed to new comments.