Announcement

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

  • Loop over files to convert gph in pdf/png - filelist

    Dear statalisters,

    I am currently looking for a way to open several gph in a row and exporting them to a format readable on any computer -- no matter which STATA version my recipient is using. Since I have a lot of gph and they are all located in the same folder, I thought using the filelist- command and advices on Statalist from Robert Picard but I failed to do so. I keep getting the same no observations issue since the filelist is not working. Am I missing something ?

    Here is my attempt

    ssc install filelist
    filelist , dir(.) pattern(*.gph) norecur

    * extract the identifier code
    gen shortname = regexs(1)
    isid shortname

    * save a copy so that we can load each observation in the loop below
    tempfile files
    save "`files'"

    local obs = _N

    * create a directory to put the Stata dta if it does not already exist
    cap mkdir pdf

    * loop over each file and do whatever's needed
    forvalues i=1/`obs' {

    use "`files'" in `i', clear
    local source = shortname
    local f = dirname + "/" + filename

    graph use "`f'"
    gen source = "`source'"
    graph export "pdf/`source'.pdf", as(pdf) name("`file'") replace

    }

  • #2
    I am going to presume that by "Not working" you mean that the -filelist- command is not finding any *.gph files and is therefore not creating a data set for you. Your filelist command works fine on my machine using pattern(*.dta )on a directory that I know contains dta files, so my *guess* would be that there aren't any files with the gph extension in your "." directory. I know this is simplistic, but if I were debugging your problem, I'd insert
    Code:
    ls *.gph
    right before your filelist command, since you and -filelist- disagree about the presence of such files in your working directory.

    Comment


    • #3
      Here is an example using Nick Cox's -fs- (ssc desc fs)


      Code:
      . cd "C:\Users\scott\Desktop\temp"
      C:\Users\scott\Desktop\temp
      
      . fs *.gph
      gr.gph     graph.gph
      
      . 
      . foreach g in `r(files)' {
        2.         graph use `g'
        3.         local g2 : subinstr local g ".gph" "" 
        4.         graph export "`g2'.pdf", as(pdf) name("`g2'") replace
        5. }
      (file gr.pdf written in PDF format)
      (file graph.pdf written in PDF format)

      Comment

      Working...
      X