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

1
  • 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/… Commented May 31, 2019 at 11:06

12 Answers 12

13

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

Sign up to request clarification or add additional context in comments.

1 Comment

The Powershell command without foreach doesn't work, but with foreach, it worked like a charm. Thanks! @sysg0blin
4

In the PowerShell, you can use New-Item

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

then hit Enter

Comments

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!"
)

4 Comments

Nice one (+1), but only 0..9 and the -WhatIf is a bit like cheating ;-)
Thank you @LotPings. The -WhatIf is only there to see what would happen. If you actually wanted the files created, remove -WhatIf.
Yes, but as windows won't allow the same file name with different casing...
Yes, you are right, @LotPings. I will remove the upper case letters.
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"}

Comments

2

For letters, in PowerShell, use:

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

Comments

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.

Comments

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

Comments

1

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

Comments

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

Comments

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.

Comments

0

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

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

Comments

0

This is how simple it is to create multiple files in Windows PowerShell

ni file1.txt, file2.txt, file3.txt, "file 4 with spaces.txt", file5.txt

ni just stands for new item.

Now to answer your question of create multiple files a.txt, b.txt, ... , z.txt. in Bash with touch {a..z}.txt, you have 2 options:

In Powershell:

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

Or (For touch {a..z}):

1..26 | ForEach-Object { New-Item -ItemType File -Path ("{0}.txt" -f ([char]($_ + 96))) }

Or (For touch {{a..z},{A..Z},{0..99}}.txt):

1..26 | ForEach-Object { New-Item -ItemType File -Path ("{0}.txt" -f [char]($_ + 96)) -Force }; 65..90 | ForEach-Object { New-Item -ItemType File -Path ("{0}.txt" -f [char]::ConvertFromUtf32($_)) -Force }; 0..99 | ForEach-Object { New-Item -ItemType File -Path ("{0:D2}.txt" -f $_) -Force }

You have both the options of entering bash and use your regular touch command or using ForEach-Object in PowerShell. Now one important thing to remember is that unlike Linux Windows does not distinguish between a.txt and A.txt, so you'll end up having either a.txt or A.txt

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.