In Ruby, how would I find all .csv files in a folder and print out path to the files that contain the word "meh"?


EDIT:

This method is significantly more direct and efficient:

d = Dir.new('.')
d.entries.select do |e| 
  /^.+.csv$/.match(e) && IO.readlines(e).grep(/meh/).length > 0
end

This should do it assuming you want to search the current directory

d = Dir.new('.')

# This will find all files whose path ends in .csv
csvs = d.entries.select {|e| /^.+.csv$/.match(e)}

# This will find all .csv files that contain one or more instance 
# of the pattern /meh/
mehs = csvs.select do |e| 
  f = File.open(e)
  [*f.each_line].grep(/meh/).length > 0
end

You can't print Word files directly from browser unless user has a word processor extension for browser like Zotero. An other option is to convert your doc file to PDF, HTML or image format. wvWare is a software you can do this with. see: http://wvware.sourceforge.net/

To create word files with PHPWord. see: http://phpword.codeplex.com/

With this you can save your doc(x) files and add the links to your php page. Or you can directly download file when opening a PHP page by creating custom headers:

<?php
header('Content-Type: application/vnd.ms-word');
header('Content-Disposition: attachment;filename="myfile.docx"');
header('Cache-Control: max-age=0');

// Code that generates DOC

// output the file to the browser
$objWriter = PHPWord_IOFactory::createWriter($PHPWord, 'Word2007');
$objWriter->save('php://output');
exit;
?>

See forum thread here: https://phpword.codeplex.com/discussions/225901

Use an if-else statment to check if w.length() == 5 or not.

Although you don't mention what problem you're trying to solve, since your code runs I'm going to assume the issue is that the output file is empty.

The reason for that is that you're not flushing or closing the file before your program exits. Just add a new_file.close() at the very end and that should get you going.

If you ever used cygwin, it includes xmllint which in turn has the --format option. This was my original approach. However xmllint did not format attributes the way I liked, so I have developed my own script. Since the word documents contain a lot of rsid noise, the script has an option to remove it.

I use the following worklflow:

  • get a word document, let say foo.docx
  • explode-docx -r foo.docx
  • edit foo.docx - make a small change
  • explode-docx -r foo.docx
  • kdiff3 foo foo.bak
Comments


Message :
Login to Add Your Comments .
Privacy Policy - Copyrights Notice - Feedback - Report Violation - RSS 2017 © bighow.org All Rights Reserved .