Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create folder in batch script and ignore if it exists

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.

like image 624
steavy Avatar asked Jul 05 '17 16:07

steavy


2 Answers

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

like image 133
phuclv Avatar answered Sep 18 '22 01:09

phuclv


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.

like image 37
Mofi Avatar answered Sep 19 '22 01:09

Mofi