# How to make batch files in Windows 7

**URL:** <https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777>\
**Category:** Factual Questions\
**Created:** [May 6, 2014, 2:56am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777 "2014-05-06T02:56:03Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![brujaja](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@brujaja](https://boards.straightdope.com/u/brujaja)\
**Post date:** [May 6, 2014, 2:56am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/1 "2014-05-06T02:56:03Z")

</div>

I just want to make a thousand files called “Filename000” through “Filename999.”

How do I do that from the command line?

---

<div class="post-metadata">

**Author:** ![brujaja](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@brujaja](https://boards.straightdope.com/u/brujaja)\
**Post date:** [May 6, 2014, 3:18am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/2 "2014-05-06T03:18:08Z")

</div>

Hmm, it looks like what I want to do is not make a batch file, but make files in a batch. You know, all at once.

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [May 6, 2014, 3:28am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/3 "2014-05-06T03:28:56Z")

</div>

This is close:

```auto

for /l %i in (0, 1, 999) do echo > "filename%i.txt"

```

I don’t know how to make it use 000 instead of 0, though.

---

<div class="post-metadata">

**Author:** ![yearofglad](https://avatars.discourse-cdn.com/v4/letter/y/b782af/32.png) [@yearofglad](https://boards.straightdope.com/u/yearofglad)\
**Post date:** [May 6, 2014, 3:44am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/4 "2014-05-06T03:44:30Z")

</div>

> [@Dr.Strangelove](#):
>
> This is close:
> 
> ```auto
> 
> for /l %i in (0, 1, 999) do echo > "filename%i.txt"
> 
> ```
> 
> I don’t know how to make it use 000 instead of 0, though.

Don’t know anything about what you’ve written, but I’d say something like:

substitute your “filename%i.txt” with “filename00%i.txt” and run it from 0 to 9 by 1s.

Then use “filename0%i.txt” and run it from 10 to 99 by 1s.

And then run what you wrote from 100 to 999.

That’s one solution. I think.

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [May 6, 2014, 3:58am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/5 "2014-05-06T03:58:41Z")

</div>

> [@yearofglad](#):
>
> That’s one solution. I think.

Yup, that came to mind a minute after I posted that. Would work fine, though I consider it cheating :).

It turns out there is a way to do it natively in a batch file, but it’s ugly due to the batch language having virtually no support for number formatting.

Personally, I’d do this :):

```auto

perl -e "open F, sprintf \"^>filename%03d.txt\", $_ for (0..999)"

```

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [May 6, 2014, 4:49am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/6 "2014-05-06T04:49:19Z")

</div>

Since it’s Windows 7, you’re no longer limited to the weak DOS command line. It comes with PowerShell. Push the Windows key, type in PowerShell and launch it, CD to the directory you want, and just copy and paste the following:

```auto

for ($i=0; $i -le 999; $i++){echo $null > $("filename" + $i.ToString("000") + ".txt")

```

No additional downloads needed.

---

<div class="post-metadata">

**Author:** ![Little\_Pig](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Little\_Pig](https://boards.straightdope.com/u/Little_Pig)\
**Post date:** [May 6, 2014, 5:59am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/7 "2014-05-06T05:59:55Z")

</div>

You can also type help at the PowerShell console command prompt.

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [May 6, 2014, 8:20am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/8 "2014-05-06T08:20:49Z")

</div>

> [@Little\_Pig](#):
>
> You can also type help at the PowerShell console command prompt.

Was this a reply to my post? I’m not sure how it’s relevant; was there a particularly useful article in PowerShell’s help for this topic?

---

<div class="post-metadata">

**Author:** ![brujaja](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@brujaja](https://boards.straightdope.com/u/brujaja)\
**Post date:** [May 6, 2014, 8:40am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/9 "2014-05-06T08:40:39Z")

</div>

I did as **Reply** suggested (except that the powershell window vanishes as soon as it opens; I had to use a regular cmd window.)

The response I receive is

**($i was unexpected at this time.**

I’m damned if I know what that means.  
So I tried what **Dr. Strangelove** suggested, and it said

**perl is not recognized as an internal or external command, operable program or batchfile.**

Any ideas?

---

<div class="post-metadata">

**Author:** ![Little\_Pig](https://avatars.discourse-cdn.com/v4/letter/l/c57346/32.png) [@Little\_Pig](https://boards.straightdope.com/u/Little_Pig)\
**Post date:** [May 6, 2014, 8:47am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/10 "2014-05-06T08:47:33Z")

</div>

> [@Reply](#):
>
> Was this a reply to my post? I’m not sure how it’s relevant; was there a particularly useful article in PowerShell’s help for this topic?

Only if you want to know about the command interpreter syntax. It will also show you this:

REMARKS  
To learn about Windows PowerShell, read the following help topics:  
get-command : Gets information about cmdlets from the cmdlet code.  
get-member : Gets the properties and methods of an object.  
where-object : Filters object properties.  
about\_object : Explains the use of objects in Windows PowerShell.  
about\_remote : Tells how to run commands on remote computers.

```
Conceptual help files are named "about_&lt;topic&gt;", such as:
  about_regular_expression.

```

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [May 6, 2014, 8:48am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/11 "2014-05-06T08:48:56Z")

</div>

> [@brujaja](#):
>
> I did as **Reply** suggested (except that the powershell window vanishes as soon as it opens; I had to use a regular cmd window.)
> 
> The response I receive is
> 
> **($i was unexpected at this time.**
> 
> I’m damned if I know what that means.

The PowerShell window vanishes? Odd, that should not happen… you’re launching it from the start menu, right?

Try this: open a regular command window first, then type powershell inside THAT and push enter. The “C:\blahblahblah” prompt should change into “PS C:\blahblahblah”, indicating that you’re now in a powershell prompt inside the regular cmd prompt. THEN paste the code I wrote above.

> [@](#):
>
> **perl is not recognized as an internal or external command, operable program or batchfile.**
> 
> Any ideas?

Perl is a programming language that Windows does not come with. It ships with many Linux and Unix systems, and for the longest time Windows did not have similar functionality. As of Windows 7, however, PowerShell was included as an alternative to the old command prompt (that you tried to use). You could, if you wanted to, download Perl for Windows and run that, but it’d just be unnecessary work if the PowerShell script works.

PowerShell uses its own language and interpreter, which is why the command I wrote won’t work in a standard shell. It has to be run in a PowerShell.

EDIT: Sorry, shell is just another word for what Windows calls a command prompt

---

<div class="post-metadata">

**Author:** ![brujaja](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@brujaja](https://boards.straightdope.com/u/brujaja)\
**Post date:** [May 6, 2014, 9:04am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/12 "2014-05-06T09:04:34Z")

</div>

I’m afraid that PowerShell vanishes no matter where I start it from. If I type it into the search bar on the start menu and press “enter,” the window appears and vanishes. If I type in powershell but don’t press enter I get 3 choices, powershell, ISE, and modules. Clicking on any of these three produces the same results.

I’m pretty sure I know why that is, but I can’t discuss it online as it would violate this board’s rules. Is there a more visceral/stone age way to do it from a regular command prompt? 🙂

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [May 6, 2014, 9:06am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/13 "2014-05-06T09:06:35Z")

</div>

> [@brujaja](#):
>
> I’m afraid that PowerShell vanishes no matter where I start it from. If I type it into the search bar on the start menu and press “enter,” the window appears and vanishes. If I type in powershell but don’t press enter I get 3 choices, powershell, ISE, and modules. Clicking on any of these three produces the same results.
> 
> I’m pretty sure I know why that is, but I can’t discuss it online as it would violate this board’s rules. Is there a more visceral/stone age way to do it from a regular command prompt? 🙂

What happens if you type it into a regular command prompt, leaving it nowhere to disappear to?

To be clear:

Start -\> cmd  
(inside the cmd window) powershell (enter)

At this point it should look something like (emphasis on the “PS” portion on the last line):

```auto

Microsoft Windows [Version 6.3.9600]
(c) 2013 Microsoft Corporation. All rights reserved.

C:\Users\user>powershell
Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.

PS C:\Users\user>

```

THEN paste the PowerShell script.

---

<div class="post-metadata">

**Author:** ![brujaja](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@brujaja](https://boards.straightdope.com/u/brujaja)\
**Post date:** [May 6, 2014, 9:09am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/14 "2014-05-06T09:09:37Z")

</div>

**"Loading windows managed powershell failed with error 80070002."**

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [May 6, 2014, 9:12am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/15 "2014-05-06T09:12:20Z")

</div>

Gack, I’m so sorry! I was missing a brace at the end of what I pasted. The correct thing to paste into powershell is this:

```auto

for ($i=0; $i -le 999; $i++){echo $null > $("filename" + $i.ToString("000") + ".txt")}

```

If it’s easier for you, you can also just download this [zipped folder with 1000 files named the way you wanted](https://www.dropbox.com/s/r2qn0y1d0geb7x7/1kblankfiles.zip).

---

<div class="post-metadata">

**Author:** ![brujaja](https://avatars.discourse-cdn.com/v4/letter/b/4af34b/32.png) [@brujaja](https://boards.straightdope.com/u/brujaja)\
**Post date:** [May 6, 2014, 9:15am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/16 "2014-05-06T09:15:29Z")

</div>

Aww, that’s so sweet! I’ll try the code first, and the zipfile if I don’t succeed. Thanks so much for all your time & help, you rock!

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [May 6, 2014, 9:31am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/17 "2014-05-06T09:31:07Z")

</div>

> [@brujaja](#):
>
> Aww, that’s so sweet! I’ll try the code first, and the zipfile if I don’t succeed. Thanks so much for all your time & help, you rock!

The code won’t work if you can’t even launch PowerShell (see below).

> [@brujaja](#):
>
> **“Loading windows managed powershell failed with error 80070002.”**

That should not happen. There’s something wrong with your Powershell.

At this point, you can either:

1. Try to fix Powershell, see [Internal Windows powershell error. Failed 80070002 - Server Fault](http://serverfault.com/questions/562886/how-to-resolve-powershell-error-internal-windows-powershell-error-failed-800700) and [http://happyfunpartytime.com/2012/10/powershell-internal-windows-powershell-error-loading-managed-windows-powershell-failed-with-error-80070002/](http://happyfunpartytime.com/2012/10/powershell-internal-windows-powershell-error-loading-managed-windows-powershell-failed-with-error-80070002/)

2. Use the zip I provided above

3. Use **yearofglad** ’s solution, like this:

```auto

@echo off
for /l %%i in (0, 1, 9) do echo > filename00%%i.txt
for /l %%i in (10, 1, 99) do echo > filename0%%i.txt
for /l %%i in (100, 1, 999) do echo > filename%%i.txt

```

(That’s for copy and pasting into a batch file. If you’re typing in the commands straight into a cmd window instead of running a .bat file, use single instead of double percentages (replace %% with %).

1. Download Perl and use Dr. Strangelove’s solution

---

<div class="post-metadata">

**Author:** ![Reply](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/reply/32/15952_2.png) [@Reply](https://boards.straightdope.com/u/Reply)\
**Post date:** [May 6, 2014, 9:33am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/18 "2014-05-06T09:33:56Z")

</div>

What kind of strange scenario calls for creating 1000 blank files in Windows 7 under a DOS prompt, anyway? It sounds like a horrible homework assignment…

---

<div class="post-metadata">

**Author:** ![Dr.Strangelove](https://sea3.discourse-cdn.com/straightdope/user_avatar/boards.straightdope.com/dr.strangelove/32/6613_2.png) [@Dr.Strangelove](https://boards.straightdope.com/u/Dr.Strangelove)\
**Post date:** [May 6, 2014, 9:57am UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/19 "2014-05-06T09:57:40Z")

</div>

A one-liner that doesn’t require PowerShell, Perl, or anything else:

```auto

for /l %a in (0, 1, 9) do for /l %b in (0, 1, 9) do for /l %c in (0, 1, 9) do echo > filename%a%b%c.txt

```

---

<div class="post-metadata">

**Author:** ![andrewm](https://avatars.discourse-cdn.com/v4/letter/a/c57346/32.png) [@andrewm](https://boards.straightdope.com/u/andrewm)\
**Post date:** [May 6, 2014, 1:28pm UTC](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777/20 "2014-05-06T13:28:26Z")

</div>

FYI “echo \> file” on Windows does not produce an empty file. The file will contain a line of text saying “echo is on” or “echo is off”.

[Next page](https://boards.straightdope.com/t/how-to-make-batch-files-in-windows-7/687777.md?page=2)
