Directory Creation Topic is solved

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
User avatar
b4iknew
Posts: 4
Joined: 08 Dec 2021, 11:01

Directory Creation

08 Dec 2021, 11:20

Hello, I thought this would be easy. The code below works fine if I am on my desktop. It won't however work in a File Explorer at all. The only directories it makes are a couple named, ces, Data, and New Folder. Why won't it work in File Explorer?
Any help would be appreciated, thanks.

Code: Select all

;#IfWinActive ahk_class CabinetWClass ahk_exe explorer.exe

^NumPad7::
FileCreateDir("Assets")
FileCreateDir("Data")
FileCreateDir("Resources")
FileCreateDir("Temp")

FileCreateDir(FolderName) {
	Send, ^+n
	Send, %FolderName%
	Sleep, 500
	Send, {Enter}
}
return
RussF
Posts: 1260
Joined: 05 Aug 2021, 06:36

Re: Directory Creation

08 Dec 2021, 14:24

Probably not a good idea to create your own function with the same name as a built-in command.

Try this:

Code: Select all

^NumPad7::
FileCreateMyDir("Assets")
FileCreateMyDir("Data")
FileCreateMyDir("Resources")
FileCreateMyDir("Temp")

Return

FileCreateMyDir(FolderName) {
	Send, ^+n
	Send, %FolderName%
	Sleep, 500
	Send, {Enter}
}
Note the new location of "Return" and make sure the explorer window is in focus before pressing your hotkey. It worked for me.

Russ
User avatar
b4iknew
Posts: 4
Joined: 08 Dec 2021, 11:01

Re: Directory Creation

08 Dec 2021, 14:35

Hey RussF, with the movement of the "return" command, it did indeed create 4 new folders in "File Explorer", it didn't give them the correct names. It named them, "ces", "New Folder", "New Folder (2)", "New Folder (3)".
I'm not quite sure why. It does still work perfectly if I am on my Desktop, just not in "File Explorer".
RussF
Posts: 1260
Joined: 05 Aug 2021, 06:36

Re: Directory Creation  Topic is solved

08 Dec 2021, 15:16

It worked perfectly for me. Did you change the name of your function and function calls from "FileCreateDir" to "FileCreateMyDir"?

It could be that your system is responding too slowly to capture the folder name since it is only getting the last 3 chars of "ResourCES".

Try:

Code: Select all

SendMode Input
^NumPad7::
FileCreateMyDir("Assets")
FileCreateMyDir("Data")
FileCreateMyDir("Resources")
FileCreateMyDir("Temp")

Return

FileCreateMyDir(FolderName) {
	Send, ^+n
    	Sleep, 500
	Send, %FolderName%
	Sleep, 500
	Send, {Enter}
}
You could also play with the value of the first Sleep command and you probably don't need the second Sleep. You might also try adding Sleeps between your 4 function calls as well (or just before the first Send instead). I honestly think it is just a timing issue.

Russ
User avatar
TheDewd
Posts: 1510
Joined: 19 Dec 2013, 11:16
Location: USA

Re: Directory Creation

08 Dec 2021, 16:45

Code: Select all

#SingleInstance, Force

^NumPad7::
	ActivePath := GetActiveExplorerPath()
	
	If (ActivePath) {
		FileCreateMyDir(ActivePath, "Assets")
		FileCreateMyDir(ActivePath, "Data")
		FileCreateMyDir(ActivePath, "Resources")
		FileCreateMyDir(ActivePath, "Temp")
	}
return

FileCreateMyDir(Path, FolderName) {
	FileCreateDir, % Path "\" FolderName
}

GetActiveExplorerPath() {
	explorerHwnd := WinActive("ahk_class CabinetWClass")
	
	If (explorerHwnd) {
		For window in ComObjCreate("Shell.Application").Windows {
			If (window.hwnd==explorerHwnd) {
				return window.Document.Folder.Self.Path
			}
		}
	}
}
RussF
Posts: 1260
Joined: 05 Aug 2021, 06:36

Re: Directory Creation

08 Dec 2021, 17:04

Well done!

Russ
User avatar
b4iknew
Posts: 4
Joined: 08 Dec 2021, 11:01

Re: Directory Creation

08 Dec 2021, 18:51

Hey Russ, thanks so much for your reply. Adding that second sleep to the script did the trick, even though I am us a Samsung 970 Pro 1 TB M.2-2280 NVME, I guess it just isn't fast enough. Thank you also TheDewd, I haven't tried your script. I'm sure it worked just fine, this was a simpler (shorter) solution. Thank you both for taking the time, and have a great day!
User avatar
flyingDman
Posts: 2817
Joined: 29 Sep 2013, 19:01

Re: Directory Creation

08 Dec 2021, 19:26

I would encourage you to try @TheDewd's script. It is (sorry @RussF ) vastly superior to the one you've picked. The main issue is that it uses simulated keystrokes and it only works when the "sleep" periods are right. It is not a good practice to rely on that.
14.3 & 1.3.7
RussF
Posts: 1260
Joined: 05 Aug 2021, 06:36

Re: Directory Creation

09 Dec 2021, 06:29

No apologies needed @flyingDman! As you saw, I gave @TheDewd the credit deserved. I've been coding long enough to realize that no matter what you come up with, whether it works or not, there is always a better, faster, more reliable or more elegant way to accomplish the same task. I'm still relatively new to the world of AHK, but try to help out wherever I can. I also learn a lot from this forum, thanks to people such as @TheDewd and yourself. Cheers!

Russ
User avatar
b4iknew
Posts: 4
Joined: 08 Dec 2021, 11:01

Re: Directory Creation

11 Dec 2021, 11:57

Thanks @flyingDman , I will definately give it a try.

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: haomingchen1998, mikeyww and 240 guests