Creating Folders in Windows Explorer

Get help with using AutoHotkey (v2 or newer) and its commands and hotkeys
Gorpis
Posts: 8
Joined: 24 Jan 2023, 18:36

Creating Folders in Windows Explorer

Post by Gorpis » 26 Jan 2023, 18:07

What I am doing wrong here, I want to create these folders when I am in Windows Explorer and it does not work

Code: Select all

;----------------------------------------------------------------------------------
; Generates folder structure for new Projects in Google Drive via Windows Explorer
; Date created: 1/26/2023
; Date of last edit: 1/26/2023
;---------------------------------------------------------------------------------

#HotIf WinActive("ahk_class CabinetWClass")

^F9::

{

CreateFolder("Bid Docs")
CreateFolder("Equipment Scopes")
CreateFolder("Order Acknowledgements")
CreateFolder("Pricing")
CreateFolder("Purchase Orders")
CreateFolder("Selections")
CreateFolder("Submittals")

CreateFolder(FolderName) {

	Send("{^+n}")
	Sleep(500)
	Send(FolderName)
	Send("{Enter}")
	Sleep(500)

	}

}
[Mod edit: [code][/code] tags added.]

User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: Creating Folders in Windows Explorer

Post by boiler » 26 Jan 2023, 18:20

Rather than interacting with a File Explorer window, it would be faster and much more reliable for your script to have you select your project folder using DirSelect and then create your new folders within the selected folder using DirCreate.

User avatar
andymbody
Posts: 916
Joined: 02 Jul 2017, 23:47

Re: Creating Folders in Windows Explorer

Post by andymbody » 26 Jan 2023, 21:17

I agree with @boiler...

also... I dont see anything in the v2 docs about using parenthesis...

should this
Send("{Enter}")

be this?
Send "{Enter}"

as well as removing parentheses from the other Send's?

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Creating Folders in Windows Explorer

Post by mikeyww » 26 Jan 2023, 21:35

It makes no difference, but I think that this would also be clear by calling those functions.

Code: Select all

#Requires AutoHotkey v2.0
Send 54  ; Send is a function
Send(54) ; Send is still a function
Send("{^+n}") => Send("^+n")

Code: Select all

#Requires AutoHotkey v2.0

#HotIf WinActive("ahk_class CabinetWClass")
^F9:: {
 Static dirList := ["test1", "test2", "test3"]
 curPath := explorerGetPath()
 For each, dir in dirList
  DirCreate curPath '\' dir
 SoundBeep 1500
}
#HotIf

explorerGetPath(hwnd := 0) { ; https://www.autohotkey.com/boards/viewtopic.php?p=387113#p387113
 Static winTitle := "ahk_class CabinetWClass"
 hWnd ?    explorerHwnd := WinExist(winTitle " ahk_id " hwnd)
      : ((!explorerHwnd := WinActive(winTitle)) && explorerHwnd := WinExist(winTitle))
 If explorerHwnd
  For window in ComObject("Shell.Application").Windows
   Try If window && window.hwnd && window.hwnd = explorerHwnd
    Return window.Document.Folder.Self.Path
 Return False
}

User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: Creating Folders in Windows Explorer

Post by boiler » 26 Jan 2023, 23:11

From the documentation:
Function Call Statements wrote:If the return value of the function is not needed and the function name is written at the start of the line (or in other contexts which allow a statement, such as following else or a hotkey), the parentheses can be omitted. In this case, the remainder of the line is taken as the function's parameter list. For example:

Code: Select all

result := MsgBox("This one requires parentheses.",, "OKCancel")
MsgBox "This one doesn't. The result was " result "."
…and:
Changes from v1.1 to v2.0 wrote: Removed "command syntax". There are no "commands", only function call statements, which are just function or method calls without parentheses. That means:
  • All former commands are now functions (excluding control flow statements).
  • All functions can be called without parentheses if the return value is not needed (but as before, parentheses cannot be omitted for calls within an expression).

User avatar
andymbody
Posts: 916
Joined: 02 Jul 2017, 23:47

Re: Creating Folders in Windows Explorer

Post by andymbody » 27 Jan 2023, 02:41

Thanks for the clarification @mikeyww and @boiler

do you guys ever sleep? :D

Gorpis
Posts: 8
Joined: 24 Jan 2023, 18:36

Re: Creating Folders in Windows Explorer

Post by Gorpis » 28 Jan 2023, 11:57

When I ran the code I got this error, what do you think needs to be done to fix it:

Error: This variable has not been assigned a value.

Specifically: local curPath

006: curPath := explorerGetPath()
007: For each, dir in dirList
▶ 008: DirCreate(curPath '\' dir)
009: SoundBeep(1500)
010: }

Gorpis
Posts: 8
Joined: 24 Jan 2023, 18:36

Re: Creating Folders in Windows Explorer

Post by Gorpis » 28 Jan 2023, 12:00

I got my original code to work by changing

Send("{^+n}")

to

Send("^+n")

I did not understand the curly brackets are for specific keys like {Enter} rather than combinations of keys which do not get the curly brackets

User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: Creating Folders in Windows Explorer

Post by boiler » 28 Jan 2023, 12:22

For one thing, braces go around a key name. You surrounded a group keys including the modifiers with them (i.e., ^+n is not a key — it’s three keys). They generally are only needed for key names that have multiple letters like Tab to distinguish it from sending the letters T, a, and b. Also, there is a list of key names found at the Send documentation.

Gorpis
Posts: 8
Joined: 24 Jan 2023, 18:36

Re: Creating Folders in Windows Explorer

Post by Gorpis » 30 Jan 2023, 10:03

Yes, I figured that out a few days ago, as I mentioned new to this so drinking from the firehose at this point, thanks for your help. Any ideas why the code above does not work? Its does not like this line: curPath := explorerGetPath()

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Creating Folders in Windows Explorer

Post by mikeyww » 30 Jan 2023, 11:17

If you are seeing an error message, then post your script, a screenshot of the error window, and all of the steps that you take to generate the error.

User avatar
boiler
Posts: 16915
Joined: 21 Dec 2014, 02:44

Re: Creating Folders in Windows Explorer

Post by boiler » 30 Jan 2023, 11:40

Gorpis wrote: Its does not like this line: curPath := explorerGetPath()
I'm going to guess that you did not copy mikeyww's entire script -- only the part that is visible in the code box without scrolling or expanding it -- so you did not copy the function itself into the script. The reason it's important to share the actual error message with us is that it gives much more detail about what the problem is, where telling us "it does not like this line..." tells us almost nothing.

Gorpis
Posts: 8
Joined: 24 Jan 2023, 18:36

Re: Creating Folders in Windows Explorer

Post by Gorpis » 30 Jan 2023, 13:53

I didn't copy it all, now I am really feeling like an idiot, thank you for your help again and not roasting me!

User avatar
mikeyww
Posts: 26871
Joined: 09 Sep 2014, 18:38

Re: Creating Folders in Windows Explorer

Post by mikeyww » 30 Jan 2023, 14:54

You're not the first to miss the hidden text in a post!

Post Reply

Return to “Ask for Help (v2)”