Help directory name issue

I am searching inside of the files ,like this

unless /.*test/.match( file_name ) || /.*Test/.match( file_name )
        text = File.read(file_name)
        text.scan(/find/) do |it|
        $files << ("#{it} ---> #{File.basename(file_name) } =====>
#{File.dirname(file_name)}")
end

I can list the keyword, file name and directory name with path name,
but i want
1- file name without any extension. for example : *.rb ,*.java, *.txt ,
...
2- I want to output only last directory name. Not path name. for
example: C:\\test\myfiles\, C:\\test\outputs\ .... so only I want to
output myfiles and outputs directory names
3- i want to output the search keyword`s left and right side sentences
also.

please help me. please!

···

--
Posted via http://www.ruby-forum.com/.

Pathname to the rescue:

http://www.ruby-doc.org/stdlib/libdoc/pathname/rdoc/index.html

Also, please look into $` (prematch) and $' (postmatch) and / or
MatchData and Regexp#match.

Kind regards

robert

···

2009/8/25 Ahmet Kilic <ahmedkilic@gmail.com>:

I am searching inside of the files ,like this

unless /.*test/.match( file_name ) || /.*Test/.match( file_name )
text = File.read(file_name)
text.scan(/find/) do |it|
$files << ("#{it} ---> #{File.basename(file_name) } =====>
#{File.dirname(file_name)}")
end

I can list the keyword, file name and directory name with path name,
but i want
1- file name without any extension. for example : *.rb ,*.java, *.txt ,
...
2- I want to output only last directory name. Not path name. for
example: C:\\test\myfiles\, C:\\test\outputs\ .... so only I want to
output myfiles and outputs directory names
3- i want to output the search keyword`s left and right side sentences
also.

please help me. please!

--
remember.guy do |as, often| as.you_can - without end
http://blog.rubybestpractices.com/

I am searching inside of the files ,like this

unless /.*test/.match( file_name ) || /.*Test/.match( file_name )
text = File.read(file_name)
text.scan(/find/) do |it|
$files << ("#{it} ---> #{File.basename(file_name) } =====>
#{File.dirname(file_name)}")
end

I can list the keyword, file name and directory name with path name,
but i want
1- file name without any extension. for example : *.rb ,*.java, *.txt ,
...
2- I want to output only last directory name. Not path name. for
example: C:\\test\myfiles\, C:\\test\outputs\ .... so only I want to
output myfiles and outputs directory names

I suggest you look at File::split, File::basename or String.split.

3- i want to output the search keyword`s left and right side sentences
also.

I am not sure what that means. You can print the line containing a
keyword easily with

File.open(filename){|f| f.each_line{|l| STDOUT << l if l =~ keyword_regexp }}

and if you want sentences you could possibly split on punctuation
instead of newlines.

HTH

Michal

···

2009/8/25 Ahmet Kilic <ahmedkilic@gmail.com>:

In addition, you can always use the universal hammer: split().

fname = "C:/path/to/dir/pic10.jpg"
pieces = fname.split("/")
puts pieces[-2] #dir

parts = pieces[-1].split(".")
puts parts[0] #pic10

···

--
Posted via http://www.ruby-forum.com/.

7stud -- wrote:

In addition, you can always use the universal hammer: split().

fname = "C:/path/to/dir/pic10.jpg"
pieces = fname.split("/")
puts pieces[-2] #dir

parts = pieces[-1].split(".")
puts parts[0] #pic10

thank you very very much. That was what I want.

and if you want sentences you could possibly split on punctuation
instead of newlines.

Yeah, I want that. I want to output some sentences, they are sql
sentences insert, update. delete and table column names.

Thanks I will try it.

···

--
Posted via http://www.ruby-forum.com/\.

I find that I often use

fname.split(File::Separator)

to make my code more OS-agnostic..

While I've heard that "/" will work in Ruby code run on Windows.. I
don't have a system to test.

Should I continue using File::Separator?

···

On Tue, 25 Aug 2009 19:59:36 +0900 7stud -- <bbxx789_05ss@yahoo.com> wrote:

fname = "C:/path/to/dir/pic10.jpg"
pieces = fname.split("/")
puts pieces[-2] #dir