0

I am using the following code to convert files from php to html. In order for it to work, I have to enter the name of each file on the second line.

p "convert files"
%w(file1 file2 file3).each do |name|
  system %(php #{DIR}/#{name}.php > #{DIR2}/#{name}.htm)
end

Can someone tell me how to make it so it will automatically find any .php files in the main directory and look in any defined folder and their sub-folders for additional .php and save them in similar folder names?

For example:

file1.php -> file1.htm
about-us/file2.php -> about-us/file2.htm
contact-us/department/file3.php -> contact-us/department/file3.htm
Andrew Grimm
  • 78,473
  • 57
  • 200
  • 338
Cofey
  • 11,144
  • 16
  • 52
  • 74

1 Answers1

1

The easiest way is to use Dir:

Dir.chdir('where_the_php_files_area')
Dir['**/*.php'].each do |php|
    htm = 'where_the_html_files_should_go/' +  php.sub(/\.php$/, '.htm')
    system("php #{php} > #{htm}")
end

The ** pattern for Dir.glob (AKA Dir[]) matches directories recursively so Dir[**/*.php] will give you all the PHP files under the current directory.

mu is too short
  • 426,620
  • 70
  • 833
  • 800
  • Thanks! So if I wanted to exclude a directory like "includes" (and possibly others), how could I do that? – Cofey Apr 15 '11 at 00:19
  • @Cofey: The easiest way would be to add an `if(i_like_this_path(File.dirname(php))` check inside the block. [`File.dirname`](http://www.ruby-doc.org/core/classes/File.html#M000027) will give you the directory part of the filename and you'd have to supply something sensible for `i_like_this_path()`. – mu is too short Apr 15 '11 at 00:24
  • @Andrew Grimm: Leave my indentation alone please. I find the Ruby standard of two spaces to be completely unreadable and I have just as much attitude about it as the Ruby community :) – mu is too short Apr 15 '11 at 00:25
  • @mu is too short, I finally got around to trying your script and it doesn't seem to be going into sub-folders to search for .php files. It's only creating .htm files in the main directory. Any ideas? – Cofey Apr 19 '11 at 01:13
  • @Cofey: Is it not finding all the PHP files or is it botching `htm` path? The `Dir['**/*.php']` should find all the PHP files under the current directory. What does `Dir['**/*.php'].inspect` have to say when you're in `irb` in the correct directory? – mu is too short Apr 19 '11 at 01:53
  • Here's an example: in the main directory, I have two files, test1.php and test2.php as well as files in sub-folders like contact-us (test3.php), about-us/ (test4.php). After the script runs, I only have two htm files (test1.htm and test2.htm). I also need the sub-folders and .htm files in addition to the main directory. Thanks for the help! – Cofey Apr 19 '11 at 02:55
  • @Cofey: Given your directory structure, `Dir['**/*.php'].each{|php|htm = php.sub(/\.php$/, '.htm');system("touch #{htm}")}` leaves me with these files: "about-us/test4.htm, about-us/test4.php, contact-us/test3.htm, contact-us/test3.php, test1.htm, test1.php, test2.htm, test2.php". Is that not what you're after? – mu is too short Apr 19 '11 at 03:28