How to make batch files in Windows 7

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

How do I do that from the command line?

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.

This is close:

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.

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

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

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:

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

No additional downloads needed.

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?

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?

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

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_<topic>", such as:

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

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? :slight_smile:

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

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

Windows PowerShell
Copyright (C) 2013 Microsoft Corporation. All rights reserved.

PS C:\Users\user>

THEN paste the PowerShell script.

"Loading windows managed powershell failed with error 80070002."

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:

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.

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

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 and

  2. Use the zip I provided above

  3. Use yearofglad’s solution, like this:

@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

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…

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

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

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”.