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.

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
5
  • 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
    Oct 25 '18 at 20:06
  • Follow-up: I guess goto :eof should be added after the copy statement. (Ref: stackoverflow.com/a/6728702/5457466)
    – Egalth
    Oct 25 '18 at 20:14
  • 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. Oct 26 '18 at 12:25
  • 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.
    – Egalth
    Oct 26 '18 at 16:15
  • 1
    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. Oct 26 '18 at 19:17
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).

2
  • 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. Jul 29 '10 at 16:08
0

Try fileboss.

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);
        }
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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