JDoeDoe
JDoeDoe

Reputation: 371

Create multiple files with Powershell?

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

Upvotes: 6

Views: 27720

Answers (12)

Shatadip Majumder
Shatadip Majumder

Reputation: 132

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

Upvotes: 0

Udit Devadiga
Udit Devadiga

Reputation: 11

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

Upvotes: 1

Ritesh Sinha
Ritesh Sinha

Reputation: 41

In the PowerShell, you can use New-Item

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

then hit Enter

Upvotes: 4

Jorge Lainez
Jorge Lainez

Reputation: 66

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

Upvotes: 1

Hari Sannidhi
Hari Sannidhi

Reputation: 1

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

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

Upvotes: 0

Anogoya Dagaate
Anogoya Dagaate

Reputation: 107

@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"}

Upvotes: 3

user9455949
user9455949

Reputation:

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.

Upvotes: 0

Yahx
Yahx

Reputation: 7

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

Upvotes: 0

Emilson Inoa
Emilson Inoa

Reputation: 21

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.

Upvotes: 2

lit
lit

Reputation: 16236

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

Upvotes: 3

Graham J
Graham J

Reputation: 497

For letters, in PowerShell, use:

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

Upvotes: 2

sysg0blin
sysg0blin

Reputation: 416

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

Upvotes: 13

Related Questions