6

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)?

JDoeDoe
  • 361
  • 1
  • 2
  • 7
  • 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. https://richardspowershellblog.wordpress.com/2010/06/06/using-range-operator-to-generate-letters/ –  May 31 '19 at 11:06

11 Answers11

12

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

sysg0blin
  • 406
  • 3
  • 5
  • The Powershell command without foreach doesn't work, but with foreach, it worked like a charm. Thanks! @sysg0blin – Leutecia May 10 '23 at 14:20
3

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!"
)
lit
  • 14,456
  • 10
  • 65
  • 119
  • Nice one (+1), but only 0..9 and the `-WhatIf` is a bit like cheating ;-) –  May 31 '19 at 18:19
  • Thank you @LotPings. The `-WhatIf` is only there to see what would happen. If you actually wanted the files created, remove `-WhatIf`. – lit May 31 '19 at 18:27
  • Yes, but as windows won't allow the same file name with different casing... –  May 31 '19 at 18:29
  • Yes, you are right, @LotPings. I will remove the upper case letters. – lit May 31 '19 at 19:02
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"}
2

For letters, in PowerShell, use:

97..( 97+25 ) | foreach { new-item $env:temp\$( [char]$_ ).txt }
Graham J
  • 457
  • 1
  • 6
  • 15
2

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.

1

The following command in powershell will create multiple files (20) named Doc_.txt in the directory that the command is executed.

new-item $(1..20 | %{"Doc$_.txt"})

I think this command is the closest to the bash equivalent:

touch Doc{1..20}.txt
1

In the PowerShell, you can use New-Item

New-Item a.txt, b.txt, c.txt

then hit Enter

smark91
  • 545
  • 1
  • 5
  • 17
0

This doesn't directly answer your question (as this method requires each file extension be provided), but a more rudimentary way is of course:

New-Item {a..z}.txt, {A..Z}.txt, {0..9}.txt
Yahx
  • 7
  • 4
0

After a few failed attempts, I mashed a couple of the answers above to create files titled [char][int].txt: 1..5 | foreach {New-Item -Path 'X' -Name "abc$_.txt"}, where X is the path. Also, just to thank the original writer of the question, as it described really succinctly exactly the problem I was trying to solve.

0

1..1000000 | foreach {new-item -path C:\Testdata$_.txt}

This worked fine for me. Created 1M files in the C:\Testdata folder.

0

To create Multiple Files using loops:

$count = Read-Host -Prompt "Enter Count"
$count 
for ($i=1 ; $i -le $count;($i++)){    
New-Item -Path "<Path>\<Filename>$i.txt"
}

To have a static number remove the count variable to a fixed number