How do I create folder (and any subfolders) in batch script? But the important thing is that, if the folder (or any subfolders) already exists, it should not return error.
For example, something like this:
mkdir mydir
- success(directory is now created)mkdir mydir\subdir
- success(now mydir
contains subdir
)mkdir mydir
- success(folder already exists, should not throw an error)mkdir mydir\subdir
- success(folders already exists, should not throw an error)What I actually need is just to ensure that the folder structure exists.
You need to check the path and create if it doesn't exist
if not exist "mydir\subdir" md "mydir\subdir"
Or you can also suppress the error message by redirecting stderr
md "mydir\subdir" 2>NUL
You don't need to run mkdir mydir
first because
Command extensions, which are enabled by default, allow you to use a single md command to create intermediate directories in a specified path.
https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/md
See also https://ss64.com/nt/md.html
A standard method to create a directory structure is:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Directory=mydir\subdir 1\subdir 2"
md "%Directory%" 2>nul
if not exist "%Directory%\*" (
echo Failed to create directory "%Directory%"
pause
goto :EOF
)
rem Other commands after successful creation of the directory.
endlocal
By default command extensions are enabled and delayed expansion is disabled. The batch code above explicitly sets up this environment.
The command MD creates the complete directory structure to specified directory with enabled command extensions.
MD outputs an error if the directory already exists. This might be useful to inform a user entering the command manually about a possible mistake in entered directory path as it could be that the user wanted to create a new directory and has entered just by mistake the name of an already existing directory.
But for scripted usage of command MD it is often a problem that this command outputs an error message if the directory to create already exists. It would be really helpful if command MD would have an option to not output an error message in case of directory to create already existing and exiting with return code 0 in this case. But there is no such option.
The solution above creates the directory and suppresses a perhaps output error message with redirecting it from handle STDERR to device NUL.
But the creation of the directory could fail because of invalid character in directory path, drive not available (on using full path), there is anywhere in path a file with name of a specified directory, NTFS permissions don't permit creation of the directory, etc.
So it is advisable to verify if the directory really exists which is done with:
if not exist "%Directory%\*"
It is important that the directory path ends now with \*
or at least with a backslash. Otherwise it could be possible for the example that there is a file with name subdir 2
in directory mydir\subdir 1
which on usage of the condition if not exist "%Directory%"
would evaluate to false although there is no directory subdir 2
.
It is of course also possible to do the directory check first and create the directory if not already existing.
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Directory=mydir\subdir 1\subdir 2"
if not exist "%Directory%\*" (
md "%Directory%"
if errorlevel 1 (
pause
goto :EOF
)
)
rem Other commands after successful creation of the directory.
endlocal
The user can see now the error message output by command MD if the directory structure could not be created explaining briefly the reason.
This batch code could be written more compact using operator ||
:
@echo off
setlocal EnableExtensions DisableDelayedExpansion
set "Directory=mydir\subdir 1\subdir 2"
if not exist "%Directory%\*" md "%Directory%" || (pause & goto :EOF)
rem Other commands after successful creation of the directory.
endlocal
For details about the operators ||
and &
read the answer on Single line with multiple commands using Windows batch file.
The command ENDLOCAL is not used before goto :EOF because this command requires also enabled command extensions. Windows command interpreter executes this command implicit on leaving execution of the batch file.
For understanding the used commands and how they work, open a command prompt window, execute there the following commands, and read entirely all help pages displayed for each command very carefully.
echo /?
endlocal /?
goto /?
if /?
md /?
pause /?
set /?
setlocal /?
Read also the Microsoft article about Using Command Redirection Operators.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With