tags:

views:

78

answers:

1

Batch file search and replace using wildcards

I have a html (txt) file I am using as a template or sourcefile to create further html files.  Filename = pg_0001.htm and it contains a line of code thus:

pg_0001.jpg

I want to parse the pg_0001.htm sourcefile, increment and replace the jpeg string, like this:  "pg_0002.jpg", and then output the edited htm file to a new filename pg_0002.htm

I then take each newly created file (pg_0002.htm, pg_0003.htm etc) as the sourcefile and repeat the processing until I have reached my target goal (let's say 100 newly created htm files containing code to display the corresponding jpeg.

It must be done this way (fileX.htm containing fileX.jpg) because there is other javascript that uses these incremented filenames as function input.

I used to know how to write incrementing batch files many years ago but I'm old & very rusty now.  Can anyone please help me do this?  Many thanks in advance.

regards Harry

A: 

I figured it out eventually. Built 250 pages with THIS little javascript gem. Started by building the filename with zeroes padding. Build the HTML markup for the new files by concatenating strings. Put the lot in a write loop to increment the filenames etc...

fso = new ActiveXObject("Scripting.FileSystemObject");

firstfile = 1; lastfile = 250;

a = firstfile; do {

    var titlestr = new String("pg_");
    var strnewswf = new String("pg_");
    var strnewfile = new String("pg_");
    var szFileNum = new String(a);
    var szFileNumLen = 4 - szFileNum.length;
    for (i = 0; i < szFileNumLen; i++)
         strnewfile += "0";
    titlestr = strnewfile + szFileNum;
    strnewswf = strnewfile + szFileNum + ".jpg";     
    strnewfile = strnewfile + szFileNum + ".htm";

    var fh = fso.CreateTextFile("E:\\"+strnewfile, true);

HTMLstring='\n'; HTMLstring+='\n'; HTMLstring+='\n'; HTMLstring+=''+titlestr+'\n'; HTMLstring+='\n';

//.........more markup here..........

HTMLstring+='\n';

    fh.write(HTMLstring);


    fh.Close(); 
    a++;

} while (a <= lastfile)