The first half of the question is answered here.
The solution to create a folder only if it doesn't exist in a batch file is the following:
if not exist "C:\FOLDER_NAME" mkdir C:\FOLDER_NAME
The problem we're facing is this batch file is being run in parallel. For business reasons we can't change the timing of when these batch file are started. Two of the jobs are clearly passing the if
statement, but one is creating the folder and the other raises the error "A subdirectory or file FOLDER_NAME already exists".
The %ERRORLEVEL% = 1
, and the thought was to simply do the following:
if %ERRORLEVEL% gtr 1 goto errorexit
. Access is denied is also %ERRORLEVEL% = 1
so this solution isn't feasible; this error stills need to exit the batch process.
How does one deal with race conditions when using mkdir in this context?