How can I create multiple, numbered copies of a file in Windows?

21,621

Solution 1

Some batch-fu. Replace "source-file.html" with your source filename. This'll do your leading zeros, too. Save this as a .BAT or .CMD and let 'er rip.

@echo off

for /L %%i IN (1,1,100) do call :docopy %%i
goto :EOF

:docopy
set FN=00%1
set FN=%FN:~-3%

copy source-file.html poll%FN%.html

Edit:

To solve a less general case in the sprit of sysadmin1138's answer:

@echo off
for /L %%i IN (1,1,9) do copy source-file.html poll00%%i.html
for /L %%i IN (10,1,99) do copy source-file.html poll0%%i.html
copy source-file.html poll100.html

Solution 2

The following powershell one-liner should do the trick:

2..100 | %{cp poll001.html ("poll{0:D3}.html" -f $_)}

Solution 3

A batch-file should do it. From the top of my head:

for /L %%N in (1,1,100) do echo <html></html> > poll%%N.html

Getting leading zeros in will be a bit trickier, but this should get there. If you need those zeros,

for /L %%N in (1,1,9) do echo <html></html> > poll00%%N.html
for /L %%N in (10,1,99) do echo <html></html> > poll0%%N.html
echo <html></html> > poll100.html

The double percent in front of the N is needed if this is used inside of a batch-file. If you're running this directly from a cmd prompt use a single percent (%N).

Share:
21,621

Related videos on Youtube

tomwolber
Author by

tomwolber

Updated on September 17, 2022

Comments

  • tomwolber
    tomwolber over 1 year

    I have a file named poll001.html, and need to create 100 copies that are named incrementally (i.e poll002.html, poll003.html...etc). I know this is stupid, but it is what boss-man wants. any suggestions to this with either a script, command-line, or python? Again, sorry this is a ridiculous request.

  • Deb
    Deb almost 14 years
    That's what I get for doing it from memory. Fixed.
  • Spence
    Spence almost 14 years
    Cute hack re: the leading zeros. I solved for a more general case... >smile< The greater-than and less-than symbols in your "echo" statements are going to cause problems, though I realize you're probably just providing the "html" content there as an example. A literal user might not follow why the script doesn't work as expected.
  • Egalth
    Egalth over 5 years
    This answer inspired me to learn more about batch scripting, even though it's eight years old by now, thanks for that! But I'm confused. Isn't the idea to jump to :end only after the last iteration of the loop? I wrote a similar script where I replaced the copy statement with ECHO %FN" and added a new line (ECHO.) on the last line after :end. When I run this it's clear that :end is called once for every iteration. What syntax should I used to call :end only once at the end of the script?
  • Egalth
    Egalth over 5 years
    Follow-up: I guess goto :eof should be added after the copy statement. (Ref: stackoverflow.com/a/6728702/5457466)
  • Spence
    Spence over 5 years
    The "goto :EOF" will be called once after the for loop completes. I removed the ":end". ":EOF" is a label that implicitly means End Of File and does not need to actually be present as a label in the file.
  • Egalth
    Egalth over 5 years
    Thanks for clarifying. I found that adding goto :eof immediately after the copy statement returns execution to the for loop, and does not actually jump to the end of the file (compare the SO reference in my previous comment). However, putting a goto :eof after the loop, as in your edit, does indeed jump to the end of the file. So it seems to me that goto :eof behaves differently inside a subroutine compared to when it's called as a single statement.
  • Spence
    Spence over 5 years
    Think of the CALL as starting a new child CMD processor which begins processing at the label that's being CALLed (with "%1", "%2", etc being the argument that the label was CALLed with). When the child command processor reaches the end of the file (either by "goto :EOF" or by literally hitting the end of file) the child terminates and control returns back to the parent, which in this case, is the FOR loop.