topbanner_forum
  *

avatar image

Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
  • October 17, 2019, 11:20 PM
  • Proudly celebrating 13 years online.
  • Donate now to become a lifetime supporting member of the site and get a non-expiring license key for all of our programs.
  • donate

Author Topic: IDEA: Subfolder Creator  (Read 1527 times)

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 153
    • View Profile
    • Donate to Member
IDEA: Subfolder Creator
« on: April 13, 2019, 12:49 PM »
Ideally AHK so I can learn.

A dropzone where one or more folders can be dropped from various source locations.

Dropzone creates one or more empty folders of user defined name(s) inside dropped folder(s).

Sounds simple, but is it?

Purpose is to allow pre-population of folder sets for subsequent use.

TIA
« Last Edit: April 13, 2019, 12:55 PM by magician62 »

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 969
    • View Profile
    • Donate to Member
Re: IDEA: Subfolder Creator
« Reply #1 on: April 13, 2019, 01:49 PM »
An example, just to see if I understand the request.

If the user drops the folder
C:\this\folder
on the GUI dropzone then the program should create
C:\this\folder\somesubfolder
C:\this\folder\anothersubfolder
C:\this\folder\third

And similarly create the same named subfolders if we drop the folder C:\other\directory and so on.

If that's it then AutoHotkey can do it.

But if you want to learn perhaps you'd like to give it a shot yourself first? Help on commands to use
https://autohotkey.com/docs/Tutorial.htm
https://autohotkey.c...ocs/commands/Gui.htm
https://autohotkey.c...Gui.htm#GuiDropFiles
https://autohotkey.c...ds/FileCreateDir.htm

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 153
    • View Profile
    • Donate to Member
Re: IDEA: Subfolder Creator
« Reply #2 on: April 13, 2019, 02:45 PM »
You have it correct
  :up:

Although I have looked at AHK in the past only bits of it made sense. I still have a long, long way to go. :)

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 969
    • View Profile
    • Donate to Member
Re: IDEA: Subfolder Creator
« Reply #3 on: April 13, 2019, 05:35 PM »
Ok, here is a basic version. You could enhance it in several ways: Edit the subfolder names of course; Add editboxes so the user can change subfolder names on each use; Change the color, font and look of the window; Accept dropping more than one folder at a time; and more. If you look up the commands on each line in the AutoHotkey manual you'll quickly get ideas on how to tweak things.

Code: Autohotkey [Select]
  1. Gui, font, s14 cBlack bold
  2. Gui, Add, Text,x100 y120,Drop folder
  3. Gui, Show, w300 h300, Subfolder Creator
  4. return
  5.  
  6. GuiDropFiles:
  7.  Loop, parse, A_GuiControlEvent, `n, `r
  8.   {
  9.     folder := A_LoopField
  10.     break
  11.   }
  12.   if !InStr(FileExist(folder), "D")
  13.     return
  14.    FileCreateDir, %folder%\subfolder name
  15.    FileCreateDir, %folder%\another subfolder
  16.    FileCreateDir, %folder%\subfolder 3
  17. return
  18.  

Here is how it looks in Windows 10
3.png
« Last Edit: April 13, 2019, 05:55 PM by Nod5 »

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 153
    • View Profile
    • Donate to Member
Re: IDEA: Subfolder Creator
« Reply #4 on: April 14, 2019, 02:50 AM »
I like your use of "quickly ". It is a relative term, and the closer you get to retirement, the longer "quickly" takes. :)

From what you have there I can see how the GUI is created. In my working version I have moved the box creation to the top which seems to work, but is there a reason not to do this? To me the logical progression is make a box, chose pen and ink, and write something with it. :)

I created a gosub for folder creation and dropped it to the bottom with the appropriate entries for folder creation.

The bit I had trouble with is a loop for a group of folders dropped. I took a while before I realised that your "Break" might be stopping a Loop through all the folders dropped  So tried substituting the gosub for the Break, and all seems to work!

Code: Autohotkey [Select]
  1. Gui, Show, w300 h300, Subfolder Creator
  2. Gui, font, s14 cBlack bold
  3. Gui, Add, Text, x150 y150, Drop folder
  4.  
  5. return
  6.  
  7. GuiDropFiles:
  8.  Loop, parse, A_GuiControlEvent, `n, `r
  9.   {
  10.     folder := A_LoopField
  11.     gosub,create_folder
  12.   }
  13.  
  14. return
  15.  
  16.  
  17.  
  18. create_folder:
  19.  if !InStr(FileExist(folder), "D")
  20.     return
  21.    FileCreateDir, %folder%\p01
  22.    FileCreateDir, %folder%\p02
  23.    FileCreateDir, %folder%\p03
  24.    FileCreateDir, %folder%\p04
  25.    FileCreateDir, %folder%\p05
  26.    FileCreateDir, %folder%\p06
  27.    FileCreateDir, %folder%\p07
  28.    FileCreateDir, %folder%\p08
  29.    FileCreateDir, %folder%\p09
  30.    FileCreateDir, %folder%\p10
  31.    FileCreateDir, %folder%\p11
  32.    FileCreateDir, %folder%\p12
  33. return
« Last Edit: April 14, 2019, 06:45 AM by magician62 »

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 969
    • View Profile
    • Donate to Member
Re: IDEA: Subfolder Creator
« Reply #5 on: April 14, 2019, 05:37 AM »
You've enhanced the program to accept multiple folder drops at once - nice going!  :)

Tip: when you add code to a forum post use the "code highlighting" dropdown to colorize the AutoHotkey code, like in my posts.

In my working version I have moved the box creation to the top which seems to work, but is there a reason not to do this?
No problem in this case. But with more complex GUIs putting Show first can result in the user first seeing a blank window and only later text, buttons, editboxes. Avoided by putting Show last. Just to illustrate that try what happens if you add in a delay, like so

Code: Autohotkey [Select]
  1. Gui, Show, w300 h300, Subfolder Creator
  2. Sleep 700
  3. Gui, font, s14 cBlack bold
  4. Gui, Add, Text,x150 y150,Drop folder

To me the logical progression is make a box, chose pen and ink, and write something with it.
Makes sense in the sketching/planning phase. But that can differ from what is later on the best code order. It often helps to comment (shown in green here) the code to make it more intuitive to read later. For example
Code: Autohotkey [Select]
  1. ;Main GUI window where user drops folders
  2. Gui, font, s14 cBlack bold
  3. Gui, Add, Text, x150 y150, Drop folder
  4. Gui, Show, w300 h300, Subfolder Creator

I created a gosub for folder creation and dropped it to the bottom with the appropriate entries for folder creation.
That works. Just for example, another way to do it without the gosub. Notice: continue instead of break.
Code: Autohotkey [Select]
  1.     #SingleInstance force
  2.     Gui, font, s14 cBlack bold
  3.     Gui, Add, Text,x100 y120,Drop folder
  4.     Gui, Show, w300 h300, Subfolder Creator
  5.     return
  6.      
  7.     GuiDropFiles:
  8.      Loop, parse, A_GuiControlEvent, `n, `r
  9.       {
  10.         folder := A_LoopField
  11.         if !InStr(FileExist(folder), "D")
  12.           continue
  13.          FileCreateDir, %folder%\subfolder name
  14.          FileCreateDir, %folder%\another subfolder
  15.          FileCreateDir, %folder%\subfolder 3
  16.       }
  17.     return
  18.      
  19.     GuiClose:
  20.     exitapp

Or alternatively with a function instead of gosub
Code: Autohotkey [Select]
  1.     #SingleInstance force
  2.     Gui, font, s14 cBlack bold
  3.     Gui, Add, Text,x100 y120,Drop folder
  4.     Gui, Show, w300 h300, Subfolder Creator
  5.     return
  6.      
  7.     GuiDropFiles:
  8.      Loop, parse, A_GuiControlEvent, `n, `r
  9.       {
  10.         folder := A_LoopField
  11.         if InStr(FileExist(folder), "D")
  12.           MakeFolders(folder)
  13.       }
  14.     return
  15.      
  16.     GuiClose:
  17.     exitapp
  18.  
  19. MakeFolders(folder) {
  20.   FileCreateDir, %folder%\subfolder name
  21.   FileCreateDir, %folder%\another subfolder
  22.   FileCreateDir, %folder%\subfolder 3
  23. }

One great thing with AutoHotkey is that it is relatively easy to get going with commands and make small programs. We can then gradually move from commands/gosub style coding to expressions, functions and objects as the programs get more complex.

If all your subfolder names will have an increasing pattern you can shorten code by replacing all the FileCreateDir lines with a loop

Simple example, but without padding so the folders get named p1, p2 ... rather than p01, p02 ...
Code: Autohotkey [Select]
  1. Loop, 12
  2.   FileCreateDir, %folder%\p%A_Index%
This gives padding
Code: Autohotkey [Select]
  1. Loop, 12
  2. {
  3.   if A_Index < 10
  4.     FileCreateDir, %folder%p0%A_Index%
  5.   else
  6.     FileCreateDir, %folder%p%A_Index%
  7. }
This gives padding in a shorter way by using expressions and the Format() function.
Code: Autohotkey [Select]
  1. Loop, 12
  2.   FileCreateDir, % folder "\p" Format("{1:02}", A_Index)
« Last Edit: April 14, 2019, 05:49 AM by Nod5 »

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 153
    • View Profile
    • Donate to Member
Re: IDEA: Subfolder Creator
« Reply #6 on: April 14, 2019, 06:50 AM »
Thank you for the answers and suggestions to improve. As I say a lot to learn. Although I played with php about 10, I have probably forgotten most of it, prior to that it was BASIC almost 40 years ago lol.

The nice thing is I use this process in various different cases so can adjust and re-use as needed.

At present it is for downloaded newspaper pages, where they may not be sequentially named. So dropping into the right folder allows for a subsequent quick rename using the folder as the new filename. Only 150 years of a weekly papers to work through. :)

Nod5

  • Supporting Member
  • Joined in 2006
  • **
  • Posts: 969
    • View Profile
    • Donate to Member
Re: IDEA: Subfolder Creator
« Reply #7 on: April 15, 2019, 02:02 AM »
At present it is for downloaded newspaper pages, where they may not be sequentially named. So dropping into the right folder allows for a subsequent quick rename using the folder as the new filename. Only 150 years of a weekly papers to work through.

Wow, that's a lot! :)

If you're downloading the pages for one paper issue in the right sequence and without gaps (download page 1 first, then page 2, ...) you might be able to rename in one go with a script that reads the "date modified" metadata. At least if there is reliably one or more second time difference between each download. See variable A_LoopFileTimeModified for Loop command.

magician62

  • Supporting Member
  • Joined in 2011
  • **
  • Posts: 153
    • View Profile
    • Donate to Member
Re: IDEA: Subfolder Creator
« Reply #8 on: April 16, 2019, 01:14 AM »
Unfortunately sometimes the download misses one or takes two copies. And to make things worse, the page code is broken. If you go one direction you get the numbers generally in random ascending order, but if you miss one and have to go back, the filename is different, or better put, the same as another page!