Home > Blockchain >  How do I create multiple directories within a directory that's in a directory with a single lin
How do I create multiple directories within a directory that's in a directory with a single lin

Time:03-16

so my path is from the desktop and I tried: mkdir OS\LAB1\Finance,Public,Archive,Customer The command makes the first 3 directories but the last three get made on my desktop and not within LAB1,how do I do it with one command line? Thank you

CodePudding user response:

not a one liner, but this does the trick

$folders = @(,'Public','Archive','Customer')
New-Item -ItemType Directory C:\OS\LAB1\Finance\ -Force
foreach($folder in $folders){
    New-Item -ItemType Directory C:\OS\LAB1\Finance\$folder -Force
}

The force parameter creates all the directories in one go.

CodePudding user response:

I think that you want to create 4 directories under LAB1 not a single directory four deep. in Bash you can do this.

for i in $(tr ',' '\n' <<< "Finance,Public,Archive,Customer"); do mkdir  "$i"; done

Result looks like this 0 drwxr-xr-x@ 7 camerontownshend staff 224 16 Mar 12:28 . 0 drwxr-xr-x@ 147 camerontownshend staff 4704 16 Mar 11:13 .. 0 drwxr-xr-x@ 2 camerontownshend staff 64 16 Mar 12:28 Archive 0 drwxr-xr-x@ 2 camerontownshend staff 64 16 Mar 12:28 Customer 0 drwxr-xr-x 2 camerontownshend staff 64 16 Mar 12:28 Finance 0 drwxr-xr-x@ 2 camerontownshend staff 64 16 Mar 12:28 Public

Explanation for i in ... -> for loop tr ',' '\n' -> replaces commas with new lines do mkdir "$i" done -> run whatever is inside the do/done pair. The mkdir "$i" - substitutes the iterator from the loop as a variable into the mkdir command

  • Related