简体   繁体   中英

Create directories if not exists in windows with for loop and list

I'm fairly new to batch files (and I just want the basic batch files, no scripts like powershell, etc.)

As part of our configuration management, I'd like to create a number of directories if it does not exist, eg pseudo code

myDir[1] = 'd:\logging\Folder1'
myDir[2] = 'd:\logging\Folder2'
...
myDir[9] = 'd:\logging\Folder9'

for i=1 to 9
  if not exist myDir[i] mkdir myDir[i]

I'm okay with the directories in the array being hardcoded

Thanks!

I don't see why you even need an array. It seems like a list would work just fine.

for %%F in (
  "d:\logging\Folder1"
  "d:\logging\Folder2"
  ... etc.
  "d:\logging\FolderN"
) do if not exist "%%~F\" mkdir "%%~F"

I use quotes around the folder paths just in case any of the names contain spaces or poison characters.

The trailing \\ in the IF NOT EXIST test forces it to only match folders and not files.

This is another method. The 2>nul just keeps the screen clear if any of the folders already exist.

The 1,1,9 means to start at 1 and count in steps of 1 up to 9

@echo off
for /L %%a in (1,1,9) do md "d:\logging\Folder%%a" 2>nul

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM