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!

3
  • 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
    Commented Sep 26, 2016 at 6:26
  • this looks promising: Effective Windows PowerShell: The Free eBook
    – MmmHmm
    Commented Sep 26, 2016 at 6:34
  • 1
    0..10 | % { "dir_name{0:000}" -f $_ } | % { New-Item -ItemType directory -Name $_ > $null} note that the output is > directed to NULL device $null. Read stackoverflow.com/a/5263780/3439404 as well.
    – JosefZ
    Commented Sep 26, 2016 at 7:45

1 Answer 1

1

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

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

2
  • You should mark this as the answer.
    – root
    Commented Sep 26, 2016 at 16:44
  • @root, thanks - I will as soon as the waiting period is over. Pls vote me up if you think the question or answer are useful :)
    – MmmHmm
    Commented Sep 26, 2016 at 18:01

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .