Skip to content

Instantly share code, notes, and snippets.

@Sieboldianus
Last active February 21, 2024 12:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Sieboldianus/11a9c89af4f7444e368d74a6c2dfbaaa to your computer and use it in GitHub Desktop.
Save Sieboldianus/11a9c89af4f7444e368d74a6c2dfbaaa to your computer and use it in GitHub Desktop.
Create New Folder and Create New File AutoHotKey Scripts

Create New Folder and Create New File AutoHotKey Script

Tested for: Win7, Win10, Win11

These two scripts are slightly modified from 1:

  • Alt+N will create a filename, with default name notes.md
  • Alt+F will create a folder, with default name of the current date in format YYYY-MM-DD

Note:

  • Both names can be modified in a user input field before pressing enter.
  • File/folder will only be created in the current explorer window, if it exists
  • The file will be opened afterwards using the standard application responsible for the filetype

Add to startup: WIN-key+R, shell:startup -> Add shortcuts to the two ahk files below.

; This is part of my AutoHotKey [1] script. When you are in Windows Explorer it
; allows you to press Alt+N and type a filename, and that file is created
; in the current directory and opened in the appropriate editor (usually
; [gVim](http://www.vim.org/) in my case, but it will use whatever program is
; associated with the file in Windows Explorer).
; This is much easier than the alternative that I have been using until now:
; Right click > New > Text file, delete default filename and extension (which
; isn't highlighted in Windows 7), type the filename, press enter twice.
; (Particularly for creating dot files like ".htaccess".)
; Credit goes to aubricus [2] who wrote most of this and davejamesmiller [3] who
; added the 'IfWinActive' check and 'Run %UserInput%' at the end. Also to
; syon [4] who changed regexp to make script work on non-english versions
; of Windows. And I changed the way how script gets full path to make it
; compatible with Windows 10 and also changed hotkey to Alt+N
; slightly adapted [5] with default name for folders (current date) and files (notes.md);
; and compatibility Windows 11 added
; [1]: http://www.autohotkey.com/
; [2]: https://gist.github.com/1148174
; [3]: https://gist.github.com/1965432
; [4]: https://github.com/syon/ahk/blob/master/NewFile/NewFile.ahk
; [5]: https://gist.github.com/Sieboldianus/11a9c89af4f7444e368d74a6c2dfbaaa
; Only run when Windows Explorer or Desktop is active
; Alt+N
#IfWinActive ahk_class CabinetWClass
!SC031::
#IfWinActive ahk_class ExploreWClass
!SC031::
#IfWinActive ahk_class Progman
!SC031::
#IfWinActive ahk_class WorkerW
!SC031::
; Get full path from open Explorer window
FullPath := GetActiveExplorerPath()
GetActiveExplorerPath() {
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
explorerHwnd := WinActive("ahk_class CabinetWClass")
if (explorerHwnd)
{
for window in ComObjCreate("Shell.Application").Windows
{
if (window.hwnd==explorerHwnd)
return window.Document.Folder.Self.Path
}
}
}
; Change working directory
SetWorkingDir, %FullPath%
; An error occurred with the SetWorkingDir directive
If ErrorLevel
Return
; Display input box for filename
InputBox, UserInput, New File, , , 400, 100, , , , , notes.md
; User pressed cancel
If ErrorLevel
Return
; Create file
FileAppend, , %UserInput%
; Open the file in the appropriate editor
Run %UserInput%
Return
#IfWinActive
; This is part of my AutoHotKey [1] script. When you are in Windows Explorer it
; allows you to press Alt+N and type a foldername, and that folder is created
; in the current directory and opened in the appropriate editor (usually
; [gVim](http://www.vim.org/) in my case, but it will use whatever program is
; associated with the folder in Windows Explorer).
; This is much easier than the alternative that I have been using until now:
; Right click > New > Text folder, delete default foldername and extension (which
; isn't highlighted in Windows 7), type the foldername, press enter twice.
; Credit goes to aubricus [2] who wrote most of this and davejamesmiller [3] who
; added the 'IfWinActive' check and 'Run %UserInput%' at the end. Also to
; syon [4] who changed regexp to make script work on non-english versions
; of Windows. And I changed the way how script gets full path to make it
; compatible with Windows 10 and also changed hotkey to Alt+N
; slightly adapted [5] with default name for folders (current date) and files (notes.md)
; and compatibility Windows 11 added
; [1]: http://www.autohotkey.com/
; [2]: https://gist.github.com/1148174
; [3]: https://gist.github.com/1965432
; [4]: https://github.com/syon/ahk/blob/master/NewFile/NewFile.ahk
; [5]: https://gist.github.com/Sieboldianus/11a9c89af4f7444e368d74a6c2dfbaaa
; Only run when Windows Explorer or Desktop is active
; Alt+F
#IfWinActive ahk_class CabinetWClass
!SC021::
#IfWinActive ahk_class ExploreWClass
!SC021::
#IfWinActive ahk_class Progman
!SC021::
#IfWinActive ahk_class WorkerW
!SC021::
; Get full path from open Explorer window
FullPath := GetActiveExplorerPath()
GetActiveExplorerPath() {
; https://www.autohotkey.com/boards/viewtopic.php?f=6&t=69925
explorerHwnd := WinActive("ahk_class CabinetWClass")
if (explorerHwnd)
{
for window in ComObjCreate("Shell.Application").Windows
{
if (window.hwnd==explorerHwnd)
return window.Document.Folder.Self.Path
}
}
}
; Change working directory
SetWorkingDir, %FullPath%
; An error occurred with the SetWorkingDir directive
If ErrorLevel
Return
; Display input box for foldername
InputBox, UserInput, New Folder, , , 400, 100, , , , , %A_YYYY%-%A_MM%-%A_DD%
; User pressed cancel
If ErrorLevel
Return
; Create folder
FileCreateDir, %UserInput%
; Open the folder in the appropriate editor
Run %UserInput%
Return
#IfWinActive
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment