Check for folder, if not exist, create - how best to do?

Get help with using AutoHotkey and its commands and hotkeys
omar
Posts: 462
Joined: 22 Oct 2015, 17:56

Check for folder, if not exist, create - how best to do?

12 Jan 2021, 23:58

I searched and found and old thread.

2 answers:

Code: Select all

IfNotExist, %CLogDir%
   FileCreateDir, %LogDir%
This doesn't work.
What is LogDir? The folder name you are trying to create?
But then what is CLogDir??

Before that someone gave this answer:

Code: Select all

If( InStr( FileExist("Dirname"), "D") )
	msgbox Directory exists
I get this one and understand more clearly.
I'm using this version for now (with FileCreateDir, DirName after)

Just wondering which is the better one. If first one, how do I make work?

Thanks.
User avatar
mikeyww
Posts: 5362
Joined: 09 Sep 2014, 18:38

Re: Check for folder, if not exist, create - how best to do?

13 Jan 2021, 00:51

Code: Select all

dir = %TEMP%\test
If !FileExist(dir) {
 FileCreateDir, %dir%
 If ErrorLevel
  MsgBox, 48, Error, An error occurred when creating the directory.`n`n%dir%
 Else MsgBox, 64, Success, Directory was created.`n`n%dir%
} Else MsgBox, 64, Exists, Directory already exists.`n`n%dir%
It's not a perfect script because it does not distinguish the directory from a file by the same name, but that would be uncommon, and the script can be modified to rectify the issue if it seems possible. Actually, it appears that AHK generates an error when trying to create a directory if a file by the same name already exists.

Return to “Ask For Help”

Who is online

Users browsing this forum: Arsonistic, mikeyww, Spawnova and 54 guests