Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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.

share|improve this question
add comment

5 Answers

Try fileboss.

share|improve this answer
add comment

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|improve this answer
    
That's what I get for doing it from memory. Fixed. –  sysadmin1138 Jul 29 '10 at 15:36
    
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. –  Evan Anderson Jul 29 '10 at 16:08
add comment

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);
        }
    }
}
share|improve this answer
add comment

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 end

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

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

:end

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
share|improve this answer
add comment

The following powershell one-liner should do the trick:

2..100 | %{cp poll001.html ("poll{0:D3}.html" -f $_)}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.