Aeternus
Aeternus

Reputation: 365

How to create many files through the windows 10 command line using a for-loop?

I am creating a directory that stores many years. I don't want to create one by one because it will take a long time. As an example, how would I create html files from the year 1980 to 2014 using a for loop in windows command line? Furthermore, is it possible to edit the contents of the file to contain text with its corresponding year? I need this for my programming project.

I have the command line open to the desired directory.

Upvotes: 2

Views: 3817

Answers (1)

Ken White
Ken White

Reputation: 125728

You can do this easily from the command line, using for. Test first using echo, as I've done

for /L %i in (1980,1,2014) do echo %i

Replace the echo with whatever you want to use to generate HTML. (The command line doesn't do that for you, unless you just want an empty file.) The easiest way might be to create a template file (something like base.html) and then just copy it in the loop:

for /L %i in (1980,1,2014) do copy base.html %i.html

The documentation for for (available by typing for /? or help for at the command prompt) says in part:

FOR /L %variable IN (start,step,end) DO command [command-parameters]

The set is a sequence of numbers from start to end, by step amount.

So (1,1,5) would generate the sequence 1 2 3 4 5 and (5,-1,1) would generate the sequence (5 4 3 2 1)

Upvotes: 7

Related Questions