When you need to create hundreds of sequentially numbered file copies (like poll001.html through poll100.html), manual creation is impractical. Here are three efficient methods to automate this process in Windows environments.
Create a batch file (.bat) with the following code:
@echo off
setlocal enabledelayedexpansion
set BASE=poll
set EXT=.html
set START=1
set END=100
for /L %%i in (%START%,1,%END%) do (
set NUM=00%%i
set NUM=!NUM:~-3!
copy "%BASE%001%EXT%" "%BASE%!NUM!%EXT%"
)
More modern PowerShell solution:
$baseName = "poll"
$extension = ".html"
$startNum = 1
$endNum = 100
1..100 | ForEach-Object {
$newName = "{0}{1:d3}{2}" -f $baseName, $_, $extension
Copy-Item "poll001.html" -Destination $newName
}
For more complex scenarios, use Python:
import shutil
import os
base_name = "poll"
extension = ".html"
start_num = 1
end_num = 100
source_file = "poll001.html"
for i in range(start_num, end_num + 1):
new_filename = f"{base_name}{i:03d}{extension}"
shutil.copy2(source_file, new_filename)
print(f"Created: {new_filename}")
- Error Handling: Always verify source file exists before copying
- Performance: Python is slower than native shell for simple file operations
- Padding: The :03d format ensures 3-digit numbering (001, 002...100)
- Logging: Python version includes progress output
Method | Best For | Complexity |
---|---|---|
Batch | Simple Windows systems | Low |
PowerShell | Modern Windows systems | Medium |
Python | Cross-platform needs | High |
All scripts assume the original file (poll001.html) exists in the execution directory. Adjust paths as needed for your specific file structure.
When working on web development projects, we often encounter situations requiring batch file operations. In this case, we need to create 100 sequentially numbered HTML files (poll001.html to poll100.html) from a template file.
The quickest way for Windows users is through a simple batch script:
@echo off
setlocal enabledelayedexpansion
set template=poll001.html
set prefix=poll
for /l %%x in (1,1,100) do (
set num=00%%x
copy "%template%" "!prefix!!num:~-3!.html"
)
For more modern Windows systems, PowerShell provides cleaner syntax:
$template = "poll001.html"
1..100 | ForEach-Object {
$newName = "poll{0:D3}.html" -f $_
Copy-Item $template $newName
}
For cross-platform compatibility or more complex scenarios, Python is ideal:
import shutil
template = "poll001.html"
for i in range(1, 101):
new_name = f"poll{i:03d}.html"
shutil.copy2(template, new_name)
If you need to modify content while copying:
template = "poll001.html"
with open(template, 'r') as f:
content = f.read()
for i in range(1, 101):
new_content = content.replace("001", f"{i:03d}")
with open(f"poll{i:03d}.html", 'w') as f:
f.write(new_content)
Always include basic error checking:
import os
import shutil
template = "poll001.html"
if not os.path.exists(template):
print(f"Error: Template file {template} not found")
exit(1)
try:
for i in range(1, 101):
new_name = f"poll{i:03d}.html"
shutil.copy2(template, new_name)
except Exception as e:
print(f"Error during file operation: {str(e)}")