View Raw SPL
/****************************************************************************
*                                                                           *
*   FILESTRREP.SPL Copyright 2010 (C) DSP Development Corporation           *
*                                                                           *
*   Author:        Randy Race                                               *
*                                                                           *
*   Synopsis:      Replaces target strings in a list of files               *
*                                                                           *
*   Revisions:     11 Apr 2010     RRR     Creation                         *
*                  19 Jun 2010     RRR     fputs no escape translation      *
*                                                                           *
****************************************************************************/

#if @HELP_FILESTRREP

    FILESTRREP

    Purpose: Replaces a pattern string with a new pattern for each input file

    Syntax:  FILESTRREP("filespec", "patstr", "repstr", recurse)

               filespec - A string, the list of files to process. Filespec
                          can include wildcard characters such as * and ?.
                          
                 patstr - A string, the pattern to search for.

                 repstr - A string, the replacement pattern.

                recurse - Optional, an integer. Recurse into sub-directories
                          if found.

                             0: do not recurse (default)
                             1: recurse

    Returns: An integer, the number of files where the replacement occured.

    Example:
             filestrrep("\temp\*.txt", "January", "April")

             Returns the count of the number of files in the "\temp" folder
             with extension ".txt" where the text "January" was replaced
             with the text "April".

    Remarks:
             The pattern and replacement strings are case sensitive.

             If the pattern string is not found in a file, the file remains
             unchanged.

    See Also:
             Strrep
#endif


/* replaces a string in all files in a directory */
filestrrep(filespec, srcstr, targstr, recurse, verbose)
{
        local dirlist, filelist, file, filedir, temp, i, j, cnt = 0, num;

        if(argc < 5)
        {
                if (argc < 4)
                {
                        if (argc < 3)
                        {
                                error("filerep - file specification, search string and target string required");
                        }
                
                        recurse = 0;
                }

                verbose = 0;
        }

        /* check inputs */
        if (strlen(srcstr) == 0 || strlen(filespec) == 0)
        {
                return(0);
        }

        /* get directory */
        (dir, temp) = dirpath(filespec, 1);

        /* get newline delimited filelist */
        (filelist, num) = filerep_filelist(filespec, recurse);

        i = 1;

        while (1)
        {
                /* get file name from list */
                file = strget(i, filelist, strescape("\n"));
                
                if (strlen(file) > 0)
                {
                        /* get directory */
                        (filedir, temp) = dirpath(file, 0);
                        
                        if (strlen(filedir) <= 0)
                        {
                                /* fullname */
                                file = dir + file;
                        }

                        /* replace string */
                        cnt += filerep_replace(srcstr, targstr, file);
                        i++;

                        if (verbose)
                        {
                                echo(sprintf("Files %ld of %ld, Replaced %ld", i, num, cnt));
                        }
                }
                else
                {
                        break;
                }
        }
        
        return(cnt);
}


/* returns a newline delimited list of files */
filerep_filelist(spec, recurse)
{
        local tempfile, cmd, options, s = "";

        if (argc < 2)
        {
                if (argc < 1) spec = "*.*";
                
                recurse = 0;
        }

        /* flag to recurse into sub-directories */
        options = recurse ? "/S" : "";

        /* build temporary file */
        tempfile = getmiscpath(1, 1) + "templist.txt";

        /* directory listing command */
        cmd = sprintf('dir "%s" /b %s > "%s"', spec, options, tempfile);

        /* run listing */
        run(cmd, -1);

        /* check tempfile existence */
        if (fstat(tempfile) > 0)
        {
                /* return listing as a string */
                s = strfile(tempfile, 0, 1, 0, 0);
                delfile(tempfile);
        }

        /* number of files */
        num = strmatch(strescape("\n"), s);
        num = length(num);

        return(s, num);
}


/* replace srcstr with targstr in a file */
filerep_replace(srcstr, targstr, fname)
{
        local instr, outstr, status = 0;

        /* convert to string */
        instr = strchars(readb(fname, ubyte));

        if (strlen(instr) > 0)
        {
                /* case sensitive replace */
                outstr = strrep(instr, srcstr, targstr, 1);

                /* check if any changes were made */
                if (outstr != instr)
                {
                        /* save changes to original file */
                        writeb(fname, ubyte, 1, charstrs(outstr));

                        status = 1;
                }
        }
        
        return(status);
}


filerep_replace_error()
{
        return(0);
}