我怎样才能在Windows中创build一个文件的多个编号副本?

我有一个名为poll001.html的文件,需要创build100个增量命名的副本(即poll002.html,poll003.html …等)。 我知道这是愚蠢的,但这是老板想要的。 任何build议这与脚本,命令行或Python? 再次,对不起,这是一个荒谬的要求。

一些批次。 用你的源文件名replace“source-file.html”。 这也会做你的领先零。 将其保存为.BAT或.CMD,然后让呃裂口。

@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 

编辑:

为了解决一个不太常见的情况下sysadmin1138的答案的精神:

 @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 

下面的PowerShell的一行应该做的伎俩:

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

尝试fileboss 。

这里是非常快速(lessTested)版本的C#代码,
你提到了一个Python,这不是很不幸,你可以尝试转换为Python。 或者,如果有人可以解释如何在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); } } } 

一个batch file应该这样做。 从我的头顶上:

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

获得前导零将有点棘手,但这应该到达那里。 如果你需要这些零,

 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 

如果在batch file中使用这个N,则需要在N之前的两倍。 如果直接从cmd提示符运行,则使用单个百分比(%N)。