8

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.

sysadmin1138
  • 135,853
tomwolber
  • 179
  • 1
  • 1
  • 3

5 Answers5

10

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
Evan Anderson
  • 142,957
5

The following powershell one-liner should do the trick:

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

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

sysadmin1138
  • 135,853
0

Try fileboss.

Chopper3
  • 101,808
0

Here is very fast (lessTested) version of C# code,
You mentioned a python, this is not that unfortunately You can try convert to python. Or if someone can explain how this can be run in powershell.

using System;
using System.IO;

namespace TestnaKonzola
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Enter The First file name:");
            string firstFile = Path.Combine(Environment.CurrentDirectory, Console.ReadLine());
            Console.WriteLine("Enter the number of copyes:");
            int noOfCopy = int.Parse(Console.ReadLine());
            string newFile = string.Empty;
            for (int i = 0; i < noOfCopy; i++)
            {
                newFile = NextAvailableFilename(firstFile);
                Console.WriteLine(newFile);
                File.Copy(firstFile, newFile);   
            }
            Console.ReadLine();



        }
        public static string NextAvailableFilename(string path)
        {
            // Short-cut if already available
            if (!File.Exists(path))
                return path;

            // If path has extension then insert the number pattern just before the extension and return next filename
            if (Path.HasExtension(path))
                return GetNextFilename(path.Insert(path.LastIndexOf(Path.GetExtension(path)), numberPattern));

            // Otherwise just append the pattern to the path and return next filename
            return GetNextFilename(path + numberPattern);
        }
        private static string numberPattern = "{000}";
        private static string GetNextFilename(string pattern)
        {
            string tmp = string.Format(pattern, 1);
            if (tmp == pattern)
                throw new ArgumentException("The pattern must include an index place-holder", "pattern");

            if (!File.Exists(tmp))
                return tmp; // short-circuit if no matches

            int min = 1, max = 2; // min is inclusive, max is exclusive/untested

            while (File.Exists(string.Format(pattern, max)))
            {
                min = max;
                max *= 2;
            }

            while (max != min + 1)
            {
                int pivot = (max + min) / 2;
                if (File.Exists(string.Format(pattern, pivot)))
                    min = pivot;
                else
                    max = pivot;
            }

            return string.Format(pattern, max);
        }
    }
}
adopilot
  • 1,541