Page 1 of 1

Directory Creation

Posted: 08 Dec 2021, 11:20
by b4iknew
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

Re: Directory Creation

Posted: 08 Dec 2021, 13:52
by flyingDman

Re: Directory Creation

Posted: 08 Dec 2021, 14:24
by RussF
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

Re: Directory Creation

Posted: 08 Dec 2021, 14:35
by b4iknew
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".

Re: Directory Creation  Topic is solved

Posted: 08 Dec 2021, 15:16
by RussF
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

Re: Directory Creation

Posted: 08 Dec 2021, 16:45
by TheDewd

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
			}
		}
	}
}

Re: Directory Creation

Posted: 08 Dec 2021, 17:04
by RussF
Well done!

Russ

Re: Directory Creation

Posted: 08 Dec 2021, 18:51
by b4iknew
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!

Re: Directory Creation

Posted: 08 Dec 2021, 19:26
by flyingDman
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.

Re: Directory Creation

Posted: 09 Dec 2021, 06:29
by RussF
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

Re: Directory Creation

Posted: 11 Dec 2021, 11:57
by b4iknew
Thanks @flyingDman , I will definately give it a try.