Announcement

Collapse
No announcement yet.
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Changing a code in multiple do files

    I have 100 plus do files that contains the following lines

    *<_version_>
    gen version= "V01"
    label var version "Version of the data"
    *</_version_>

    I am looking for a way to change this line of code between the html tags so that it becomes this:

    *<_version_>
    gen version= "V02"
    label var version "Version of the data"
    *</_version_>

    Anyone knows a quick way to do this without opening each of the 100 do files?

    Appreciate any help!



  • #2
    I have never actually attempted this, but here's how I would approach it. If you don't already have Robert Picard's -filelist- program (available from SSC), install it. The first step is to build up a data set containing the full pathnames of the files. Since you have given no information about that aspect of the problem, I can't advise you about how you would code that: you might be able to do it in a single step if there is a single directory (perhaps with subdirectories participating) and a simple pattern to the names. Or you might have to do it in several steps and then append several data sets each containing some of the files. Anyway, once that is done:

    Code:
    gen strL contents = fileread(dirname+"/"+filename)
    replace contents = subinstr(contents, `"gen version= "V01""', `"gen version= "V02""', .)
    gen long new_file_size = filewrite(dirname+"/"+filename, contents, 1)
    I strongly recommend you test this out on a small number of files first to make sure it works as expected before you apply it to all 100+. And I would back up all the original files first, just in case. Use at your own risk.

    Be aware of some limitations of this code: for simplicity it seeks to replace -gen version = "V01"- anywhere it occurs in each file. I did not restrict the code to the entire context you show in #1 because it gets complicated dealing with the proper line break characters depending on your OS. If this replacement target occurs elsewhere and you don't want to change it in those places, then the code will need modification. Another thing: this code requires an exact character by character match to -gen version = "V01"- in the target file to trigger a replacement. If some of the files have variant spacing, or have things like v1 or v01 or V1 instead of V01, there will be no change made. Similarly if the target file has `"V01"' instead of "V01", it will not be replaced.
    Last edited by Clyde Schechter; 09 Aug 2022, 11:13.

    Comment


    • #3
      The first step is to build up a data set containing the full pathnames of the files.
      This is fairly easy in Windows. Put all of the files into a single folder. Use control A to select all. Hold down shift, then right click on the selected files. Select "copy as path" from the tooltip menu. Now paste into either a text document or excel.
      Last edited by Daniel Schaefer; 09 Aug 2022, 13:09.

      Comment

      Working...
      X