Create multiple files with Powershell?

12,175

Solution 1

For Powershell:

1..5 | foreach { new-item -path c:\temp\$_.txt }

The foreach loop will run for each number in 1 to 5, and generate a file in the desired path with the name of that number passed to the command (represented by the $_)

You could also write it as:

%{1..5} | new-item c:\temp\$_.txt

For cmd:

for /L %v in (1,1,5) do type nul > %v.txt

More information here: cmd/batch looping

Solution 2

Not quite as concise as bash, but it can be done.

@(97..(97+25)) + @(48..(48+9)) |
    ForEach-Object { New-Item -Path "$([char]$_).txt" -WhatIf }

Another way...

@([int][char]'a'..[int][char]'z') + @([int][char]'0'..[int][char]'9') |
    ForEach-Object { New-Item -Path "$([char]$_).txt" -WhatIf }

And one more...

function rng { @($([int][char]$args[0])..$([int][char]$args[1])) }

(rng 'a' 'z') + (rng '0' '9') |
    ForEach-Object { New-Item -Path "$([char]$_).txt" -WhatIf }

If you are desperate to do this in a cmd.exe shell, this might work. When it looks like the correct commands are produced, delete or comment out the echo line and remove the rem from the next line.

@ECHO OFF
SETLOCAL ENABLEDELAYEDEXPANSION
SET "CLIST=abcdefghijklmnopqrstuvwxyz0123456789"
FOR /L %%i IN (0,1,35) DO (
    CALL SET "S=%%CLIST:~%%i,1%%.txt"
    echo TYPE NUL ^>"!S!"
    rem TYPE NUL >"!S!"
)

Solution 3

@Emilson Inoa Your solution is very ingenuous; there's a typo in the names, am sure you meant to exclude the extensions in the array. You'll end up with

("Text1", "Text2", "Text3") | % {ni -Path "/path/to/dir" -Name "$_.txt"}

Solution 4

For letters, in PowerShell, use:

97..( 97+25 ) | foreach { new-item $env:temp\$( [char]$_ ).txt }

Solution 5

Very simple, take a look:

("Text1.txt","Text2.txt", "Text3.txt") | foreach { New-Item -Path "X" -Name "$_.txt" }

You will replace X of course with the path where you want the files to be created.

If further explanation is required, let me know.

Share:
12,175

Related videos on Youtube

JDoeDoe
Author by

JDoeDoe

Updated on June 04, 2022

Comments

  • JDoeDoe
    JDoeDoe almost 2 years

    From this answer I can create multiple files a.txt, b.txt, ... , z.txt. in Bash with:

    touch {a..z}.txt
    

    Or 152 with:

    touch {{a..z},{A..Z},{0..99}}.txt
    

    How can I do this in Powershell? I know New-Item a.txt, but If I want multiple files as above?

    For curiosity, what are the equivalent commands in Command Prompt (cmd.exe)?

    • I.T Delinquent
      I.T Delinquent almost 5 years
      Downvoted because it shows no tried examples or research effort. Please include what you have already tried and the specific errors/issues you are having.
    • Admin
      Admin almost 5 years
      ATM there are only numeric ranges with the range operator .. IIRC that is a feature to come with future PS versions. In windows the command wouldn't work because it doesn't distinguish between the casings. richardspowershellblog.wordpress.com/2010/06/06/…
  • Admin
    Admin almost 5 years
    Nice one (+1), but only 0..9 and the -WhatIf is a bit like cheating ;-)
  • lit
    lit almost 5 years
    Thank you @LotPings. The -WhatIf is only there to see what would happen. If you actually wanted the files created, remove -WhatIf.
  • Admin
    Admin almost 5 years
    Yes, but as windows won't allow the same file name with different casing...
  • lit
    lit almost 5 years
    Yes, you are right, @LotPings. I will remove the upper case letters.