 |
AutoHotkey Community Let's help each other out
|
| View previous topic :: View next topic |
| Author |
Message |
danthecardman
Joined: 23 Oct 2007 Posts: 3 Location: wyoming
|
Posted: Sun Oct 28, 2007 12:00 am Post subject: Building Folders From File Names |
|
|
I need a script that simply creates folders out of file names in a directory.
For example, I have a folder containing an enormous list of files. Below is my code and in it I have taken a rather brute force approach. So far I have been successful writing the script to do the task for a single file-to folder renaming. The script does exactly this: selects a file at the top of the folder, highlights the name of the file then copies that file's name, then I create a new folder (by right-clicking off from that top-most file), and finally pasting the name recently highlighted so as to rename this new folder exactly as the name of highlighted file.
I simply want to find out how to create a loop that will increment/step down/progress to each file name and repeat the code that thus far works for a one file-to-folder creation.
Where I am stuck at is in creating the loop (probably recursion is the key here). In the code below the cursor goes to the top of the folder by pressing HOME key (e.g. SEND, {HOME}). It is at this point that I want to have it do a {HOME++} or a {DOWN++} ; thereby pressing the down arrow consecutively through the loop to iterate to the next file in the list.
The reason for the right click of the mouse in my code was to simply move the mouse over to a random location with inside the folder so that I could utilize the menu that happens when I right-click my mouse.
Here is my code:
Sleep, 100
WinWait, MP3_RandomFolder,
IfWinNotActive, MP3_RandomFolder, , WinActivate, MP3_RandomFolder,
WinWaitActive, MP3_RandomFolder,
SendInput, {HOME} ; highlights the song at the top of the containing folder
SendInput, {F2}{CTRLDOWN}c{CTRLUP} ; copies the name of the file
Sleep, 100
MouseClick, right, 656, 170 ; moves away from the file to bring up the right-click menu in order to prepare for creating a NEW FOLDER
SendInput, w{ENTER};built in right-click shortcut for creating NEW FOLDER
Sleep,100
SendInput, {CTRLDOWN}v{CTRLUP}{ENTER} ; renames the NEW FOLDER
And laugh all you want at my rookie coding but hey the code is tight and it's major drawback is that the directory name is hard coded in and therefore my technique is not a dynamic approach. All I would love is to see someone's awesome approach at creating a tightly coded approach on creating an incremental loop. Then I myself would be able to control the number of iterations by simply counting the number of files (or Mp3 files in this particular instance).
I would greatly appreciate anyone's skillful help. Thank you in advance
Dan-TheCardMan |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 265 Location: Russia
|
Posted: Sun Oct 28, 2007 9:28 am Post subject: |
|
|
Am I getting you right that you want to run this script when you are in an Explorer window? And you want to create new folders in the same directory where the files lie? If so, try the following code. But I could not menage to create folders with exactly the same names as files. Windows wouldn't let me do that. So I had to add underscores.
| Code: |
#IfWinActive, ahk_class ExploreWClass ; Explorer window with the tree view.
#n:: CreateFolders() ; Press Win-N to create folders.
#IfWinActive, ahk_class CabinetWClass ; Explorer without the tree view.
#n:: CreateFolders()
#IfWinActive
;===============================================
CreateFolders()
{
ControlFocus, SysListView321, A ; Move focus to the file list.
Send, ^a ; Select all.
Clipboard= ; Empty the clipboard.
Send, ^c ; Copy all.
ClipWait, 5 ; Wait for the data to arrive. 5 seconds.
If ErrorLevel
{
MsgBox, Could not copy files onto the clipboard.
Return
}
List=%Clipboard% ; Get the list from the clipboard.
Count=0
Loop, Parse, List, `n, `r ; Read it line by line.
{
Item=%A_LoopField%
FileGetAttrib, Attribs, %Item% ; Check if the current item is a folder.
If InStr(Attribs, "D")
Continue ; If so, skip the rest of the code.
SplitPath, Item, FileName, ParentDir
FileCreateDir, %ParentDir%\%FileName%_
If not ErrorLevel
Count++
}
MsgBox, Done. %Count% folders created.
}
|
|
|
| Back to top |
|
 |
danthecardman
Joined: 23 Oct 2007 Posts: 3 Location: wyoming
|
Posted: Wed Oct 31, 2007 4:57 am Post subject: Building Folders From File Names |
|
|
First a note about the code YMP developed to help me out- it purely rocks! If you ever want to save time sorting though a large mix of random files (like documents or Mp3's) and file them into matching folders identical to each file needing sorted, this code is amazing and all you do is use the Hotkey: "WindowsKey+n" (as in "Windows NEW FOLDER")
YMP... I want to truly thank you for your code you put together for me. Learning to write code and seeing how others' code, is exciting to me like playing a video game is for some people. I have spent all my free time in the AHK forum lately trying to soak up volumes of ideas; and after seeing your code, my mouth dropped in astonishment (honestly how long have you been programming?). You gave me code that did exactly what I was in search of. What you did was save me many weeks of free time I know I'd be spending trying to solve this little problem of sorting massively sized folders (that I have never before been able to efficiently sort..until now).
Within a few short days of me posting the question to the forum you had a solution to a puzzle I have been stuck trying to figure out (for quite sometime). Thank you so very much for your generous time you gave in helping me out. I might be skilled at handling a deck of cards, but to me seeing how you did what you did is like the excitement I get watching another great magician perform- your code is so good; it's amazing to me! Was this super simple and the idea just was so obvious to you? I greatly respect you, thank you.
I believe in returning the favor to people who help me out (as you can tell I am no coding-magician by any means), but If you ever wanted to learn a great trick or a few stunning card moves, card magic is my thing. Let me know and I will return the generosity ten-fold. Thank you sir (or ma'am)...stop by my site and from there you can drop me an email in the Contact Us section at http://www.danthecardman.com |
|
| Back to top |
|
 |
YMP
Joined: 23 Dec 2006 Posts: 265 Location: Russia
|
Posted: Wed Oct 31, 2007 7:58 am Post subject: |
|
|
Thank you for your kind words. If it was not so hard, that's because AutoHotkey is so powerful. We can now save a lot of our time because Chris spent a lot of his while developing it. After a few months with AHK and its really amazing Help where everything is so thoroughly described, you don't need to be a programmer to make people think you are.  |
|
| Back to top |
|
 |
|
|
You can post new topics in this forum You can reply to topics in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|