0

How to make multiple directories with padded zeros from a single call to md (mkdir, New-Item)? From this thread, I see that I can use this command to pad regular directory names with trailing numbers:

0..10 | % { "dir_name{0:000}" -f $_ } | % { New-Item -ItemType directory -Name $_ }

~/directory/
dir_name000
dir_name001
dir_name002
dir_name003
dir_name004
dir_name005
dir_name006
dir_name007
dir_name008
dir_name009
dir_name010

...but is there a less verbose way with a single call to md?

Thanks to all the helpful input on this thread about finding a PowerShell equivalent to the 'nix command: mkdir dir_name{1..9} I see how this command:

0..10 | foreach $_{ New-Item -ItemType directory -Name $("dir_name" + $_) }

...can be done like this:

mkdir $(0..10 | %{"dir_name$_"})

...but how would I slug in the number padding into this syntax? Thank you!

MmmHmm
  • 738
  • 11
  • 24
  • Aha! After many incantations, I figured out a solution: `md -Name $_ $(0..10 | % { "dir_name{0:000}" -f $_ } )` I am having trouble wrapping my head around piping syntax and PowerShell mojo... Any resource suggestions? – MmmHmm Sep 26 '16 at 06:26
  • this looks promising: [Effective Windows PowerShell: The Free eBook](https://rkeithhill.wordpress.com/2009/03/08/effective-windows-powershell-the-free-ebook/) – MmmHmm Sep 26 '16 at 06:34
  • 1
    `0..10 | % { "dir_name{0:000}" -f $_ } | % { New-Item -ItemType directory -Name $_ > $null}` note that the [output is `>` directed](https://technet.microsoft.com/en-us/library/hh847746.aspx) to NULL device `$null`. Read http://stackoverflow.com/a/5263780/3439404 as well. – JosefZ Sep 26 '16 at 07:45

1 Answers1

1

md -Name $_ $(0..10 | % { "dir_name{0:000}" -f $_ } )

~/directory/
dir_name000
dir_name001
dir_name002
...
dir_name008
dir_name009
dir_name010

MmmHmm
  • 738
  • 11
  • 24